First 9 prime Fibonacci number: Difference between revisions
Line 51: | Line 51: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<lang ring> |
<lang ring> |
||
load " |
load "stdlibcore.ring" |
||
see "working..." + nl |
see "working..." + nl |
||
num = 0 |
num = 0 |
||
see "The |
see "The first 9 Prime Fibonacci numbers: " + nl |
||
for n = 1 to 1000000 |
for n = 1 to 1000000 |
||
x = fib(n) |
x = fib(n) |
||
Line 61: | Line 61: | ||
num++ |
num++ |
||
if num< 10 |
if num< 10 |
||
? "" + x + " " |
|||
else |
else |
||
exit |
exit |
Revision as of 04:19, 20 January 2022
- Task
Show on this page the first 9 Fibonacci number.
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
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... Prime Fibonacci numbers: 2 3 5 13 89 233 1597 28657 514229 done...
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