primes/primes.ml

32 lines
786 B
OCaml
Raw Normal View History

2011-03-08 07:01:27 +00:00
(* OCaml version of a prime number finder
haplo@mindstab.net *)
let primes max =
let sq = ref 2 in
let sqm = ref 4 in
let cnt = ref 3 in
let m = ref 0 in
while !cnt <= !max do
let i = ref 3 in
let stop = ref false in
m := 1;
while !i <= !sq && not !stop do
m := (!cnt mod !i);
if !m = 0 then
stop := true;
i := !i + 2
done;
if not (!m = 0) then
(print_int(!cnt);
print_newline());
if !cnt >= !sqm then
(sq := !sq+1; sqm := !sq * !sq);
cnt := !cnt + 2
done;;
let max = ref (int_of_string Sys.argv.(1)) in
if !max < 1 then (print_string("Invalid Max Num\n"); exit(1););
primes(max);