Advertisement
Guest User

Untitled

a guest
Nov 12th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.82 KB | None | 0 0
  1. let era_sieve n =
  2.   (* set the upper bound of numbers to check *)
  3.   let upper = n * n in
  4.  
  5.   (* populate a list with numbers that we can record primeness of *)
  6.   let primes = Array.create upper true in
  7.   (* walk the numbers between 2 and the upper bound *)
  8.   for this_num = 2 to upper do
  9.       (* next, just iterate through the sieve *)
  10.       (* yes, this will hit the same numbers bunches of times, but
  11.        * this isn't about efficiency... *)
  12.       for iterator = this_num to upper do
  13.         let multiple = 1 in
  14.         let next_nonprime = iterator * iterator + multiple * iterator in
  15.           while next_nonprime < upper do
  16.             (* well, all these numbers are certainly not prime! *)
  17.             primes.( next_nonprime ) <- false ;
  18.             multiple := !multiple + 1 ;
  19.           done
  20.       done
  21.   done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement