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?
Puzzling Stack Exchange is a question and answer site for those who create, solve, and study puzzles. It only takes a minute to sign up.
Sign up to join this communityA 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?
My answer:
(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 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:
In total:
96022049 7s
The answer is:
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);
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; } }
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'); } } } }
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
How about:
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
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.
The answer is:
96022049
Calculated using the following Python 3 script:
count = 0 for s in range(123456789+1): count += str(s).count("7") print(count)
Once.
The number '7' occurs only once, as with all numbers.