Wagstaff primes: Difference between revisions
Content added Content deleted
(→OCaml: add) |
(→OCaml: much faster without prime generator) |
||
Line 307: | Line 307: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Using the function <code>seq_primes</code> from [[Extensible prime generator#OCaml]]: |
|||
<syntaxhighlight lang="ocaml">let is_prime n = |
<syntaxhighlight lang="ocaml">let is_prime n = |
||
let |
let rec test x = |
||
let q = n / x in x > q || x * q <> n && n mod (x + 2) <> 0 && test (x + 6) |
|||
in if n < 5 then n lor 1 = 3 else n land 1 <> 0 && n mod 3 <> 0 && test 5 |
|||
let |
let is_wagstaff n = |
||
let w = succ (1 lsl n) / 3 in |
let w = succ (1 lsl n) / 3 in |
||
if n |
if is_prime n && is_prime w then Some (n, w) else None |
||
let () = |
let () = |
||
let show (p, w) = Printf.printf "%u -> %u%!\n" p w in |
let show (p, w) = Printf.printf "%u -> %u%!\n" p w in |
||
Seq.(ints 3 |> filter_map is_wagstaff |> take 11 |> iter show)</syntaxhighlight> |
|||
...checking for primality in a safe way. So, even when compiled with ''ocamlopt'', it takes more than three minutes to finish with the 11th. |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |