I am almost embarrassed to make a post about problem 97 of Project Euler which reads
The first known prime found to exceed one million digits was discovered in 1999, and is a Mersenne prime of the form 26972593
1; it contains exactly 2,098,960 digits. Subsequently other Mersenne primes, of the form 2p
1, have been found which contain more digits.
However, in 2004 there was found a massive non-Mersenne prime which contains 2,357,207 digits: 28433
27830457+1.
Find the last ten digits of this prime number.
I decided to use the BigInteger class for solving this, so I didn’t have to care about numerical issues. The only other insight that was needed was to use the ModPow method which takes the modulo along with the power instead of doing those seperatly.
When doing that the code looks like
long mod = 10000000000; BigInteger result = 28433 * BigInteger.ModPow(2, 7830457, mod) +1; result %= mod;
It could have been done in a oneliner, but then I couldn’t fit it onto the website without scrolling.
This runs in
The last 10 digits are 8739992577 Solution took 0,062 ms
What took the longest was the fact that I forgot to add one to the end of the result.
You can find the source file here as usual.




Written by Kristian on 19 May 2012
Topics: Project Euler