-4

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?

Rand al'Thor
  • 116,845
  • 28
  • 322
  • 627
  • 4
    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 05 '16 at 11:09
  • 4
    Welcome to Puzzling! plz help why? remember that ongoing contest puzzles are not valid here – lois6b Sep 05 '16 at 11:10
  • 2
    On a sheet of paper? A single sheet? I want to see it! – rhsquared Sep 05 '16 at 11:13
  • @RadoslavHristov you might use a magnifying glass – lois6b Sep 05 '16 at 11:14
  • @Will, how do you know that :) – Beastly Gerbil Sep 05 '16 at 11:24
  • 4
    @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 05 '16 at 11:25
  • I too am curious about the need for "help". I wrote a simple Python script which solved this in a few minutes but that feels like cheating. – Anthony Geoghegan Sep 05 '16 at 11:41
  • 1
    @AnthonyGeoghegan is not cheating if the tag is computer puzzle – lois6b Sep 05 '16 at 11:42
  • 2
    @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 05 '16 at 11:44
  • 1
    @lois6b Thanks for the response. Your interpretation was correct. I haven't been around here for very long and this is the first puzzle I've seen here that could be solved with a program. I also wasn't sure if the OP was using the computer-puzzle tag correctly (I hadn't noticed that it had been added by Beastly Gerbil). It did occur to me that this could be a programming assignment for the OP (I see similar homework questions on [so]). – Anthony Geoghegan Sep 05 '16 at 12:11
  • 1
    I'm surprised this isn't a duplicate. – Shuri2060 Sep 05 '16 at 12:12
  • 2
    @AnthonyGeoghegan oh nice. The thing is, is OP duty to know if is a valid puzzle (reading the tour or meta). Both ongoing contest or homework activities are cheating and its OP's responsability. I hope he proceeds with nice judgement – lois6b Sep 05 '16 at 12:14
  • 2
    @AyandaAgustoZwane, there are multiple answers... and could you explain why the hurry and the "help please"? is this a puzzle of your invention or is a task you must answer? – lois6b Sep 05 '16 at 12:23
  • 2
    I must assume that this is from a contest, and I will therefore flag this for closure. If it is not, then please say so. Otherwise, this will be closed and possibly deleted. – Mithical Sep 05 '16 at 12:25
  • To the close-voters: are you sure this is a maths problem rather than a maths puzzle? I suspect there may be a quick, clever, and interesting way of finding the solution, though I haven't found one yet. – Rand al'Thor Sep 05 '16 at 12:27
  • 1
    @randal'thor we dont even know if the OP knows how this site works.. he/she just ask again for help even though there is multiple answers – lois6b Sep 05 '16 at 12:28
  • well thx guys i just found the answer – Ayanda Agusto Zwane Sep 05 '16 at 12:34
  • @Marius, lois6b,A J, elias, Gerbil, etc – Ayanda Agusto Zwane Sep 05 '16 at 12:37
  • Was this puzzle from an ongoing contest? Or why did it 'save' you to find the answer? Just curious :-) – Rand al'Thor Sep 05 '16 at 12:41
  • 1
    I think you should mark as accepted the answer provided by elias. He answered 3 minutes before me and I did have some wrong calculations at first. – Marius Sep 05 '16 at 12:42

11 Answers11

24

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

elias
  • 9,612
  • 3
  • 35
  • 59
10

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

lois6b
  • 3,346
  • 21
  • 41
7

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.

Marius
  • 18,049
  • 4
  • 54
  • 101
  • I'm not sure if you've missed anything, but you definitely counted some of them more times. – elias Sep 05 '16 at 12:01
  • 1
    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 05 '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 05 '16 at 12:01
  • 10-100 actually has only 19 I think, just as RosieF says. – elias Sep 05 '16 at 12:02
  • @elias. Yep you are right. I'm recalculating now. I won't change the approach. Only the digits. – Marius Sep 05 '16 at 12:03
  • I've changed the numbers. Can someone please double check it? – Marius Sep 05 '16 at 12:07
  • I think you missed some in the 123.456.700 - 123.456.789 step; there are more than 10 with 2 7's, namely everything with 77, but also everything with 77. Therefore it should be 18 7's in the tens instead of 10 7's, giving you the same answer as the others. Edit: I see you fixed it while I was typing. – Wu33o Sep 05 '16 at 12:16
  • OP seems to be confused which one to mark right. – A J Sep 05 '16 at 12:39
  • @AJ v.v yeah .. – lois6b Sep 05 '16 at 12:40
  • 1
    @lois6b, http://puzzling.stackexchange.com/questions/38945/next-number-in-the-following-sequence/38946#comment116303_38946 – elias Sep 05 '16 at 12:46
  • ...So then what do you use for decimals? – Mithical Sep 05 '16 at 12:48
  • commas, I suppose – elias Sep 05 '16 at 12:49
  • @Mithrandir. Commas: https://en.wikipedia.org/wiki/Decimal_mark#Countries_using_Arabic_numerals_with_decimal_point – Marius Sep 05 '16 at 12:51
  • 1
    @elias nice one! hahahaha I mantain that, is OP's decision hahahah i loled – lois6b Sep 05 '16 at 12:52
6

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

}

IAmInPLS
  • 11,471
  • 4
  • 43
  • 112
6

Well, the answer is

96022049

As wrote the program

enter image description here

A J
  • 3,974
  • 5
  • 29
  • 58
  • 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 05 '16 at 12:30
  • 1
    @lois6b This is what surprised me. I didn't expect this. – A J Sep 05 '16 at 12:31
  • @AyandaAgustoZwane I must say there are better answers than mine. – A J Sep 05 '16 at 12:31
  • Why the screenshot and not the code itself? – Marius Sep 05 '16 at 12:33
  • It's OPs decision. I'm fine with it, in the worst case, I get my Populist badge... – elias Sep 05 '16 at 12:34
  • @Marius Because there were problem while putting them into spoiler tag. And this took time which is why IAmInPLS beat me. – A J Sep 05 '16 at 12:35
  • 1
    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 05 '16 at 12:38
  • If you think this is worth a discussion, bring it to Meta. Comments are definitely not the place for this conversation. – elias Sep 05 '16 at 12:45
  • I don't think it's right either that my answer is the accepted one: http://puzzling.stackexchange.com/questions/42029/how-many-sevens#comment124925_42029 – Marius Sep 05 '16 at 12:53
4

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

4

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&lt;= 123456789 ; i++)
  {
      if (i.ToString().Contains("7"))
      {
          num += i.ToString().Count(f => f == '7');
      }
  }

} }

rhsquared
  • 9,220
  • 3
  • 31
  • 52
3

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</p>

Solution:

There are 96'022'049 sevens

Puck
  • 383
  • 2
  • 9
  • multiply it by 7 and wonder! – elias Sep 05 '16 at 11:54
  • @elias but... why? – Puck Sep 05 '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 05 '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 05 '16 at 20:52
  • @PeregrineRook Aha!... Edited, now I get the right answer. – Puck Sep 06 '16 at 07:49
2

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.

Numberknot
  • 3,042
  • 2
  • 21
  • 33
1

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)

0

Once.

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

Rand al'Thor
  • 116,845
  • 28
  • 322
  • 627
Mithical
  • 8,860
  • 4
  • 36
  • 88