Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 40s
Scaled-down PE7 (real version asks for the 10001st prime = 104743).
Trial-division within an outer while loop searching forward from 2,
short-circuited via bool ref:
let nth_prime n =
let count = ref 0 in
let i = ref 1 in
let result = ref 0 in
while !count < n do
i := !i + 1;
let p = ref true in
let j = ref 2 in
while !j * !j <= !i && !p do
if !i mod !j = 0 then p := false;
j := !j + 1
done;
if !p then begin
count := !count + 1;
if !count = n then result := !i
end
done;
!result
nth_prime 100 = 541
100 keeps the run under our 3-minute budget while exercising the
same algorithm.
112 baseline programs total.
23 lines
400 B
OCaml
23 lines
400 B
OCaml
let nth_prime n =
|
|
let count = ref 0 in
|
|
let i = ref 1 in
|
|
let result = ref 0 in
|
|
while !count < n do
|
|
i := !i + 1;
|
|
let p = ref true in
|
|
let j = ref 2 in
|
|
while !j * !j <= !i && !p do
|
|
if !i mod !j = 0 then p := false;
|
|
j := !j + 1
|
|
done;
|
|
if !p then begin
|
|
count := !count + 1;
|
|
if !count = n then result := !i
|
|
end
|
|
done;
|
|
!result
|
|
|
|
;;
|
|
|
|
nth_prime 100
|