domenica 10 ottobre 2010

Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

solution = primes!!10000
    
primes :: Integral a => [a]
primes = 2 : iterate nextPrime 3
    where nextPrime x = head $ filter isPrime[(x+1)..] 

isPrime :: Integral a => a -> Bool
isPrime x = and $ [mod x p /= 0 | p <- takeWhile lowerThanSqrtX primes]
    where lowerThanSqrtX candidate = candidate <= (round $ sqrt $ fromIntegral x)

Nessun commento:

Posta un commento