-4
$\begingroup$

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?

$\endgroup$

closed as off-topic by JMP, IAmInPLS, Beastly Gerbil, Rand al'Thor, Milo Brandt Sep 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." – JMP, 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.

  • 4
    $\begingroup$ 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. $\endgroup$ – Will Sep 5 '16 at 11:09
  • 4
    $\begingroup$ Welcome to Puzzling! plz help why? remember that ongoing contest puzzles are not valid here $\endgroup$ – lois6b Sep 5 '16 at 11:10
  • 2
    $\begingroup$ On a sheet of paper? A single sheet? I want to see it! $\endgroup$ – rhsquared Sep 5 '16 at 11:13
  • 4
    $\begingroup$ @BeastlyGerbil Will is a time traveller. he spent over 3 years writting down those numbers and now he came back to tell us $\endgroup$ – lois6b Sep 5 '16 at 11:25
  • 2
    $\begingroup$ @lois6b That was added later, not by the OP, so we don't really know if it was intended to be a computer puzzle $\endgroup$ – bg6471 Sep 5 '16 at 11:44

11 Answers 11

7
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I'm not sure if you've missed anything, but you definitely counted some of them more times. $\endgroup$ – elias Sep 5 '16 at 12:01
  • 1
    $\begingroup$ 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. $\endgroup$ – Rosie F Sep 5 '16 at 12:01
  • $\begingroup$ @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. $\endgroup$ – Marius Sep 5 '16 at 12:01
  • 1
    $\begingroup$ @lois6b, puzzling.stackexchange.com/questions/38945/… $\endgroup$ – elias Sep 5 '16 at 12:46
  • 1
    $\begingroup$ @elias nice one! hahahaha I mantain that, is OP's decision hahahah i loled $\endgroup$ – lois6b Sep 5 '16 at 12:52
24
$\begingroup$

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

$\endgroup$
10
$\begingroup$

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);

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

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; } }

$\endgroup$
6
$\begingroup$

Well, the answer is

96022049

As wrote the program

enter image description here

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – lois6b Sep 5 '16 at 12:30
  • 1
    $\begingroup$ @lois6b This is what surprised me. I didn't expect this. $\endgroup$ – A J Sep 5 '16 at 12:31
  • $\begingroup$ @AyandaAgustoZwane I must say there are better answers than mine. $\endgroup$ – A J Sep 5 '16 at 12:31
  • $\begingroup$ Why the screenshot and not the code itself? $\endgroup$ – Marius Sep 5 '16 at 12:33
  • 1
    $\begingroup$ 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. $\endgroup$ – elias Sep 5 '16 at 12:38
4
$\begingroup$

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'); } } } }

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

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

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

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

$\endgroup$
  • $\begingroup$ multiply it by 7 and wonder! $\endgroup$ – elias Sep 5 '16 at 11:54
  • $\begingroup$ @elias but... why? $\endgroup$ – Puck Sep 5 '16 at 11:55
  • $\begingroup$ 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? $\endgroup$ – elias Sep 5 '16 at 13:10
  • $\begingroup$ 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$. $\endgroup$ – Peregrine Rook Sep 5 '16 at 20:52
  • $\begingroup$ @PeregrineRook Aha!... Edited, now I get the right answer. $\endgroup$ – Puck Sep 6 '16 at 7:49
2
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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)

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

Once.

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

$\endgroup$
  • $\begingroup$ Yes but the queston is how many sevens did he write down in total. Not how many times he wrote 7. $\endgroup$ – Beastly Gerbil Sep 5 '16 at 11:16
  • 1
    $\begingroup$ He wrote down one seven, because there is only one seven. $\endgroup$ – Mithrandir Sep 5 '16 at 11:17
  • $\begingroup$ This is not lateral-thinking + $\endgroup$ – Yandrakus Sep 5 '16 at 11:17
  • $\begingroup$ OP is new, maybe he doesnt know about lateral thinking tag. he should specify the statement as: how many digits are seven? $\endgroup$ – lois6b Sep 5 '16 at 11:18
  • $\begingroup$ +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? $\endgroup$ – bg6471 Sep 5 '16 at 12:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.