38
11
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
.
- The given example (
- -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.
There's also some, like JavaScript, that physically can't do that requirement. – Isiah Meadows – 2015-01-31T07:46:02.960
2And what should
55*96-12
return? – ProgramFOX – 2014-01-15T18:04:02.247155*96-12=5268, should be the same output as entered 5268 – ST3 – 2014-01-15T19:07:16.337
3Bonuses may be a bit on the big side, seems to be becoming a competition on the biggest negative score :) – Joachim Isaksson – 2014-01-15T19:12:42.720
Possibly, however, I have set them, so it wouldn't be fair to change it, as there are some answers, anyway, winner probably will have all bonuses and the shortest code. – ST3 – 2014-01-15T19:15:50.323
7@ST3 if it's virtually impossible to win without the bonuses, then it's almost better to just make them requirements, or be worth less. – Cruncher – 2014-01-15T19:17:00.797
@Cruncher they could still matter for intra-language competition – FireFly – 2014-01-15T19:35:59.803
1Do you want a program or a function? Some guys are posting functions, so I don't know if this is acceptable. – Gabriele D'Antona – 2014-01-15T20:10:50.173
Would it be acceptable to take input on the command-line (as arguments) instead of stdin? – FireFly – 2014-01-15T20:34:17.083
@FireFly edited – ST3 – 2014-01-15T20:38:15.327
1A complete set of 0-9 adds up to 45. A complete set of 0-99 is 45110. A complete set of 0-999 is 4511100. Optimizations exist if one cared about performance rather than codesize. – keshlam – 2014-01-16T04:19:26.203
1@keshlam: if there'd be a proper incentive about performance (e.g. it has to run for very large numbers in decent time, and the bonus for this would be higher) then maybe there'd be more solutions. At the moment there are only two efficient solutions here as far as I can tell – SztupY – 2014-01-16T09:52:55.423
Almost everyone could shorten their solution by using the closed form
1+2+...+n=n*(n+1)/2
. – flonk – 2014-01-16T12:13:09.8031@flonk: This is wrong. Why people don't read tasks, I wonder. I decided to put a note in task (as an edit). Seriously, this is not task about summing numbers. – Konrad Borowski – 2014-01-16T12:35:59.767
1@xfix Thank you for pointing out my mistake. I can't give you a satisfying answer why "people" don't read tasks, but in my personal case the reason is simply that I only do this for procrastination, so only a small part of my brain is really able to focus on PCG. – flonk – 2014-01-16T13:26:32.240
1Do we get the bonus if it processes postfix expressions? (e.g.
3 4 + 2 *
=14
) – AJMansfield – 2014-01-16T16:17:08.890I'd say postfix should count. As long as it's clearly defined syntax... – keshlam – 2014-01-16T18:29:15.687
@ST3 Does my answer (Batch) qualify for the -100 bonus?
– unclemeat – 2014-01-16T22:26:24.523Obviously, if I wasn't already disqualified. – unclemeat – 2014-01-16T23:16:09.017
Anybody have an answer for that? – unclemeat – 2014-01-17T03:07:24.033
@ST3 - if you stated that input up to 2^64, than you should have answer for all numbers up to 2^64, and people who answer need to provide output for such big numbers, so that you can judge whether their solutions are correct. – SergeyS – 2014-01-19T12:20:23.713
How
relies upon any web resources
is a penalty, not disallow? – l4m2 – 2018-03-21T07:55:40.5073-1 because this challenge uses the outdated (and awful) scoring incentive of "bonuses". – mbomb007 – 2018-05-09T21:23:22.810
+1 for the idea, but -1 for the second and third bonus, that's a whole different problem. That equals 0 from me. – nyuszika7h – 2014-09-19T20:16:07.220
I bet that many solutions did not test "The code must be able to properly handle inputs at least up to (2^64)-1.". So who guarantees that all solutions are tested/treated equally? How can a fair judgement be guaranteed? – None – 2014-09-21T07:33:08.410
Please try
python -c 'print xrange(2**64)'
. Python2 complainsOverflowError: long int too large to convert to int
so a Python2 solution relying onxrange()
(orrange()
for other reasons too) to generate1...InputNumber
breaks "The code must be able to properly handle inputs at least up to(2^64)-1
.". Noone seems to complain about this... or am I wrong with ihis conclusion?¿? – None – 2014-09-23T09:30:18.250@yeti The question should probably be fixed, this requirement is either a mistake or fairly arbitrary. The biggest 64-bit integer is
(2^63)-1
, which Python can handle fine. It seems to me that technically any solution that can handle(2^64)-1
would qualify for the -25 bonus as well. – nyuszika7h – 2014-09-23T18:33:24.917