Triple Digit Hunting

1234567891011121314 9989991000 \large 1234567891011121314\ldots 9989991000

The number above shows a concatenating of the first 1000 positive integers in ascending order. How many 3-digit substrings consist of the same digit?

Bonus : Generalize for 1234567891011121314 1 0 n . 1234567891011121314\ldots 10^n.


The answer is 36.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Andrew Ellinor
Mar 3, 2016

Sometimes, I can Python

biglist = [n for n in range(1001)]
bigstring = ' '
count = 0

for k in biglist:
   bigstring = bigstring + str(k)

for i in range(len(bigstring) - 2): 
    if bigstring[i] == bigstring[i + 1] == bigstring[i + 2]:
    count = count + 1

print count

For my generalization, I found that the number of 3-digit substrings consisting of the same digit is n 1 0 n 1 + 800 ( 1 0 n 3 1 9 ) + 94 + n n\cdot10^{n-1} + 800\left(\frac{10^{n - 3} - 1}{9}\right) + 94 + n ... I think.

Equivalently, here's a two-liner version of your code.

1
2
3
>>> k=''.join([str(i) for i in range(1,1001)])
>>> print(sum(k[i]==k[i+1]==k[i+2] for i in range(len(k)-2)))
36

Your generalization, although has a typo (fix: replace all n n by n 1 n-1 ), is quite interesting.

Prasun Biswas - 5 years, 3 months ago

Log in to reply

A two liner. :O

Just beautiful.

Andrew Ellinor - 5 years, 3 months ago

Tricky question! I used the sub-string '000', '111', '222' etc. to count the matches. What it does is, it counts the sub-string '0000' as 1 instead of 2. So I ended up at 20 instead of 36. I was about to post a report, when I just looked at your code and here I am liking your answer.

Siva Bathula - 5 years, 3 months ago

Sometimes , you can snake :3

Nihar Mahajan - 5 years, 3 months ago
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
a=""
b=1
while b<=1000:
    a+=str(b)
    b+=1
n=0
c=0
while n<len(a)-2:
    if a[n]==a[n+1] and a[n]==a[n+2]:
        c+=1
    n+=1
print c

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...