0 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 …
The above is the beginning of all non-negative integers concatenated into a single infinite string.
1 1 1 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 of 1 1 1 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.
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.
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
Log in to reply
What about 111 followed by 112 - this as four, and five digits consecutively?
Log in to reply
The problem clearly states that five in a row does not count as four in a row.
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.
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.
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.
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.
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.
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.
The 1st such string of 4 consecutives at least is 110111112
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.
Yeah what about 110 111 112
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.
Here is the catch - "with different digits preceding and following the run of the identical digits."
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?
Why is it not 110 111 112?
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…
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 |
|
1 2 3 4 5 6 |
|
What about 111,112? after all it asks for 4 of the same digits not 4 of the same digits alone
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
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.
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
Novice with Python... and not getting this code haha... sad...
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?
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
code to count the digits
Length@Flatten[IntegerDigits/@Range[0,8998]]
this returns 3 4 8 8 6
the next number is
8
9
9
9
so we add
2
(position where the sequence begins)
and the answer is
3
4
8
8
8
Nice ! I guess we can count the digits as follows as well : N = 1 × 1 0 + 2 × 9 0 + 3 × 9 0 0 + 4 × ( 9 0 0 0 − 9 9 9 ) − 1 = 3 4 8 8 8
wait what about 110111112
the question didn't ask for the first string with exactly 4 identical digits, so wouldn't this count??
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.
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
Log in to reply
The question asked for exactly 4 identical digits... but 110,111,112 has 5 identical digits.
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.
Log in to reply
Yes poorly worded question, should have said 'exactly'
"different digits preceding and following the run of the identical digits"
For two digit numbers or Between 1 to 100 we can have ⋯ 1 0 1 1 1 2 1 ⋯ 2 1 2 2 2 3 ⋯ 3 2 3 3 3 4 ⋯ 4 3 4 4 4 5 ⋯ 5 4 5 5 5 6 ⋯ 8 7 8 8 8 9 9 0 ⋯ 9 8 9 9 . We notice that as the identical digits a a a starts to appear the preceding digits is a − 1 and following digits is a + 1 . With this notice we can be sure that there is no possibilities to appear such nth sub-string except 8 9 8 c 8 9 9 c 9 0 0 for greater than 3 digits numbers and 1 0 1 0 c 1 1 1 c 1 1 2 . If a b c is three digit then on concatenation we find ⋯ ( c − 1 ) c a b c c a b ( c + 1 ) ⋯ . If c + 1 = 0 then only we have chance to meet such number for which c corresponds to 9 .
On the same manner, there is no possibilities to appear other sub-string except 8 9 9 8 c 8 9 9 9 c 9 0 0 0 . Therefore , the first required sub-string is 0 1 2 3 4 5 6 7 8 9 1 0 ⋯ ⋯ ⋯ 8 9 9 8 8 9 9 9 9 0 0 0 Now to determine the position of sub-string we can use the fact from 0 to 9 digits = 9 × 1 + 1 = 1 0 from 10 to 99 digits = 8 9 × 2 + 2 = 1 8 0 from 100 to 999 digits = 8 9 9 × 3 + 3 = 2 7 0 0 from 1000 to 8999 digits = 7 9 9 9 × 4 + 4 = 3 2 0 0 0 or total digits = ( 8 9 9 9 × 4 + 4 ) − ( 1 0 0 0 × 4 ) = 3 2 0 0 0 Totalling we find 3 4 8 9 0 . Hence deducing − 2 (since we jump to last 9 of 8 9 9 9 ) we find 3 2 8 9 0 − 2 = 3 4 8 8 8 which is the required position of sub-string.
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 |
|
3 4 8 8 8 is the first one !
C++ code to do this:
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
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 .
1 2 3 4 5 |
|
1 2 |
|
Problem Loading...
Note Loading...
Set Loading...
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 -digit numbers to ( k + 1 ) -digit numbers: 9 8 , 9 9 , 1 0 0 has a sub-string of two 9 's; 9 9 8 , 9 9 9 , 1 0 0 0 has a sub-string of three 9 's; and 9 9 9 8 , 9 9 9 9 , 1 0 0 0 0 has a sub-string of four 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 -digit numbers, where k = 2 , 3 or 4 .
We check each case separately. (In the following, x is the repeating digit while a and b are any digits not equal to x .)
Two-digit numbers:
∙ _ a , x x , x x , b _ ∙ a x , x x , x b Two of the same number; clearly not possible The ones digit of the first and second numbers are the same; not possible
Three-digit numbers:
∙ _ a x , x x x , b _ _ ∙ a x x , x x b ∙ _ _ a , x x x , x b _ The ones digit of the two numbers are the same; not possible The hundreds digit changes but the tens digit does not; not possible 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
Four-digit numbers:
∙ _ _ _ a , x x x x , b _ _ _ ∙ _ _ a x , x x x b ∙ _ a x x , x x b ∙ a x x x , x b _ _ The thousands digit of the second number changes, so x=9 and xxxx=9999, already mentioned above The tens digit changes, so x=9, then a=8 and b=0; possible , first example is 9989,9990 The tens digit changes, so x=9, then a=8 and b=0; possible , first example is 9899,9900 The hundreds digit is changing, so x=9, then a=8 and b=0; possible , first example is 8999,9000
Of the four possible cases, the earliest is 8 9 9 9 , 9 0 0 0 . We need to find the location of the second digit of 8 9 9 9 .
∙ 0 − 9 ∙ 1 0 − 9 9 ∙ 1 0 0 − 9 9 9 ∙ 1 0 0 0 − 8 9 9 9 The first ten numbers are single-digit, for a total of 10 digits. The next ninety numbers are two-digit, for a total of 180 digits. The next nine hundred numbers are three-digit, for a total of 2700 digits. The next eight thousand numbers are four-digit, for a total of 32000 digits.
Thus after 1 0 + 1 8 0 + 2 7 0 0 + 3 2 0 0 0 = 3 4 8 9 0 digits we've reached the last digit of 8 9 9 9 , so the position of the second digit of 8 9 9 9 is 3 4 8 9 0 − 2 = 3 4 8 8 8