32 lines
786 B
OCaml
32 lines
786 B
OCaml
(* 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);
|