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 10 001st prime number?
In [9]:
let compositesInRange fromNum toNum =
[|2 .. toNum/fromNum|]
|> Array.map (fun x -> fromNum * x)
let primesTo n =
// start off assuming every number from 0 .. n is prime
let isPrime = Array.init (1+n) (fun _ -> true)
// simple sieve of eratosthenes - update "primeness" of
// each number in the isPrime array
for x in 2 .. int(sqrt(double(n))) do
if isPrime.[x] then
compositesInRange x n
|> Array.map (fun i -> isPrime.[i] <- false)
|> ignore
// now return numbers which are "true" in isPrime array
[|2..n|]
|> Array.filter (fun x -> isPrime.[x])
// pick a big number, hopefully we have enough primes!!
let primes = primesTo 1000000
primes.[10000]
Out[9]:
In [ ]: