Where are they hiding ?

012345678910 111 21314 012345678910{\color{#3D99F6}111}21314\ldots

The above is the beginning of all non-negative integers concatenated into a single infinite string.

111 \color{#3D99F6}111 is the first sub-string with 3 identical digits in a row, with different digits preceding and following the run of the identical digits. The first 1 \color{#3D99F6}1 of 111 \color{#3D99F6}111 comes at position 13 (from left) in the infinite string.

Where is the first such sub-string with 4 identical digits?

Give the position of the first digit of the four.


The answer is 34888.

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.

8 solutions

Zico Quintina
May 14, 2018

Relevant wiki: Integers - Problem Solving

The first thought that occurs is that we will have a string of consecutive digits (with different digits before and after) when switching from k k -digit numbers to ( k + 1 ) (k+1) -digit numbers: 98 , 99 , 100 98,99,100 has a sub-string of two 9 9 's; 998 , 999 , 1000 998,999,1000 has a sub-string of three 9 9 's; and 9998 , 9999 , 10000 9998,9999,10000 has a sub-string of four 9 9 's, this last one potentially being the sub-string we seek. So if there is an earlier such sub-string, it must occur among k k -digit numbers, where k = 2 , 3 k=2, 3 or 4 4 .

We check each case separately. (In the following, x x is the repeating digit while a a and b b are any digits not equal to x x .)

Two-digit numbers:

_ a , x x , x x , b _ Two of the same number; clearly not possible a x , x x , x b The ones digit of the first and second numbers are the same; not possible \begin{aligned} &\bullet \quad \_a,xx,xx,b\_ && \quad \small \text{Two of the same number; clearly } \color{#D61F06} \text{not possible} \\ &\bullet \quad ax,xx,xb && \quad \small \text{The ones digit of the first and second numbers are the same; } \color{#D61F06} \text{not possible} \end{aligned}

Three-digit numbers:

_ a x , x x x , b _ _ The ones digit of the two numbers are the same; not possible a x x , x x b The hundreds digit changes but the tens digit does not; not possible _ _ a , x x x , x b _ The tens digit changes, so the ones digit is 9, so the second number is 999; but then the third number has first digit 9, not possible \begin{aligned} &\bullet \quad \_ax,xxx,b\_\_ && \quad \small \text{The ones digit of the two numbers are the same; } \color{#D61F06} \text{not possible} \\ &\bullet \quad axx,xxb && \quad \small \text{The hundreds digit changes but the tens digit does not; } \color{#D61F06} \text{not possible} \\ &\bullet \quad \_\_a,xxx,xb\_ && \quad \small \text{The tens digit changes, so the ones digit is 9, so the second number is 999; but then the third number has first digit 9, } \color{#D61F06} \text{not possible} \end{aligned}

Four-digit numbers:

_ _ _ a , x x x x , b _ _ _ The thousands digit of the second number changes, so x=9 and xxxx=9999, already mentioned above _ _ a x , x x x b The tens digit changes, so x=9, then a=8 and b=0; possible , first example is 9989,9990 _ a x x , x x b The tens digit changes, so x=9, then a=8 and b=0; possible , first example is 9899,9900 a x x x , x b _ _ The hundreds digit is changing, so x=9, then a=8 and b=0; possible , first example is 8999,9000 \begin{aligned} &\bullet \quad \_\ \_\ \_a,xxxx,b\_\ \_\ \_ && \quad \small \text{The thousands digit of the second number changes, so x=9 and xxxx=9999, } \color{#3D99F6} \text{already mentioned above} \\ &\bullet \quad \_\ \_ax,xxxb && \quad \small \text{The tens digit changes, so x=9, then a=8 and b=0; } \color{#20A900} \text{possible} \color{#333333} \text{, first example is 9989,9990} \\ &\bullet \quad \_axx,xxb\ && \quad \small \text{The tens digit changes, so x=9, then a=8 and b=0; } \color{#20A900} \text{possible} \color{#333333} \text{, first example is 9899,9900} \\ &\bullet \quad axxx,xb\_\ \_ && \quad \small \text{The hundreds digit is changing, so x=9, then a=8 and b=0; } \color{#20A900} \text{possible} \color{#333333} \text{, first example is 8999,9000} \end{aligned}

Of the four possible cases, the earliest is 8999 , 9000 8999,9000 . We need to find the location of the second digit of 8999 8999 .

0 9 The first ten numbers are single-digit, for a total of 10 digits. 10 99 The next ninety numbers are two-digit, for a total of 180 digits. 100 999 The next nine hundred numbers are three-digit, for a total of 2700 digits. 1000 8999 The next eight thousand numbers are four-digit, for a total of 32000 digits. \begin{aligned} &\bullet \quad 0\ -\ 9 && \quad \small \text{The first ten numbers are single-digit, for a total of 10 digits.} \\ &\bullet \quad 10\ -\ 99 && \quad \small \text{The next ninety numbers are two-digit, for a total of 180 digits.} \\ &\bullet \quad 100\ -\ 999 && \quad \small \text{The next nine hundred numbers are three-digit, for a total of 2700 digits.} \\ &\bullet \quad 1000\ -\ 8999 && \quad \small \text{The next eight thousand numbers are four-digit, for a total of 32000 digits.} \end{aligned}

Thus after 10 + 180 + 2700 + 32000 = 34890 10 + 180 + 2700 + 32000 = 34890 digits we've reached the last digit of 8999 8999 , so the position of the second digit of 8999 8999 is 34890 2 = 34888 34890 - 2 = \boxed{34888}

Very nice. My reasoning was very similar, but I counted digits in a slightly different way. I didn't think it would have to go so far so I came up with formulas for the first digit for n, based its number of digits each time I had to go up a digit.

2 digit numbers, first digit is in position 2n-9

3 digit numbers, first digit is in position 3n-109

4 digit numbers, first digit is in position 4n-1109

So the 8999 begins at 4*8999-1109=34887 and its second digit is at position 34888

Jeremy Galvagni - 3 years ago

Log in to reply

What about 111 followed by 112 - this as four, and five digits consecutively?

Russell Arnott - 3 years ago

Log in to reply

The problem clearly states that five in a row does not count as four in a row.

Jeremy Galvagni - 3 years ago

Log in to reply

@Jeremy Galvagni As others have stated the question could be made much more clear throwing int the word exactly. It does state "with different digits preceding and following the run of the identical digits" but that's buried in the question.

Ben Goeke - 3 years ago

Sir, when we will reach at the hundred's line, there will occur 108109110111112. We see here that 1111 also forms a substring of subset 4, so why don't we consider it ? Thank you Sir.

Manisha Motwani - 3 years ago

Log in to reply

I think it's because there's actually a string of five 1's there, and we want exactly four digits the same.

Will Morris - 3 years ago

Log in to reply

The problem say that there must be "different digits preceding and following the run of the identical digits", therefore there can only be 4 equal numbers in a row.

Ti Co - 3 years ago

Yeah, 1111 will be followed by 1112, which means 7 of the same number in a row.

Does the sub-string "111112" not count? It has 5 numbers, though 4 are still in sequence.

James Carmody - 3 years ago

Log in to reply

This has five 1's in sequence. We are looking for exactly four digits in sequence with different digits before and after the sequence. That is, we're told to look for a sequence of the form axxxxb where neither a or b are x. Your sequence is of the form axxxxxb which is different.

Chris Maitland - 3 years ago

The 1st such string of 4 consecutives at least is 110111112

Didi Missy - 3 years ago

Log in to reply

This has five 1's in sequence. We are looking for exactly four digits in sequence with different digits before and after the sequence. That is, we're told to look for a sequence of the form axxxxb where neither a or b are x. Your sequence is of the form axxxxxb which is different.

Chris Maitland - 3 years ago

Yeah what about 110 111 112

Zenen Cardenas - 3 years ago

Log in to reply

This has five 1's in sequence. We are looking for exactly four digits in sequence with different digits before and after the sequence. That is, we're told to look for a sequence of the form axxxxb where neither a or b are x. Your sequence is of the form axxxxxb which is different.

Chris Maitland - 3 years ago

Here is the catch - "with different digits preceding and following the run of the identical digits."

V K - 3 years ago

Oh I thought the digit had to be one. Whoops. If the digit had to be one, can anyone tell me if 44400 is right?

Zain Majumder - 3 years ago

Why is it not 110 111 112?

Gautam Sood - 3 years ago

It sounds tempting to generalise: If n is even, we first find n equal digits within the numbers with k = n digits, between 89…9 and 90…0. If n is odd, we first find n equal digits within the numbers with k = ( n +1)/2 digits, between 1…11 and 1…12. So the sequence of the values n which are "served first" is: 1, 3, 2, 5, 7, 4, 9, 11, 6, 13, 15, 8, 17, 19, 10…

Giacomo Lanza - 2 years, 7 months ago
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# https://brilliant.org/weekly-problems/2018-05-14/intermediate/?p=5
def before_after(value, a):
    pos_before = value.find(a)
    pos_after = pos_before + len(a)
    if pos_before == -1 or pos_after == -1: 
        return 0, ''  
    return pos_before, value[pos_before-1:pos_before]+a+value[pos_after:pos_after+1]

def where_in_string(x):
    for i in patterns:
        a,b = before_after(x,i)  
        if len(set(b))==3:
            return a+1,b  

for str_len in range(2,8):
    patterns = []
    for i in range(10):
        j = str(i)*str_len
        patterns.append(j)
    x = ''
    if str_len%2 == 0:
        end_rng = int('1'+'0'*str_len)
    else:
        end_rng = int('1'+'0'*(str_len-1))
    for j in range(end_rng):
        x += str(j)
    print str_len, where_in_string(x)

1
2
3
4
5
6
2 (170, '8990')
3 (13, '01112')
4 (34888, '899990')  <-------------
5 (224, '0111112')
6 (5288886, '89999990')
7 (3335, '011111112')

What about 111,112? after all it asks for 4 of the same digits not 4 of the same digits alone

Mauricio Camarena - 3 years ago

Log in to reply

Maybe I read it wrong but I read ".... with different digits preceding and following the run of the identical digits..." 111112 has a run of 5 not 4

Michael Fitzgerald - 3 years ago

Log in to reply

Potentially, but on the other hand it does have a run of 4 at which point you can stop looking at the following numbers. It depends if the 5th 1 matters for the context of the question, 111, 112 was also the first answer I thought of when looking at the solution.

Keith Hitchcock - 3 years ago

Python solution:

>>> import re
>>> s = ''.join(str(x) for x in range(100000))
>>> answer = 1000000
>>> for x in range(10):
...  answer=min(re.search(f'[^{x:d}]{x:d}{x:d}{x:d}{x:d}[^{x:d}]', s).span()[0],answer) + 2
...
>>> answer
34888

One-liner:

>>> min(re.search(f'[^{x:d}]{x:d}{x:d}{x:d}{x:d}[^{x:d}]', ''.join(str(x) for x in range(100000))).span()[0]+2 for x in range(10))
34888

Ricky Teachey - 3 years ago

Novice with Python... and not getting this code haha... sad...

Jaeho Park - 3 years ago

So ...108109110111112113... which yields a string of five ones beginning at the 222nd digit of the concatenated number doesn't count as the first substring of four consecutive digits even though the 222nd, 223rd, 224th and 225th digits are all the same? Why not?

&y Edwards - 3 years ago
Giorgos K.
May 1, 2018

the substring we are searching will occur at the point ...8999,9000......
knowing this here is a M a t h e m a t i c a Mathematica code to count the digits

Length@Flatten[IntegerDigits/@Range[0,8998]]

this returns 34886 34886

the next number is 8999 8999 so we add 2 2 (position where the sequence begins)
and the answer is 34888 34888

Nice ! I guess we can count the digits as follows as well : N = 1 × 10 + 2 × 90 + 3 × 900 + 4 × ( 9000 999 ) 1 = 34888 N = 1 \times 10 + 2 \times 90 + 3 \times 900 + 4 \times (9000-999)-1 = 34888

Romain Bouchard - 3 years, 1 month ago

wait what about 110111112

the question didn't ask for the first string with exactly 4 identical digits, so wouldn't this count??

Elijah L - 3 years ago

Log in to reply

The question asked for exactly 4 identical digits... but 110,111,112 has 5 identical digits.

More accurately, it asks for 'exactly' 4 identical digits but also for 'different digits preceding and following the run of the identical digits'.

That said, I was tricked too by this poorly worded question.

Six Sandlava - 3 years ago

Yo también pensé que eran esos, también estaba dudando porque habían 5 iguales. I also thought that was that, I was also hesitating because I had 5 equals.

I think it is 110111112

Sheng Huang - 3 years ago

Log in to reply

The question asked for exactly 4 identical digits... but 110,111,112 has 5 identical digits.

Log in to reply

The question wording is really bad

Elijah L - 3 years ago

OHHH, I think the question is asking where EXACTLY four identical digits occur.... oh that makes much more sense. Yeah, I thought it was 110, 111, 112 too.

Joshua Lowrance - 3 years ago

Log in to reply

Yes poorly worded question, should have said 'exactly'

Scott Craycraft - 3 years ago

"different digits preceding and following the run of the identical digits"

Paul-Boer Putter - 3 years ago
Naren Bhandari
May 14, 2018

For two digit numbers or Between 1 to 100 we can have 10 111 21 21 222 3 32 333 4 43 444 5 54 555 6 87 888 99 0 9899. \cdots10{\color{#3D99F6}111}21 \cdots 21{\color{#3D99F6}222}3\cdots 32{\color{#3D99F6}333}4 \cdots 43{\color{#3D99F6}444}5 \cdots 54{\color{#3D99F6}555}6 \cdots 87 {\color{#3D99F6}888} {\color{#D61F06}99}0\cdots 9899. We notice that as the identical digits a a a aaa starts to appear the preceding digits is a 1 a-1 and following digits is a + 1 a+1 . With this notice we can be sure that there is no possibilities to appear such nth sub-string except 898 c 899 c 900 898 \phantom{c}899 \phantom{c}900 for greater than 3 digits numbers and 1010 c 111 c 112 1010\phantom{c}111\phantom{c}112 . If a b c abc is three digit then on concatenation we find ( c 1 ) c a b c c a b ( c + 1 ) \cdots (c-1)\phantom{c} abc \phantom{c}ab(c+1)\cdots . If c + 1 = 0 c+1=0 then only we have chance to meet such number for which c c corresponds to 9 9 .

On the same manner, there is no possibilities to appear other sub-string except 8998 c 8999 c 9000 8998\phantom{c} 8999 \phantom{c} 9000 . Therefore , the first required sub-string is 012345678910 899889999000 012345678910\cdots \cdots \cdots 8998 8999 9000 Now to determine the position of sub-string we can use the fact from 0 to 9 digits = 9 × 1 + 1 = 10 from 10 to 99 digits = 89 × 2 + 2 = 180 from 100 to 999 digits = 899 × 3 + 3 = 2700 from 1000 to 8999 digits = 7999 × 4 + 4 = 32000 or total digits = ( 8999 × 4 + 4 ) ( 1000 × 4 ) = 32000 \text{from 0 to 9 digits } =9\times 1+1=10\\ \text{from 10 to 99 digits } =89\times 2 +2 =180\\ \text{from 100 to 999 digits } = 899\times 3 +3 = 2700 \\ \text{ from 1000 to 8999 digits } = 7999\times 4 +4 = 32000\\\ \text{or total digits } = (8999\times 4+4) -(1000×4) = 32000 Totalling we find 34890 34890 . Hence deducing 2 -2 (since we jump to last 9 9 of 8999 8999 ) we find 32890 2 = 34888 32890-2=\boxed{34888} which is the required position of sub-string.

Mickaël Durand
May 15, 2018
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# I create a string of concatenated integers
bigString = ''
for i in range(10000):
    bigString += str(i)

# I print the indices of the sub-strings with only 4 identical digits
for idx, item in enumerate(bigString):
    if (bigString[idx - 1] == bigString[idx] == bigString[idx + 1] == bigString[idx + 2]  # we want 4 identicals digits
            and bigString[idx + 3] != bigString[idx]  # the next digit needs to be different
            and bigString[idx - 2] != bigString[idx]):   # the previous digit needs to be different
        print(idx)

1
2
3
34888   <===
38489
38850

34888 \boxed{34888} is the first one !

Paul-Boer Putter
May 20, 2018

C++ code to do this:

include <stdio.h>

include <cstdlib>

include <cmath>

include <iostream>

include <string>

using namespace std;

int main(int argc, char **argv) {

int len = 4;
if ( argc > 1 )
    len = atoi( argv[1] ); 
int i = 0;
char prev = -1;
string number;
int pos = 0;
int sequence = 1;

do {

    if ( number.length() == 0 ){
        number = to_string(i++);
    }

    char next = number[0];
    cout << next;
    number.erase(0, 1);
    pos++;

    if ( next == prev ){
        sequence++;
    }
    else{
        if ( sequence == len ){
            cout << endl << len << " consecutive " << prev << "'s found at position " << pos-len << endl;
            return 0;
        }
        sequence = 1;
        prev = next;
    }

} while( true );

}

4 consecutive 9's found at position 34888

心培 邱
May 18, 2018

Python solution:

# all non-negative integers concatenated into a single infinite string
str_txt = ''
for i in range(10001):
    str_txt = str_txt + str(i)

print(str_txt)

# the function of the position of the first digit of the four
def str_search(str_txt,pattern):

    M = len(str_txt)
    N = len(pattern)

    for i in range(M-N+1):

        temp = str_txt[i:i+N]

        if i>0 and i+N < M:
            temp_pre = str_txt[i-1]
            temp_suf = str_txt[i+N]
        elif i == 0:
            temp_pre = ''
            temp_suf = str_txt[i+N]
        elif i+N == M:
            temp_pre = str_txt[i-1]
            temp_suf = ''

        if temp == pattern and temp_suf != pattern[0] and temp_pre != pattern[0]:
            return i+1

print(str_search(str_txt,'0000'))
print(str_search(str_txt,'1111'))
print(str_search(str_txt,'2222'))
print(str_search(str_txt,'3333'))
print(str_search(str_txt,'4444'))
print(str_search(str_txt,'5555'))
print(str_search(str_txt,'6666'))
print(str_search(str_txt,'7777'))
print(str_search(str_txt,'8888'))
print(str_search(str_txt,'9999'))

Output :

None
None
None
None
None
None
None
None
34888
38892

So the position of the first digit of the four is 34888 .

Charlie Gunn
May 14, 2018
1
2
3
4
5
f :: String -> Int -> Int
f r s = if (all (==s!!1) $ take r $ tail s) && s!!0 /= s!!1 && s!!r /= s!!(r+1) then 2 else (+1) $ f r $ tail s

p :: x -> String
p x = (show x) ++ (p $ x+1)

1
2
Prelude> f 4 (p 0)
34888

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...