Pi,Pi,Pi!

Find the first 20 20 digit prime in consecutive digits of π \pi .

This problem was inspired by Agnishom's problem: Google,Euler's Number and the Hidden Prime .


The answer is 89793238462643383279.

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.

1 solution

Ah, Nice!

Some Mathematica Code:

FromDigits[#] & /@ 
  Partition[RealDigits[Pi - 3, 10, 1000] // First, 20, 1];

Select[%, PrimeQ] // First

This solution was inspired by Agnishom's solution of the other problem :P

Because the reader has already solved this problem, the linked problem should not be anymore troublesome to him. It is suggested that he reads the explanation of the code there.

Here is some sage code:

digits=str(n(pi,digits=10000)).split('.')[1]

for i in xrange(10000):
    x = int(digits[i:i+20])
    if x in Primes():
        print x
        break

Sage is a powerful free software alternate. You can try Sage at http://sagenb.org online without having to download anything. Sage is built upon Python.

If you are interested in writing it all by yourself, this can help:

I couldnt understand the question..can someone please explain the question

Naveen Verma - 6 years, 12 months ago

@Thaddeus Abiy , I've done something more than just Mathematica Shortcuts this time :)

Log in to reply

Haha. No its cool.the Mathematica code seems more elegant anyways. I am not a hardcore conservative,in fact python coders are the most envied when it comes to lazy code. But there is simply no language like mathematica when it comes math. Just look at the project euler forums,50 lines of C code done in 3 lines of mathematica. The C coders are usually not very happy..

Thaddeus Abiy - 7 years ago

Log in to reply

C coders are not happy at all. -_-

Akshay Arora - 7 years ago

I agree but then Sage is nice too.

Proprietary software is itself an injustice.

-Richard Matthew Stallman

What was the number BTW? I tried finding in the digits of pi but the number was so far away that I got an error due to limit capacity of substring method. Here is my code in java -

import java.io.*;
import java.util.*;

class test {
public static void main(String[] args) throws IOException {
    String e;

    try(BufferedReader br = new BufferedReader(new FileReader("pi.txt"))) {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }

        e = sb.toString();
    }

    for(int i = 0;; i++){
        double d = Double.parseDouble(e.substring(i, i + 20));
        if(prime(d)){
            System.out.println(d);
            return;
        }
    }
}

static boolean prime(double n) {
    if(n==2) return true;
    if (n%2==0) return false;
    for(int i=3;i*i<=n;i+=2) 
    if(n%i==0) return false;
    return true;
}
}

Aryan Gaikwad - 6 years, 3 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...