Generate prime numbers using an online Sieve of Eratosthenes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let primes = | |
let a = {2} in | |
let grow() = | |
let p0 = Array.get a (Array.length a - 1) + 1 in | |
let b = Array.init p0 [_ → True] in | |
let () = | |
Array.fold [(), di → | |
let rec loop i = | |
if i >= Array.length b then () else | |
let () = Array.Unsafe.set b i False in | |
loop(i+di) in | |
let i0 = floor(p0 / di) * di in | |
loop(if i0<p0 then i0+di-p0 else i0-p0) ] () a in | |
let f ((a, i), b) = | |
(if b then Array.Unsafe.append a (p0+i) else a), i+1 in | |
let _ = Array.fold f (a, 0) b in | |
() in | |
let rec loop n = | |
if n < Array.length a then Array.get a n else | |
let () = grow() in | |
loop n in | |
loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment