Project Euler — Problem 97 Solution


The first known prime found to exceed one mil­lion dig­its was dis­cov­ered in 1999, and is a Mersenne prime of the form 26972593-1; it con­tains exact­ly 2,098,960 dig­its. Sub­se­quent­ly oth­er Mersenne primes, of the form 2p-1, have been found which con­tain more dig­its.

How­ev­er, in 2004 there was found a mas­sive non-Mersenne prime which con­tains 2,357,207 dig­its: 28433x27830457+1.

Find the last ten dig­its of this prime num­ber.


// define a func­tion which returns the last n dig­its of a num­ber
let get­Last­Dig­it­sOf n num­ber =
let num­ber­Str = number.ToString()
let dig­its =
if numberStr.Length > n then numberStr.Substring(numberStr.Length — n, n)
else numberStr.Substring(0, numberStr.Length)

// define a func­tion which iter­a­tive­ly pow­ers up the base (b) but all the time
// only keep­ing track of the last n dig­its of the result
let F b pow n =
let muta­ble track­er = 1L
for i = 1 to pow do track­er