First 9 prime Fibonacci number: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Fix Python solution to match output) |
(→OCaml: add) |
||
Line 1,081: | Line 1,081: | ||
25 F(9311) = 34232 … 76289 (1946 digits) |
25 F(9311) = 34232 … 76289 (1946 digits) |
||
26 F(9677) = 10565 … 70357 (2023 digits)</pre> |
26 F(9677) = 10565 … 70357 (2023 digits)</pre> |
||
=={{header|OCaml}}== |
|||
<syntaxhighlight lang="ocaml">let is_prime n = |
|||
let rec test x = |
|||
x * x > n || n mod x <> 0 && n mod (x + 2) <> 0 && test (x + 6) |
|||
in |
|||
if n < 5 |
|||
then n land 2 <> 0 |
|||
else n land 1 <> 0 && n mod 3 <> 0 && test 5 |
|||
let seq_fibonacci = |
|||
let rec next b a () = Seq.Cons (a, next (b + a) b) in |
|||
next 1 0 |
|||
let () = |
|||
seq_fibonacci |> Seq.filter is_prime |> Seq.take 9 |
|||
|> Seq.iter (Printf.printf " %u") |> print_newline</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 2 3 5 13 89 233 1597 28657 514229</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |