First 9 prime Fibonacci number: Difference between revisions
First 9 Prime Fibonacci Number in various BASIC dialents |
Added Go |
||
Line 327: | Line 327: | ||
28657 |
28657 |
||
514229 |
514229 |
||
</pre> |
|||
=={{header|Go}}== |
|||
{{trans|C}} |
|||
<lang go>package main |
|||
import "fmt" |
|||
func isPrime(n uint64) bool { |
|||
if n < 2 { |
|||
return false |
|||
} |
|||
if n%2 == 0 { |
|||
return n == 2 |
|||
} |
|||
if n%3 == 0 { |
|||
return n == 3 |
|||
} |
|||
d := uint64(5) |
|||
for d*d <= n { |
|||
if n%d == 0 { |
|||
return false |
|||
} |
|||
d += 2 |
|||
if n%d == 0 { |
|||
return false |
|||
} |
|||
d += 4 |
|||
} |
|||
return true |
|||
} |
|||
func main() { |
|||
f1 := uint64(1) |
|||
f2 := f1 |
|||
count := 0 |
|||
limit := 12 // as far as we can get without using big.Int |
|||
fmt.Printf("The first %d prime Fibonacci numbers are:\n", limit) |
|||
for count < limit { |
|||
f3 := f1 + f2 |
|||
if isPrime(f3) { |
|||
fmt.Printf("%d ", f3) |
|||
count++ |
|||
} |
|||
f1 = f2 |
|||
f2 = f3 |
|||
} |
|||
fmt.Println() |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
The first 12 prime Fibonacci numbers are: |
|||
2 3 5 13 89 233 1597 28657 514229 433494437 2971215073 99194853094755497 |
|||
</pre> |
</pre> |
||
Revision as of 18:09, 5 February 2022
- Task
Show on this page the first 9 prime Fibonacci numbers.
ALGOL 68
<lang algol68>BEGIN # show the first 9 prime fibonacci numbers #
PR read "primes.incl.a68" PR # include prime utilities # INT p count := 0; INT prev := 0; INT curr := 1; WHILE p count < 9 DO INT next = prev + curr; prev := curr; curr := next; IF is probably prime( curr ) THEN # have a prime fibonacci number # p count +:= 1; print( ( " ", whole( curr, 0 ) ) ) FI OD
END</lang>
- Output:
2 3 5 13 89 233 1597 28657 514229
AWK
<lang AWK>
- syntax: GAWK -f FIRST_9_PRIME_FIBONACCI_NUMBER.AWK
BEGIN {
f1 = f2 = 1 stop = 9 printf("First %d Prime Fibonacci numbers:\n",stop) while (count < stop) { f3 = f1 + f2 if (is_prime(f3)) { printf("%d ",f3) count++ } f1 = f2 f2 = f3 } printf("\n") exit(0)
} function is_prime(n, d) {
d = 5 if (n < 2) { return(0) } if (n % 2 == 0) { return(n == 2) } if (n % 3 == 0) { return(n == 3) } while (d*d <= n) { if (n % d == 0) { return(0) } d += 2 if (n % d == 0) { return(0) } d += 4 } return(1)
} </lang>
- Output:
First 9 Prime Fibonacci numbers: 2 3 5 13 89 233 1597 28657 514229
BASIC
BASIC256
<lang BASIC256>function isPrime(v) if v < 2 then return False if v mod 2 = 0 then return v = 2 if v mod 3 = 0 then return v = 3 d = 5 while d * d <= v if v mod d = 0 then return False else d += 2 end while return True end function
function fib(nr) if nr = 0 then return 0 if nr = 1 then return 1 if nr > 1 then return fib(nr-1) + fib(nr-2) end function
i = 0 cont = 0 print "The first 9 Prime Fibonacci numbers: " while True i += 1 num = fib(i) if isPrime(num) then cont += 1 if cont < 10 then print num; " "; else exit while end if end if end while end</lang>
- Output:
Igual que la entrada de FreeBASIC.
FreeBASIC
<lang freebasic>Function isPrime(Byval ValorEval As Integer) As Boolean
If ValorEval <= 1 Then Return False For i As Integer = 2 To Int(Sqr(ValorEval)) If ValorEval Mod i = 0 Then Return False Next i Return True
End Function
Function fib(nr As Integer) As Integer
If nr = 0 Then Return 0 If nr = 1 Then Return 1 If nr > 1 Then Return fib(nr-1) + fib(nr-2)
End Function
Dim As Integer i = 0, num, cont = 0 Print "The first 9 Prime Fibonacci numbers: " Do
i += 1 num = fib(i) If isprime(num) Then cont += 1 If cont < 10 Then Print num; " "; Else Exit Do End If End If
Loop Sleep</lang>
- Output:
The first 9 Prime Fibonacci numbers: 2 3 5 13 89 233 1597 28657 514229
PureBasic
<lang PureBasic>Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False ElseIf v < 4 : ProcedureReturn #True ElseIf v % 2 = 0 : ProcedureReturn #False ElseIf v < 9 : ProcedureReturn #True ElseIf v % 3 = 0 : ProcedureReturn #False Else Protected r = Round(Sqr(v), #PB_Round_Down) Protected f = 5 While f <= r If v % f = 0 Or v % (f + 2) = 0 ProcedureReturn #False EndIf f + 6 Wend EndIf ProcedureReturn #True
EndProcedure
Procedure fib(nr.i)
If nr = 0 : ProcedureReturn 0 ElseIf nr = 1 : ProcedureReturn 1 ElseIf nr > 1 : ProcedureReturn fib(nr-1) + fib(nr-2) EndIf
EndProcedure
If OpenConsole()
Define i.i = 0, cont.i = 0 PrintN("The first 9 Prime Fibonacci numbers: ") Repeat i + 1 num = fib(i) If isprime(num) cont + 1 If cont < 10 Print(Str(num) + " ") Else Break EndIf EndIf ForEver PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input() CloseConsole()
EndIf</lang>
- Output:
Igual que la entrada de FreeBASIC.
Yabasic
<lang yabasic>sub isPrime(v)
if v < 2 then return False : fi if mod(v, 2) = 0 then return v = 2 : fi if mod(v, 3) = 0 then return v = 3 : fi d = 5 while d * d <= v if mod(v, d) = 0 then return False else d = d + 2 : fi wend return True
end sub
sub fib(nr)
if nr = 0 then return 0 : fi if nr = 1 then return 1 : fi if nr > 1 then return fib(nr-1) + fib(nr-2) : fi
end sub
i = 0 cont = 0 print "The first 9 Prime Fibonacci numbers: " do
i = i + 1 num = fib(i) if isPrime(num) then cont = cont + 1 if cont < 10 then print num, " "; else break end if end if
loop end</lang>
- Output:
Igual que la entrada de FreeBASIC.
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
F#
<lang fsharp> // Prime Fibonacci Numbers. Nigel Galloway: January 21st., 2022 seq{yield! [2I;3I]; yield! MathNet.Numerics.Generate.FibonacciSequence()|>Seq.skip 5|>Seq.filter(fun n->n%4I=1I && Open.Numeric.Primes.MillerRabin.IsProbablePrime &n)}|>Seq.take 23|>Seq.iteri(fun n g->printfn "%2d->%A" (n+1) g) </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 21->96041200618922553823942883360924865026104917411877067816822264789029014378308478864192589084185254331637646183008074629 22->357103560641909860720907774139063454445569926582843306794041997476301071102767570483343563518510007800304195444080518562630900027386498933944619210192856768352683468831754423234217978525765921040747291316681576556861490773135214861782877716560879686368266117365351884926393775431925116896322341130075880287169244980698837941931247516010101631704349963583400361910809925847721300802741705519412306522941202429437928826033885416656967971559902743150263252229456298992263008126719589203430407385228230361628494860172129702271172926469500802342608722006420745586297267929052509059154340968348509580552307148642001438470316229 23->500195636126957292905024512596972806695803345136243348970565288179435361313804956505581782637634612477979679893275103396147348650762007594937510804541145002304302867341006298493404319657382123201158007188252606550806694535329232256851056656372379649097735304781630173812454531781511107460619516018844320335033801984806819067802561370394036732654089838823551603083295670024453477589093119918386566397677610274213837391954591147603054442650326827980781140275941425217172428448698161710841740688042587204161256084914166762549007012713922172748259690566614580062682196606466498102571627683726718483229578044343646737694436406261444368327649097401550241341102704783841619376027737767077127010039900586625841991295111482539736725172169379740443890332234341104310470907449898415522414805210341138063350999730749950920147250683227798780264811215647706542511681027825390882770762662185410080310045261286851842669934849330548237271838345164232560544964315090365421726004108704302854387700053591957
Factor
<lang factor>USING: kernel lists lists.lazy math.primes prettyprint sequences ;
- prime-fib ( -- list )
{ 0 1 } [ [ rest ] [ sum suffix ] bi ] lfrom-by [ second ] lmap-lazy [ prime? ] lfilter ;
9 prime-fib ltake [ . ] leach</lang>
- Output:
2 3 5 13 89 233 1597 28657 514229
Go
<lang go>package main
import "fmt"
func isPrime(n uint64) bool {
if n < 2 { return false } if n%2 == 0 { return n == 2 } if n%3 == 0 { return n == 3 } d := uint64(5) for d*d <= n { if n%d == 0 { return false } d += 2 if n%d == 0 { return false } d += 4 } return true
}
func main() {
f1 := uint64(1) f2 := f1 count := 0 limit := 12 // as far as we can get without using big.Int fmt.Printf("The first %d prime Fibonacci numbers are:\n", limit) for count < limit { f3 := f1 + f2 if isPrime(f3) { fmt.Printf("%d ", f3) count++ } f1 = f2 f2 = f3 } fmt.Println()
}</lang>
- Output:
The first 12 prime Fibonacci numbers are: 2 3 5 13 89 233 1597 28657 514229 433494437 2971215073 99194853094755497
J
Here, we pick a convenient expression for generating fibonacci numbers
<lang J>fib=: <. 0.5 + (%:5) %~ (2 %~ 1+%:5)^i.63</lang>
Then we select the first 9 which are prime:
<lang J> 9 {. (#~ 1&p:) fib 2 3 5 13 89 233 1597 28657 514229</lang>
jq
Works with jq (*)
Works with gojq, the Go implementation of jq
See Erdős-primes#jq for a suitable definition of `is_prime` as used here.
(*) For unlimited precision integer arithmetic, use gojq. <lang jq># Emit an unbounded stream of Fibonacci numbers def fibonaccis:
# input: [f(i-2), f(i-1)] def fib: (.[0] + .[1]) as $sum | if .[2] == 0 then $sum else $sum, ([ .[1], $sum ] | fib) end; [-1, 1] | fib;
"The first 9 prime Fibonacci numbers are:", limit(9; fibonaccis | select(is_prime))</lang>
- Output:
The first 9 prime Fibonacci numbers are: 2 3 5 13 89 233 1597 28657 514229
Julia
<lang julia>using Lazy using Primes
fibs = @lazy big"0":big"1":(fibs + drop(1, fibs))
primefibs = @>> fibs filter(isprime)
println(take(9, primefibs)) # List: (2 3 5 13 89 233 1597 28657 514229) </lang>
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
Phix
You can run this online here.
with javascript_semantics include mpfr.e integer n = 1, count=0 mpz f = mpz_init() atom t0 = time(), t1 = time()+1 while count<iff(platform()=JS?21:26) do integer fn = iff(n<4?n+2:get_prime(n)) mpz_fib_ui(f, fn) if mpz_prime(f) then count += 1 string e = elapsed(time()-t0) printf(1,"%2d: fib(%d) = %s (%s)\n",{count,fn,shorten(mpz_get_str(f)),e}) elsif platform()!=JS and time()>t1 then printf(1,"%d\r",fn) t1 = time()+1 end if n += 1 end while
- Output:
1: fib(3) = 2 (0s) 2: fib(4) = 3 (0.1s) 3: fib(5) = 5 (0.2s) 4: fib(7) = 13 (0.2s) 5: fib(11) = 89 (0.2s) 6: fib(13) = 233 (0.2s) 7: fib(17) = 1597 (0.2s) 8: fib(23) = 28657 (0.2s) 9: fib(29) = 514229 (0.2s) 10: fib(43) = 433494437 (0.2s) 11: fib(47) = 2971215073 (0.2s) 12: fib(83) = 99194853094755497 (0.2s) 13: fib(131) = 1066340417491710595814572169 (0.2s) 14: fib(137) = 19134702400093278081449423917 (0.2s) 15: fib(359) = 47542043773469822074...62268716376935476241 (75 digits) (0.2s) 16: fib(431) = 52989271100609562179...55134424689676262369 (90 digits) (0.2s) 17: fib(433) = 13872771278047838271...25954602593712568353 (91 digits) (0.2s) 18: fib(449) = 30617199924845450305...49015933442805665949 (94 digits) (0.2s) 19: fib(509) = 10597999265301490732...54396195769876129909 (107 digits) (0.2s) 20: fib(569) = 36684474316080978061...15228143777781065869 (119 digits) (0.2s) 21: fib(571) = 96041200618922553823...31637646183008074629 (119 digits) (0.2s) 22: fib(2971) = 35710356064190986072...48642001438470316229 (621 digits) (2.8s) 23: fib(4723) = 50019563612695729290...02854387700053591957 (987 digits) (14.0s) 24: fib(5387) = 29304412869392580554...82040327194725855833 (1,126 digits) (22.4s) 25: fib(9311) = 34232086066590238613...37580645424669476289 (1,946 digits) (2 minutes and 38s) 26: fib(9677) = 10565977873308861656...95169792504550670357 (2,023 digits) (3 minutes and 3s)
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...
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... 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