Happy Pi Day!

In America, today's date is written as 3.14.15 3.14.15 , and at 9 : 26 : 53 9:26:53 10 digits of π \pi will appear on watches for the first time in 100 years. Now, in what position in π \pi will the sequence 1 , 2 , 3 , 4 , 5 1,2,3,4,5 commence for the first time?

Note: The sequence 3 , 1 , 4 , 1 , 5 3,1,4,1,5 commences at position 1 1 , 1 , 4 , 1 , 5 , 9 1,4,1,5,9 commences at position 2 2 , etc.


The answer is 49703.

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.

6 solutions

Tarun Singh
Mar 15, 2015

The EASIEST solution
Get digits here - http://www.eveandersson.com/pi/digits/1000000
The inbuilt method "index()" can be used here -

1
2
3
j=open('pi.txt')  #reading digits from a text file
text=j.read() 
print 'Answer : ', text.index('12345')  

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.

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
# downloaded first million
# digits of pi to pi.txt and
# deleted the "."
pi = open("pi.txt").read()
index = 0
while pi[index:index+5] != '12345':
    index += 1
print "Answer:", index + 1

Brock Brown - 6 years, 2 months ago

Log in to reply

thanks for teaching me one more efficient way of searching a text -

pi[index:index+5] != '12345':

Tarun Singh - 6 years, 2 months ago

This is basically how I did it, except I used the first billion digits of pi :D

David Holcer - 6 years, 2 months ago
Otavio Monteagudo
Mar 15, 2015

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

Vishnu Bhagyanath - 5 years, 11 months ago

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!

Ryan Tamburrino - 6 years, 3 months ago

Log in to reply

Wait ... You didn't solve this question ?

A Former Brilliant Member - 6 years, 3 months ago

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!

Ryan Tamburrino - 6 years, 3 months ago

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 !

A Former Brilliant Member - 6 years, 3 months ago

Log in to reply

@A Former Brilliant Member Being lazy is awesome. Procrastinators, unite! ... tomorrow.

Ryan Tamburrino - 6 years, 3 months ago

this is incredible.

Vighnesh Raut - 6 years, 3 months ago
Aryan Gaikwad
Mar 18, 2015

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)

Sim Leek
May 19, 2015

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.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...