Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Count sum of all digits

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.

Answer*

Draft saved
Draft discarded
Cancel
5
  • \$\begingroup\$ Are you sure the -100 bonus is added here? Because it states "-100 Bonus if your code qualifies for the -50 bonus and does not use eval or 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 like eval does). \$\endgroup\$ Commented Oct 9, 2018 at 11:56
  • \$\begingroup\$ @KevinCruijssen Yes, as it does not use eval or similar to handle expressions. ⍎¨ is only used to convert digits to integers, not to handle expressions. \$\endgroup\$ Commented Oct 9, 2018 at 12:00
  • \$\begingroup\$ Ah wait, I looked at your explanation incorrectly. But isn't the kinda an input+eval builtin then, or is eval always done implicitly when expressions are input? \$\endgroup\$ Commented Oct 9, 2018 at 12:06
  • 1
    \$\begingroup\$ @KevinCruijssen 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\$ Commented Oct 9, 2018 at 12:19
  • \$\begingroup\$ If 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\$ Commented Oct 9, 2018 at 12:22

AltStyle によって変換されたページ (->オリジナル) /