This challenge is to write a program or script which counts the sum of all digits within the integers from 1 up to and including a given number.
Input, one positive integer.
Output, the sum of digits in that number and all smaller numbers.
Examples:
Input: 5
Integer Sequence: 1, 2, 3, 4, 5
Sum of Digits: 1 +たす 2 +たす 3 +たす 4 +たす 5 =わ 15
Input: 12
Integer Sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
Sum of Digits: 1 +たす 2 +たす 3 +たす 4 +たす 5 +たす 6 +たす 7 +たす 8 +たす 9 +たす 1 +たす 0 +たす 1 +たす 1 +たす 1 +たす 2 =わ 51
To be clear, this is to count a sum of the digits - not the integers. For single-digit inputs, this will be the same. However, inputs larger than 10 will have different responses. This would be an incorrect response:
Input: 12
Output: 78
Another example, to show the difference:
Input: 10
Integer Sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Sum of Integers (INCORRECT RESPONSE): 1 +たす 2 +たす 3 +たす 4 +たす 5 +たす 6 +たす 7 +たす 8 +たす 9 +たす 10 =わ 55
Digit Sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0
Sum of Digits (CORRECT RESPONSE): 1 +たす 2 +たす 3 +たす 4 +たす 5 +たす 6 +たす 7 +たす 8 +たす 9 +たす 1 +たす 0 =わ 46
A larger test case (CORRECT RESPONSE):
Input: 1000000
Output: 27000001
Rules & Guidelines:
- Submitted code must be a complete program or script - not just a function. If the code requires includes, imports, etc., they must be included in the posted code.
- The number must be input by the user - not hard-coded. Input may be received as a command-line argument, file, stdin, or any other means by which your language can take user input.
- The code must be able to properly handle inputs at least up to
(2^64)-1.
- The code should only output the sum.
- Submitted programs & scripts should be user-friendly and not wasteful of computer resources (e.g.: they should not declare insanely-large arrays to hold every character). There is no strict bonus or penalty for this, but please be good programmers.
Scoring:
Primary scoring mechanism is by code length. Lower scores are better. The following bonuses and penalties also apply:
- -25 Bonus if your code can handle all positive numbers, for example:
1234567891234567891234564789087414984894900000000
- -50 Bonus if your code can handle simple expressions, for example
55*96-12. To qualify for this bonus, the code should handle + - / * (addition, subtraction, division, multiplication) operators and enforce order of operations. Division is regular integer division.
- The given example (
55*96-12) evaluates to 5268. Your code should return the same for either of those inputs - correct answer is 81393.
- -10 Bonus if your code qualifies for the -50 bonus and can handle the
^ (exponent) operator.
- -100 Bonus if your code qualifies for the -50 bonus and does not use
eval or similar to handle expressions.
- +300 Penalty if your code relies upon any web resources.
evalor similar to handle expressions." Since⍎¨seems to execute each character one by one, it's kinda the same as an eval (except it executes the characters one by one instead of all at the same time likeevaldoes). \$\endgroup\$⍎¨is only used to convert digits to integers, not to handle expressions. \$\endgroup\$⎕kinda an input+eval builtin then, or is eval always done implicitly when expressions are input? \$\endgroup\$⎕always takes an expression as input, evaluates it, and returns its result. So to input a string, you'd have to put quotes around it. The fact that a related built-in (⍞) returns the input as raw text shouldn't matter (especially since the symbols indicate that⎕is the primary input method, and⍞is a special variant), as otherwise getting the bonus would require implementing a math evaluator — an entirely different task than the main one. I don't like bonuses, and the -100 one is just silly or had APL in mind, but imho, it does seem an exact fit for the bonus. \$\endgroup\$⎕is indeed the normal way of getting input and automatically handles expressions, I indeed see it fit into the bonus as well, so +1 from me. Bonuses are silly these days anyway, but nice way of utilizing them to minimize your score. \$\endgroup\$