Skip to content

Instantly share code, notes, and snippets.

@cbadke
Created November 23, 2012 00:35
Show Gist options
  • Save cbadke/4133458 to your computer and use it in GitHub Desktop.
Save cbadke/4133458 to your computer and use it in GitHub Desktop.
Fetch Primes Attempt 1
module PrimeFetcherFSharp
let FetchPrimesUpTo (max : int) : int[] =
match max with
| x when x < 2 -> [||]
| x when x = 2 -> [|2|]
| x when x < 5 -> [|2; 3|]
| _ ->
let candidates = [|for x in 0 .. max -> if x < 2 then 0 else x|]
let RemoveMultiples (coll : int[]) (x : int) =
Array.iteri (fun index num ->
if (num % x) = 0 && num <> x then
coll.[index] <- 0) coll
coll
let input = Array.concat [ [|2|]; [| 3 .. 2 .. max/2 |] ]
Array.fold RemoveMultiples candidates input
|> Array.filter (fun num -> num <> 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment