# How many sevens? [closed]

A retail clerk writes down every whole number from 1 to 123456789 on a sheet of paper.

How many sevens (7s) did he write down in total?

## closed as off-topic by JonMark Perry, IAmInPLS, Beastly Gerbil, Rand al'Thor, Milo BrandtSep 6 '16 at 1:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question is off-topic as it appears to be a mathematics problem, as opposed to a mathematical puzzle. For more info, see "Are math-textbook-style problems on topic?" on meta." – JonMark Perry, IAmInPLS, Beastly Gerbil, Rand al'Thor, Milo Brandt
If this question can be reworded to fit the rules in the help center, please edit the question.

• Fun fact: if he did this with 12pt writing (with no space between lines), and wrote in 20 columns on both sides, the piece of paper would need to be over 8 miles long. – Will Sep 5 '16 at 11:09
• Welcome to Puzzling! plz help why? remember that ongoing contest puzzles are not valid here – lois6b Sep 5 '16 at 11:10
• On a sheet of paper? A single sheet? I want to see it! – rhsquared Sep 5 '16 at 11:13
• @BeastlyGerbil Will is a time traveller. he spent over 3 years writting down those numbers and now he came back to tell us – lois6b Sep 5 '16 at 11:25
• @lois6b That was added later, not by the OP, so we don't really know if it was intended to be a computer puzzle – bg6471 Sep 5 '16 at 11:44

(sorry for using dot as a separator for thousands. I'm from Romania, this is how we do it here)

96.022.049

Reasoning:

1 - 10 => 1.
10 - 100 => $9 * 1 + 10 = 19$ (70 to 79 not counting twice for 77 because we already counted that).
So this means 1 - 100 we get $20$.
100 - 1000 => $9 * 20 + 100 = 280$ (10 times what we had before and all the numbers 700 - 799).
So 1 to 1000 we get 300.
In the same manner 1 - 10000 => $4.000$
1 - 100000 => $50.000$

Following the same pattern:

1 - 100.000.000 => $80.000.000$
Now we count the total from 100.000.000 to 120.000.000.
We can ignore the leading 1 we end up with 2 times the numbers of 7 between 1 and 10.000.000 which was 7.000.000 so 14.000.000.

Again following the same pattern:

add the numbers of 7 between 120.000.000 and 123.000.000 which is $3*600.000 = 1.800.000$.

In the same manner we get.

123.000.000 - 123.400.000 => $4*50.000 = 200.000$.
123.400.000 - 123.450.000 => $5*4.000 = 20.000$.
123.450.000 - 123.456.000 => $6*300 = 1.800$.

Now it gets tricky.

123.456.000 - 123.456.699 => $7*20 = 140$.
123.456.700 - 123.456.789 => $90 + 10 + 9 = 109$ (one 7 in every number - the hundreds, + 10 of them have 2 7's - the tens + 9 for the last digit).

Summing this up we get

$80.000.000 + 14.000.000 + 1.800.000 + 200.000 + 20.000 + 1.800 + 140 + 109$

Which is

$96.022.049$

I hope I didn't miss anything.

• I'm not sure if you've missed anything, but you definitely counted some of them more times. – elias Sep 5 '16 at 12:01
• This overcounts. {10,...,100} indeed have 10 7s in the tens place, but have only 9 7s in the units place -- the other 7 in the units place comes from 7, which you'd counted in your {1,...,10} stage. There is more multiple counting later. – Rosie F Sep 5 '16 at 12:01
• @elias. I think I missed something and that's why I counted some of them twice. I'm revising this, but at least the approach is good. – Marius Sep 5 '16 at 12:01
• – elias Sep 5 '16 at 12:46
• @elias nice one! hahahaha I mantain that, is OP's decision hahahah i loled – lois6b Sep 5 '16 at 12:52

I might be wrong, as I did all the calculations manually, but as far as I can see, this has a beautiful symmetry, so let me post a solution which uses it:

I would do it by counting it in separate ranges, namely the following:

• 1 to 99999999
We can actually count it from 0 to 99999999, or even from 00000000 to 99999999 without the answer changing. From this latter form it is trivial that all the digits appear the same number of times, and there are a total of 8x100000000 digits (there are 100000000 numbers, each one being 8 digits), so the number of 7s is the tenth of them, that is 80000000.
• 100000000 to 119999999
The prefix being either 10 or 11, does not contain any 7s. 20000000 numbers, each with a 7-digit ending, in which every digit appears the same number of times again. That's 14000000 7s.
• 120000000 to 122999999
Analogously to the previous ones. 3000000 numbers, 6-digit ending. 1800000 7s.
• 123000000 to 123399999
Analogously to the previous ones. 400000 numbers, 5-digit ending. 200000 7s.
• 123400000 to 123449999
Analogously to the previous ones. 50000 numbers, 4-digit ending. 20000 7s.
• 123450000 to 123455999
Analogously to the previous ones. 6000 numbers, 3-digit ending. 1800 7s.
• 123456000 to 123456699
Analogously to the previous ones. 700 numbers, 2-digit ending. 140 7s.
• 123456700 to 123456769
A minor trick here. Besides the usual '70 numbers, 1-digit ending'-mantra resulting in 7 7s, there is another 70 coming from the prefix.
• 123456770 to 123456779
Like the last one: 1 7 in the ending, 20 in the prefix, as the prefix itself has 2 7s now.
• 123456780 to 123456789
The same approach gives 1 in the ending, 10 in the prefix.

In total:

96022049 7s

96022049

Because:

I wrote this code: (I know its ugly)
int num = 0; for (int i= 1; i<= 123456789 ; i++){ if(String.valueOf(i).contains("7")){//if contains at least 1 seven for(int j =0; j < String.valueOf(i).length() ; j++){ if( String.valueOf(i).charAt(j) == ("7").charAt(0) ){// how many 7's inside num++; } } } } System.out.println(num);

• Will that not count numbers like 77 just once? – rhsquared Sep 5 '16 at 11:38
• true ^^' i need to modify it heheh – lois6b Sep 5 '16 at 11:39

I've just run a program that gives me:

96022049

The code used :

 int count = 0; for(int i=1; i <= 123456789; i++) { QString s = QString::number(i); if(s.contains("7")) { int number = s.count(QString::number(7)); count = count + number; } } 

96022049

As wrote the program

• congrats A J , but let me ask, why is this the correct one? my answer and elias are the same and with more votes and less time to post – lois6b Sep 5 '16 at 12:30
• @lois6b This is what surprised me. I didn't expect this. – A J Sep 5 '16 at 12:31
• @AyandaAgustoZwane I must say there are better answers than mine. – A J Sep 5 '16 at 12:31
• Why the screenshot and not the code itself? – Marius Sep 5 '16 at 12:33
• It is fine. OP has all the rights to choose the answer he liked the most. Most of us is not here for the reputation scores anyway. – elias Sep 5 '16 at 12:38

Based on loi6b my version of his program:

96022049

Because:

I wrote this code:
 class Program { static void Main( string[] args ) { long num = 0; for (long i= 1; i<= 123456789 ; i++) { if (i.ToString().Contains("7")) { num += i.ToString().Count(f => f == '7'); } } } }

• I guess you were ninja'd! – IAmInPLS Sep 5 '16 at 11:48
• HAHAHAH i also finished the execution of the updated program. less elegant tho – lois6b Sep 5 '16 at 11:48

96022049

I found this number by using this script :

var sevenTotalCount = 0 for (var i = 1; i <= 123456789; i++) { var iStr = i.toString(); var sevenCount = (iStr.match(/7/g) || []).length; sevenTotalCount += sevenCount; } console.log(sevenTotalCount);

Edit: off by one error

• Only less than 123? You might want to include the final number too. – bg6471 Sep 5 '16 at 11:49
• I posted my test code by mistake =( I fixed it now. – Forcent Vintier Sep 5 '16 at 11:50

You know there is one seven in the last position each ten numbers, ten seven in the second last position each 100, ... So:  1) 123456789/10 = 12345678.9 --> 12345679*1 sevens 2) 123456789/100 = 1234567.89 --> 1234568*10 sevens 3) 123456789/1000 = 123456.789 --> 123457*100 -10 sevens 4) 123456789/10000 = 12345.6789 --> 12345*1000 sevens 5) 123456789/100000 = 1234.56789 --> 1234*10000 sevens 6) 123456789/1000000 = 123.456789 --> 123*100000 sevens 7) 123456789/10000000 = 12.3456789 --> 12*1000000 sevens 8) 123456789/100000000 = 1.23456789 --> 1*10000000 seven
Note the rounding, if the decimal part is >= 0.7, then you can add a seven. Else only the integer part would count. Note also the $-10$ in $3)$ because ten sevens won't appear in the third last position. Then you add: 12345679*1+1234568*10+123457*100-10+12345*1000+1234*10000+123*100000+12*1000000+1*10000000=96022049

Solution:

There are 96'022'049 sevens

• multiply it by 7 and wonder! – elias Sep 5 '16 at 11:54
• @elias but... why? – Puck Sep 5 '16 at 11:55
• There are many loosely related reasons behind this, I think. Actually your answer is very close to 1/10+1/10^2+1/10^3+...=1/9 times 123456789, while the solution of the original problem is close to 7/9 times it - this has a more complicated reason. Have you ever wondered, what is the decimal representation of 10/81? – elias Sep 5 '16 at 13:10
• Since nobody has given constructive criticism yet, and the correct answer has been posted by multiple people for several hours: You say, "there is ... one seven in the second last position each 100 [numbers]". Think about that for a moment. What's the one number between 1 and 100 that has a 7 in the second-to-last position (i.e., the second-from-the-right; i.e., the $10$s position)? If you haven't had an "Aha! moment" yet: call that number $N$, and look ant $N-1$ and $N+1$. – Peregrine Rook Sep 5 '16 at 20:52
• @PeregrineRook Aha!... Edited, now I get the right answer. – Puck Sep 6 '16 at 7:49

Notice some pattern in it.

1        ->0
12       ->1
123      ->22(contain 22 7's in the range of 1 to 123)
1234     ->343
12345    ->4664
123456   ->58985
1234567  ->713307
12345678 ->8367637
123456789->96022049


So now as we minus below number(containg number 7's) by above number

  1-0                       ->1
22-1                      ->11
343-22                    ->321
4664-343                  ->4321
58985-4664                ->54321
713307-58985              ->654322
8367637-713307            ->7654330
96022049-8367637          ->87654412


They getting some pattern (can say palindromic) but actually they not.
Hope you guys would find out something.

96022049

Calculated using the following Python 3 script:

 count = 0 for s in range(123456789+1): count += str(s).count("7") print(count) 

• You're missing many 7s. – IAmInPLS Sep 5 '16 at 11:48
• @IAmInPLS Thanks for the feedback. I fixed the program to account for multiple occurrences of numerals in each number. – Anthony Geoghegan Sep 5 '16 at 12:03
• you're still missing one, as range(n) goes up to n-1 – elias Sep 5 '16 at 12:06

Once.

The number '7' occurs only once, as with all numbers.

• Yes but the queston is how many sevens did he write down in total. Not how many times he wrote 7. – Beastly Gerbil Sep 5 '16 at 11:16
• He wrote down one seven, because there is only one seven. – Mithrandir Sep 5 '16 at 11:17
• This is not lateral-thinking + – Yandrakus Sep 5 '16 at 11:17
• OP is new, maybe he doesnt know about lateral thinking tag. he should specify the statement as: how many digits are seven? – lois6b Sep 5 '16 at 11:18
• +1 If we can assume the OP meant a computer puzzle and the total number of 7s (and edit the question to suit that assumption) why can't we assume it was lateral thinking? – bg6471 Sep 5 '16 at 12:05