Sum of primes in odd positions is prime: Difference between revisions

m (→‎{{header|J}}: bugfix for generalization: constraint is primes less than 1000, not primes up through 1000)
(→‎OCaml: add)
Line 479:
119 653 17959
143 823 26879</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let is_prime n =
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 () = Seq.ints 3
|> Seq.filter is_prime
|> Seq.take_while ((>) 1000)
|> Seq.zip (Seq.ints 2)
|> Seq.filter (fun (i, _) -> i land 1 = 1)
|> Seq.scan (fun (_, pi, sum) (i, p) -> i, p, sum + p) (1, 2, 2)
|> Seq.filter (fun (_, _, sum) -> is_prime sum)
|> Seq.iter (fun (i, pi, sum) -> Printf.printf "p(%u) = %u, %u\n" i pi sum)</syntaxhighlight>
{{out}}
<pre>
p(1) = 2, 2
p(3) = 5, 7
p(11) = 31, 89
p(27) = 103, 659
p(35) = 149, 1181
p(67) = 331, 5021
p(91) = 467, 9923
p(95) = 499, 10909
p(99) = 523, 11941
p(119) = 653, 17959
p(143) = 823, 26879
</pre>
 
=={{header|PARI-GP}}==
559

edits