Jump to content

Wagstaff primes: Difference between revisions

(Upgraded to 'full' task.)
(→‎OCaml: add)
Line 304:
23 3539 73796098201..86486497963 1065 digits
24 5807 40184962373..86663568043 1748 digits
</pre>
 
=={{header|OCaml}}==
Using the function <code>seq_primes</code> from [[Extensible prime generator#OCaml]]:
<syntaxhighlight lang="ocaml">let is_prime n =
let not_divisible x = n mod x <> 0 in
seq_primes |> Seq.take_while (fun x -> x * x <= n) |> Seq.for_all not_divisible
 
let wagstaff n =
let w = succ (1 lsl n) / 3 in
if n <> 2 && is_prime w then Some (n, w) else None
 
let () =
let show (p, w) = Printf.printf "%u -> %u%!\n" p w in
seq_primes |> Seq.filter_map wagstaff |> Seq.take 11 |> Seq.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}}
<pre>
3 -> 3
5 -> 11
7 -> 43
11 -> 683
13 -> 2731
17 -> 43691
19 -> 174763
23 -> 2796203
31 -> 715827883
43 -> 2932031007403
61 -> 768614336404564651
</pre>
 
559

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.