First 9 prime Fibonacci number
Show on this page the first 9 prime Fibonacci number.
- Task
C
Requires C99 or later. <lang c>#include <stdio.h>
- include <stdint.h>
- include <stdbool.h>
bool isPrime(uint64_t n) {
if (n < 2) return false; if (!(n%2)) return n == 2; if (!(n%3)) return n == 3; uint64_t d = 5; while (d*d <= n) { if (!(n%d)) return false; d += 2; if (!(n%d)) return false; d += 4; } return true;
}
int main() {
uint64_t f1 = 1, f2 = 1, f3; int count = 0, limit = 12; // as far as we can get without using GMP printf("The first %d prime Fibonacci numbers are:\n", limit); while (count < limit) { f3 = f1 + f2; if (isPrime(f3)) { printf("%ld ", f3); count++; } f1 = f2; f2 = f3; } printf("\n"); return 0;
}</lang>
- Output:
The first 12 prime Fibonacci numbers are: 2 3 5 13 89 233 1597 28657 514229 433494437 2971215073 99194853094755497
Perl
<lang perl>#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/First_9_Prime_Fibonacci_Number use warnings; use ntheory qw( is_prime );
my @first; my $x = my $y = 1; while( @first < 9 )
{ ($x, $y) = ($x + $y, $x); is_prime( $x ) and push @first, $x; }
print "@first\n";</lang>
- Output:
2 3 5 13 89 233 1597 28657 514229
Raku
<lang perl6>put ++$ .fmt("%2d: ") ~ $_ for (0, 1, * + * … *).grep( &is-prime )[^20];</lang>
- Output:
1: 2 2: 3 3: 5 4: 13 5: 89 6: 233 7: 1597 8: 28657 9: 514229 10: 433494437 11: 2971215073 12: 99194853094755497 13: 1066340417491710595814572169 14: 19134702400093278081449423917 15: 475420437734698220747368027166749382927701417016557193662268716376935476241 16: 529892711006095621792039556787784670197112759029534506620905162834769955134424689676262369 17: 1387277127804783827114186103186246392258450358171783690079918032136025225954602593712568353 18: 3061719992484545030554313848083717208111285432353738497131674799321571238149015933442805665949 19: 10597999265301490732599643671505003412515860435409421932560009680142974347195483140293254396195769876129909 20: 36684474316080978061473613646275630451100586901195229815270242868417768061193560857904335017879540515228143777781065869
Python
<lang python> print("working...") print("The firsr 9 Prime Fibonacci numbers:")
num = 0
def isprime(m):
for i in range(2,int(m**0.5)+1): if m%i==0: return False return True
def fib(nr): if (nr == 0): return 0 if (nr == 1): return 1 if (nr > 1): return fib(nr-1) + fib(nr-2)
for n in range(2,520000): x = fib(n) if isprime(x): num = num + 1 if (x > 1): if (num < 11): print(str(x),end=" ") else: break
print() print("done...") </lang>
- Output:
working... The firsr 9 Prime Fibonacci numbers: 2 3 5 13 89 233 1597 28657 514229 done...
Ring
<lang ring> load "stdlibcore.ring" see "working..." + nl num = 0
see "The first 9 Prime Fibonacci numbers: " + nl for n = 1 to 1000000
x = fib(n) if isprime(x) num++ if num< 10 ? "" + x + " " else exit ok ok
next
see "done..." + nl
func fib nr
if nr = 0 return 0 ok if nr = 1 return 1 ok if nr > 1 return fib(nr-1) + fib(nr-2) ok
</lang>
- Output:
working... The first 9 Prime Fibonacci numbers: 2 3 5 13 89 233 1597 28657 514229 done...
Wren
<lang ecmascript>import "./math" for Int
var limit = 11 // as far as we can go without using BigInt System.print("The first %(limit) prime Fibonacci numbers are:") var count = 0 var f1 = 1 var f2 = 1 while (count < limit) {
var f3 = f1 + f2 if (Int.isPrime(f3)) { System.write("%(f3) ") count = count + 1 } f1 = f2 f2 = f3
} System.print()</lang>
- Output:
The first 11 prime Fibonacci numbers are: 2 3 5 13 89 233 1597 28657 514229 433494437 2971215073
XPL0
<lang XPL0>func IsPrime(N); \Return 'true' if N is prime int N, I; [if N <= 2 then return N = 2; if (N&1) = 0 then return false; for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false; I:= I+1; ];
return true; ];
int F, N, N0, C; [C:= 0; N:= 1; N0:= 1; loop [F:= N + N0;
if IsPrime(F) then [IntOut(0, F); ChOut(0, ^ ); C:= C+1; if C >= 9 then quit; ]; N0:= N; N:= F; ];
]</lang>
- Output:
2 3 5 13 89 233 1597 28657 514229