## Interview Question

Trader Interview New York, NY

`Jane Street`

## What is the sum of the digits of all the numbers from 1 to

1000000? This is different from the sum of the numbers. For instance the sum of the numbers from 1 to 10 is 55 whereas the sum of the digits is 46.

## Interview Answer

12 Answers

Maybe I'm reading it wrong, but isn't the sum of the digits of 1-10 just 11? 1 digit for 1-9, 2 digits for 10-99? So it's 9+90*2+900*3+9000*4+90000*5+900000+6

no, I think the easy way to solve it in your head is to remember that when adding all digits 1 to 100, you have 50 pairs: 1+100, 2+99, etc. Each pair is 101, times 50 is 5050.

1 to 1000 would be 500500

so 1 to 1,000,000 would be 500,000,500,000

Pretty cool, huh?

scott, dude you should add digits not the numbers, so 99+2 = 18+2 =20. not 101

Scott's answer from May 24 is the correct way to think about it if summing the numbers.

The answer is 27,000,001 - if you do it programatically the operation is a simple map reduce - simply map a digit sum function across the list of values [1, 1000000] and then reduce an addition operator across the result.

Python Proof:

>>> sum(map(lambda n: sum(map(int, str(n))), xrange(1, 1000001)))

27000001

21085156

Don't mind the above answer. I read the question wrong.

Each digit will appear 1+10+100+1000+10000+100000 times. so the answer is 111,111*45+1=4444440+555555+1=4999996

nb is right. Another way you can think about it easily is if you want the digits from 1 to 1,000,000 then each digit should appear 1/10 * 1,000,000 times.

So

100,000 x 6 (for each place value) x 45 (the sum from 1 to 9) + 1 (for the 1 million)

27,000,001

I think John Doe is right.

27,000,001 is what I got. Think of each number as a 6 digit number. The average number each digit could be from 000,000 to 999,999 is (9+0)/2=4.5. Since the average of each number is 4.5 and there are 6 digits the average sum of the digits for a 6 digit number should be 4.5*6=27. There are 1 million numbers from 000,000 to 999,999 so the sum of the digits from 000,000 to 999,999 is 27,000,000. Subtract the digits of 000,000 which is just 0 and add the digits of 1,000,000 which is just 1 to get 27,000,001.

## Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up

The main idea is that if you write all the numbers from 0 to 999999 down as six digit numbers (possibly prepending zeros) then all digits appear the same number of times. So, its digit appears exactly 6 x 1000000/10 = 600000 times. so the result is 600000x 45 +1 (+1 for the number 1000000)

May 2, 2012