Primal Pi
Posted: Thu Feb 12, 2009 2:43 pm
Hi folks,
I'm very curious about your way of solving this challenge, since all I could think of was just brute-forcing witht the fast Miller-Rabin prime test. I was too lazy to implement the algorithm myself in any language, so I just took Java and used BigInteger, who has a isProbablePrime() method:
So, my only optimization here is to check for the last digit, and skip the test if it's 0/2/4/5/6/8 because numbers ending with one of these digits are never primes (at least numbers > 9).
The other idea I had was searching in a prime database for all primes between let's say 500 and 2048 digits, but unfortunately the only site I found on the net had a strange format and you couldn't see the full prime numbers itself but only the way they would be calculated.
See http://primes.utm.edu/primes/search.php
So, is there any other way than brute-forcing? Or any other optimization I've not thought of?
I'm very curious about your way of solving this challenge, since all I could think of was just brute-forcing witht the fast Miller-Rabin prime test. I was too lazy to implement the algorithm myself in any language, so I just took Java and used BigInteger, who has a isProbablePrime() method:
Code: Select all
import java.Math.BigInteger;
public class PrimalPi {
private static final String sequence = "14159 [.................]"
public static void main(String[] args)
{
for (int i=0; i<sequence.length(); i++) {
System.out.println("Testing with startindex " + i);
for (int j=sequence.length(); j>i; j--) {
String totest_str = sequence.substring(i, j);
char lastchar = totest_str.charAt(totest_str.length()-1);
if (lastchar == '0' ||
lastchar == '2' ||
lastchar == '4' ||
lastchar == '5' ||
lastchar == '6' ||
lastchar == '8')
continue;
BigInteger totest = new BigInteger(totest_str);
if (totest.isProbablePrime(4)) {
System.out.println("Probable prime: " + totest_str);
}
}
}
}
}
The other idea I had was searching in a prime database for all primes between let's say 500 and 2048 digits, but unfortunately the only site I found on the net had a strange format and you couldn't see the full prime numbers itself but only the way they would be calculated.
See http://primes.utm.edu/primes/search.php
So, is there any other way than brute-forcing? Or any other optimization I've not thought of?