First 9 prime Fibonacci number: Difference between revisions
Content added Content deleted
m (Added Algol W) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 5: | Line 5: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Prime_Fibonacci is |
procedure Prime_Fibonacci is |
||
Line 51: | Line 51: | ||
end if; |
end if; |
||
end loop; |
end loop; |
||
end Prime_Fibonacci;</ |
end Prime_Fibonacci;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 67: | Line 67: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{libheader|ALGOL 68-primes}} |
{{libheader|ALGOL 68-primes}} |
||
< |
<syntaxhighlight lang="algol68">BEGIN # show the first 9 prime fibonacci numbers # |
||
PR read "primes.incl.a68" PR # include prime utilities # |
PR read "primes.incl.a68" PR # include prime utilities # |
||
INT p count := 0; |
INT p count := 0; |
||
Line 82: | Line 82: | ||
FI |
FI |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 89: | Line 89: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="ada">begin % -- show the first 9 prime fibonacci numbers % |
||
% -- returns true if n is prime, false otherwise - uses trial division % |
% -- returns true if n is prime, false otherwise - uses trial division % |
||
Line 120: | Line 120: | ||
end while_pCount_lt_9 |
end while_pCount_lt_9 |
||
end task |
end task |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 127: | Line 127: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f FIRST_9_PRIME_FIBONACCI_NUMBER.AWK |
# syntax: GAWK -f FIRST_9_PRIME_FIBONACCI_NUMBER.AWK |
||
BEGIN { |
BEGIN { |
||
Line 158: | Line 158: | ||
return(1) |
return(1) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 168: | Line 168: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
< |
<syntaxhighlight lang="basic256">function isPrime(v) |
||
if v < 2 then return False |
if v < 2 then return False |
||
if v mod 2 = 0 then return v = 2 |
if v mod 2 = 0 then return v = 2 |
||
Line 200: | Line 200: | ||
end if |
end if |
||
end while |
end while |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 207: | Line 207: | ||
==={{header|FreeBASIC}}=== |
==={{header|FreeBASIC}}=== |
||
< |
<syntaxhighlight lang="freebasic">Function isPrime(Byval ValorEval As Integer) As Boolean |
||
If ValorEval <= 1 Then Return False |
If ValorEval <= 1 Then Return False |
||
For i As Integer = 2 To Int(Sqr(ValorEval)) |
For i As Integer = 2 To Int(Sqr(ValorEval)) |
||
Line 235: | Line 235: | ||
End If |
End If |
||
Loop |
Loop |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The first 9 Prime Fibonacci numbers: |
<pre>The first 9 Prime Fibonacci numbers: |
||
Line 241: | Line 241: | ||
==={{header|PureBasic}}=== |
==={{header|PureBasic}}=== |
||
< |
<syntaxhighlight lang="purebasic">Procedure isPrime(v.i) |
||
If v <= 1 : ProcedureReturn #False |
If v <= 1 : ProcedureReturn #False |
||
ElseIf v < 4 : ProcedureReturn #True |
ElseIf v < 4 : ProcedureReturn #True |
||
Line 285: | Line 285: | ||
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input() |
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 292: | Line 292: | ||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
< |
<syntaxhighlight lang="yabasic">sub isPrime(v) |
||
if v < 2 then return False : fi |
if v < 2 then return False : fi |
||
if mod(v, 2) = 0 then return v = 2 : fi |
if mod(v, 2) = 0 then return v = 2 : fi |
||
Line 324: | Line 324: | ||
end if |
end if |
||
loop |
loop |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 334: | Line 334: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
Requires C99 or later. |
Requires C99 or later. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdint.h> |
#include <stdint.h> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
Line 367: | Line 367: | ||
printf("\n"); |
printf("\n"); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 378: | Line 378: | ||
{{libheader|GMP}} |
{{libheader|GMP}} |
||
{{libheader|Primesieve}} |
{{libheader|Primesieve}} |
||
< |
<syntaxhighlight lang="cpp">#include <chrono> |
||
#include <iostream> |
#include <iostream> |
||
#include <sstream> |
#include <sstream> |
||
Line 451: | Line 451: | ||
std::chrono::duration<double> ms(finish - start); |
std::chrono::duration<double> ms(finish - start); |
||
std::cout << "elapsed time: " << ms.count() << " seconds\n"; |
std::cout << "elapsed time: " << ms.count() << " seconds\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 485: | Line 485: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">fibonacci = iter () yields (int) |
||
a: int := 1 |
a: int := 1 |
||
b: int := 1 |
b: int := 1 |
||
Line 518: | Line 518: | ||
end |
end |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 |
<pre>2 |
||
Line 531: | Line 531: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. PRIME-FIBONACCI. |
PROGRAM-ID. PRIME-FIBONACCI. |
||
Line 594: | Line 594: | ||
IF FIB IS EQUAL TO 2 OR 3, MOVE 'X' TO PRIME-FLAG. |
IF FIB IS EQUAL TO 2 OR 3, MOVE 'X' TO PRIME-FLAG. |
||
DONE. |
DONE. |
||
EXIT.</ |
EXIT.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 2 |
<pre> 2 |
||
Line 607: | Line 607: | ||
=={{header|Comal}}== |
=={{header|Comal}}== |
||
< |
<syntaxhighlight lang="comal">0010 FUNC prime(n) CLOSED |
||
0020 IF n<4 THEN RETURN n=2 OR n=3 |
0020 IF n<4 THEN RETURN n=2 OR n=3 |
||
0030 IF n MOD 2=0 OR n MOD 3=0 THEN RETURN FALSE |
0030 IF n MOD 2=0 OR n MOD 3=0 THEN RETURN FALSE |
||
Line 629: | Line 629: | ||
0210 c:=a+b;a:=b;b:=c |
0210 c:=a+b;a:=b;b:=c |
||
0220 ENDWHILE |
0220 ENDWHILE |
||
0230 END</ |
0230 END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 643: | Line 643: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
sub prime(n: uint32): (p: uint8) is |
sub prime(n: uint32): (p: uint8) is |
||
Line 676: | Line 676: | ||
a := b; |
a := b; |
||
b := c; |
b := c; |
||
end loop;</ |
end loop;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 |
<pre>2 |
||
Line 689: | Line 689: | ||
=={{header|Draco}}== |
=={{header|Draco}}== |
||
< |
<syntaxhighlight lang="draco">proc nonrec prime(ulong n) bool: |
||
bool comp; |
bool comp; |
||
ulong d; |
ulong d; |
||
Line 723: | Line 723: | ||
b := c |
b := c |
||
od |
od |
||
corp</ |
corp</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 |
<pre>2 |
||
Line 736: | Line 736: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Prime Fibonacci Numbers. Nigel Galloway: January 21st., 2022 |
// 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) |
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) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 769: | Line 769: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: kernel lists lists.lazy math.primes prettyprint sequences ; |
||
: prime-fib ( -- list ) |
: prime-fib ( -- list ) |
||
Line 775: | Line 775: | ||
[ second ] lmap-lazy [ prime? ] lfilter ; |
[ second ] lmap-lazy [ prime? ] lfilter ; |
||
9 prime-fib ltake [ . ] leach</ |
9 prime-fib ltake [ . ] leach</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 791: | Line 791: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 835: | Line 835: | ||
} |
} |
||
fmt.Println() |
fmt.Println() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 847: | Line 847: | ||
Here, we pick a convenient expression and [https://code.jsoftware.com/wiki/Essays/Fibonacci_Sequence generate fibonacci numbers] |
Here, we pick a convenient expression and [https://code.jsoftware.com/wiki/Essays/Fibonacci_Sequence generate fibonacci numbers] |
||
< |
<syntaxhighlight lang="j">fib=: <. 0.5 + (%:5) %~ (2 %~ 1+%:5)^i.63</syntaxhighlight> |
||
Then we select the first 9 which are prime: |
Then we select the first 9 which are prime: |
||
< |
<syntaxhighlight lang="j"> 9 {. (#~ 1&p:) fib |
||
2 3 5 13 89 233 1597 28657 514229</ |
2 3 5 13 89 233 1597 28657 514229</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 863: | Line 863: | ||
(*) For unlimited precision integer arithmetic, use gojq. |
(*) For unlimited precision integer arithmetic, use gojq. |
||
< |
<syntaxhighlight lang="jq"># Emit an unbounded stream of Fibonacci numbers |
||
def fibonaccis: |
def fibonaccis: |
||
# input: [f(i-2), f(i-1)] |
# input: [f(i-2), f(i-1)] |
||
Line 873: | Line 873: | ||
"The first 9 prime Fibonacci numbers are:", |
"The first 9 prime Fibonacci numbers are:", |
||
limit(9; fibonaccis | select(is_prime))</ |
limit(9; fibonaccis | select(is_prime))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 890: | Line 890: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
Uses the PrimeGenerator class from [[Extensible prime generator#Java]]. |
Uses the PrimeGenerator class from [[Extensible prime generator#Java]]. |
||
< |
<syntaxhighlight lang="java">import java.math.BigInteger; |
||
public class PrimeFibonacciGenerator { |
public class PrimeFibonacciGenerator { |
||
Line 949: | Line 949: | ||
return str; |
return str; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 983: | Line 983: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Lazy |
||
using Primes |
using Primes |
||
Line 991: | Line 991: | ||
println(take(9, primefibs)) # List: (2 3 5 13 89 233 1597 28657 514229) |
println(take(9, primefibs)) # List: (2 3 5 13 89 233 1597 28657 514229) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
First solution by guessing some upper bound: |
First solution by guessing some upper bound: |
||
< |
<syntaxhighlight lang="mathematica">Select[Fibonacci /@ Range[100], PrimeQ, 9]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{2, 3, 5, 13, 89, 233, 1597, 28657, 514229}</pre> |
<pre>{2, 3, 5, 13, 89, 233, 1597, 28657, 514229}</pre> |
||
Second solution without guessing some upper bound: |
Second solution without guessing some upper bound: |
||
< |
<syntaxhighlight lang="mathematica">list = {}; |
||
Do[ |
Do[ |
||
f = Fibonacci[i]; |
f = Fibonacci[i]; |
||
Line 1,011: | Line 1,011: | ||
]; |
]; |
||
out=Row[{"F(",#1,") = ",If[IntegerLength[#2]<=10,#2,Row@Catenate[{Take[IntegerDigits[#2],5],{" \[Ellipsis] "},Take[IntegerDigits[#2],-5],{" (",IntegerLength[#2]," digits)"}}]]}]&@@@list; |
out=Row[{"F(",#1,") = ",If[IntegerLength[#2]<=10,#2,Row@Catenate[{Take[IntegerDigits[#2],5],{" \[Ellipsis] "},Take[IntegerDigits[#2],-5],{" (",IntegerLength[#2]," digits)"}}]]}]&@@@list; |
||
TableForm[out,TableHeadings->{Automatic,None}]</ |
TableForm[out,TableHeadings->{Automatic,None}]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 F(3) = 2 |
<pre>1 F(3) = 2 |
||
Line 1,041: | Line 1,041: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/First_9_Prime_Fibonacci_Number |
use strict; # https://rosettacode.org/wiki/First_9_Prime_Fibonacci_Number |
||
Line 1,054: | Line 1,054: | ||
is_prime( $x ) and push @first, $x; |
is_prime( $x ) and push @first, $x; |
||
} |
} |
||
print "@first\n";</ |
print "@first\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,063: | Line 1,063: | ||
{{libheader|Phix/online}} |
{{libheader|Phix/online}} |
||
You can run this online [http://phix.x10.mx/p2js/primefib.htm here]. |
You can run this online [http://phix.x10.mx/p2js/primefib.htm here]. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
Line 1,082: | Line 1,082: | ||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,115: | Line 1,115: | ||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="pike">bool isPrime(int n) { |
||
if (n < 2) { |
if (n < 2) { |
||
return false; |
return false; |
||
Line 1,161: | Line 1,161: | ||
write("\n"); |
write("\n"); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The first 12 prime Fibonacci numbers are: |
<pre>The first 12 prime Fibonacci numbers are: |
||
Line 1,167: | Line 1,167: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python"> |
||
print("working...") |
print("working...") |
||
print("The firsr 9 Prime Fibonacci numbers:") |
print("The firsr 9 Prime Fibonacci numbers:") |
||
Line 1,199: | Line 1,199: | ||
print() |
print() |
||
print("done...") |
print("done...") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,210: | Line 1,210: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>put ++$ .fmt("%2d: ") ~ $_ for (0, 1, * + * … *).grep( &is-prime )[^20];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1: 2 |
<pre> 1: 2 |
||
Line 1,234: | Line 1,234: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlibcore.ring" |
load "stdlibcore.ring" |
||
see "working..." + nl |
see "working..." + nl |
||
Line 1,258: | Line 1,258: | ||
if nr = 1 return 1 ok |
if nr = 1 return 1 ok |
||
if nr > 1 return fib(nr-1) + fib(nr-2) ok |
if nr > 1 return fib(nr-1) + fib(nr-2) ok |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,268: | Line 1,268: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">// [dependencies] |
||
// rug = "1.15.0" |
// rug = "1.15.0" |
||
// primal = "0.3" |
// primal = "0.3" |
||
Line 1,331: | Line 1,331: | ||
let time = now.elapsed(); |
let time = now.elapsed(); |
||
println!("elapsed time: {} milliseconds", time.as_millis()); |
println!("elapsed time: {} milliseconds", time.as_millis()); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,365: | Line 1,365: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">say 12.by { .fib.is_prime }.map { .fib }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,373: | Line 1,373: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
< |
<syntaxhighlight lang="ecmascript">import "./math" for Int |
||
var limit = 11 // as far as we can go without using BigInt |
var limit = 11 // as far as we can go without using BigInt |
||
Line 1,389: | Line 1,389: | ||
f2 = f3 |
f2 = f3 |
||
} |
} |
||
System.print()</ |
System.print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,398: | Line 1,398: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime |
||
int N, I; |
int N, I; |
||
[if N <= 2 then return N = 2; |
[if N <= 2 then return N = 2; |
||
Line 1,420: | Line 1,420: | ||
N:= F; |
N:= F; |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |