3 . 1 4 . 1 5 , and at 9 : 2 6 : 5 3 10 digits of π will appear on watches for the first time in 100 years. Now, in what position in π will the sequence 1 , 2 , 3 , 4 , 5 commence for the first time?
In America, today's date is written asNote: The sequence 3 , 1 , 4 , 1 , 5 commences at position 1 , 1 , 4 , 1 , 5 , 9 commences at position 2 , etc.
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.
I did it the slightly less efficient way; I wasn't aware that the String class had an index function.
+1 for teaching me something new about Python.
Python 2.7:
1 2 3 4 5 6 7 8 |
|
Log in to reply
thanks for teaching me one more efficient way of searching a text -
pi[index:index+5] != '12345':
This is basically how I did it, except I used the first billion digits of pi :D
This exercise's answer is wrong according with its own terms. The answer is set as '49703', which would be correct in 'computer counting', as for example in an array where the first element is indexed as '0', not '1'. Since the first element in the actual digits of pi is indexed as '1' in the exercise's note, the correct answer following the guidelines should be '49704'.
By the way, below is my solution, written in Python:
pi = "3.1415(...)" # first 100,000 digits, source: http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html
for i in range(0,len(pi)):
if (pi[i] == "1"):
if (pi[i+1] == "2"):
if (pi[i+2] == "3"):
if (pi[i+3] == "4"):
if (pi[i+4] == "5"):
print (i + 1)
break
Your algorithm also counts the decimal point, that is why your result came as 49704
I don't know if there's a Mathematical solution for it , seeing as the question is tagged under NT .
Well , let me make it very clear that I cheated ! I visited here and typed 12345 to get the answer .
Also some cool sites for you guys!
Also I asked this question in DSE thinking perhaps someone from there might be able to provide a better answer ,any way I have provided the link , see for yourselves !
This should be under computer science, I believe!
Log in to reply
Wait ... You didn't solve this question ?
Log in to reply
Nah, I was too lazy. But I don't see how there could be any other solution besides a CS one. If there is one, I'd love to see it!
Log in to reply
@Ryan Tamburrino – You could use Java for instance and using the instr function you could get it . Now I am too lazy to type it out right now !
Log in to reply
@A Former Brilliant Member – Being lazy is awesome. Procrastinators, unite! ... tomorrow.
this is incredible.
Java solution -
String pi = read(); //pi contains 1 million pi digits
for(int i = 0;; i++)
if(pi.substring(i, i + 5).equals("12345"))
System.out.println(i + 1);
Execution time - 0.006526417 seconds (excluding time taken to read the pi file)
I just opened up a python console and copied pi from here once I scrolled down enough that ctrl-f returned something for '12345'.
>>>pi_str="3.14159..."
>>>pi_str.find("12345")
49703
And the decimal fixes the problem with indices starting at 0.
Problem Loading...
Note Loading...
Set Loading...
The EASIEST solution
Get digits here - http://www.eveandersson.com/pi/digits/1000000
The inbuilt method "index()" can be used here -
Because the decimal is also included in the digits in text file, so it will not affect the answer otherwise we would add 1 to answer.