First 9 prime Fibonacci number: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Raku}}: slightly nicer layout) |
(Added XPL0 example.) |
||
Line 61: | Line 61: | ||
2 3 5 13 89 233 1597 28657 514229 |
2 3 5 13 89 233 1597 28657 514229 |
||
done... |
done... |
||
</pre> |
|||
=={{header|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> |
|||
{{out}} |
|||
<pre> |
|||
2 3 5 13 89 233 1597 28657 514229 |
|||
</pre> |
</pre> |
Revision as of 01:46, 20 January 2022
First 9 prime Fibonacci number is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
Show on this page the first 9 Fibonacci number.
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 "stdlib.ring" see "working..." + nl num = 0
see "The firts 9 Prime Fibonacci numbers: " + nl for n = 1 to 1000000
x = fib(n) if isprime(x) num++ if num< 10 see "" + 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