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 to5268
. Your code should return the same for either of those inputs - correct answer is81393
. - -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.
55*96-12
return? \$\endgroup\$