First 9 prime Fibonacci number: Difference between revisions

Content added Content deleted
m (Added Algol W)
m (syntax highlighting fixup automation)
Line 5: Line 5:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO;
<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;</lang>
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}}
<lang algol68>BEGIN # show the first 9 prime fibonacci numbers #
<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</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 89: Line 89:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang ada>begin % -- show the first 9 prime fibonacci numbers %
<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.</lang>
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}}===
<lang BASIC256>function isPrime(v)
<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</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 207: Line 207:


==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
<lang freebasic>Function isPrime(Byval ValorEval As Integer) As Boolean
<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</lang>
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}}===
<lang PureBasic>Procedure isPrime(v.i)
<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</lang>
EndIf</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 292: Line 292:


==={{header|Yabasic}}===
==={{header|Yabasic}}===
<lang yabasic>sub isPrime(v)
<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</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 334: Line 334:
{{trans|Wren}}
{{trans|Wren}}
Requires C99 or later.
Requires C99 or later.
<lang c>#include <stdio.h>
<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;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 378: Line 378:
{{libheader|GMP}}
{{libheader|GMP}}
{{libheader|Primesieve}}
{{libheader|Primesieve}}
<lang cpp>#include <chrono>
<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";
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 485: Line 485:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>fibonacci = iter () yields (int)
<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</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>2
<pre>2
Line 531: Line 531:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<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.</lang>
EXIT.</syntaxhighlight>
{{out}}
{{out}}
<pre> 2
<pre> 2
Line 607: Line 607:


=={{header|Comal}}==
=={{header|Comal}}==
<lang comal>0010 FUNC prime(n) CLOSED
<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</lang>
0230 END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 643: Line 643:


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<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;</lang>
end loop;</syntaxhighlight>
{{out}}
{{out}}
<pre>2
<pre>2
Line 689: Line 689:


=={{header|Draco}}==
=={{header|Draco}}==
<lang draco>proc nonrec prime(ulong n) bool:
<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</lang>
corp</syntaxhighlight>
{{out}}
{{out}}
<pre>2
<pre>2
Line 736: Line 736:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<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}}
<lang factor>USING: kernel lists lists.lazy math.primes prettyprint sequences ;
<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</lang>
9 prime-fib ltake [ . ] leach</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 791: Line 791:
=={{header|Go}}==
=={{header|Go}}==
{{trans|C}}
{{trans|C}}
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 835: Line 835:
}
}
fmt.Println()
fmt.Println()
}</lang>
}</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]


<lang J>fib=: <. 0.5 + (%:5) %~ (2 %~ 1+%:5)^i.63</lang>
<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:


<lang J> 9 {. (#~ 1&p:) fib
<syntaxhighlight lang="j"> 9 {. (#~ 1&p:) fib
2 3 5 13 89 233 1597 28657 514229</lang>
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.
<lang jq># Emit an unbounded stream of Fibonacci numbers
<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))</lang>
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]].
<lang java>import java.math.BigInteger;
<syntaxhighlight lang="java">import java.math.BigInteger;


public class PrimeFibonacciGenerator {
public class PrimeFibonacciGenerator {
Line 949: Line 949:
return str;
return str;
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 983: Line 983:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Lazy
<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:
<lang Mathematica>Select[Fibonacci /@ Range[100], PrimeQ, 9]</lang>
<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:
<lang Mathematica>list = {};
<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}]</lang>
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}}==
<lang perl>#!/usr/bin/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";</lang>
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].
<!--<lang Phix>(phixonline)-->
<!--<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,115: Line 1,115:
=={{header|Pike}}==
=={{header|Pike}}==
{{trans|C}}
{{trans|C}}
<lang Pike>bool isPrime(int n) {
<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;
}</lang>
}</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}}==
<lang 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 perl6>put ++$ .fmt("%2d: ") ~ $_ for (0, 1, * + * … *).grep( &is-prime )[^20];</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}}==
<lang 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}}==
<lang rust>// [dependencies]
<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());
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,365: Line 1,365:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>say 12.by { .fib.is_prime }.map { .fib }</lang>
<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}}
<lang ecmascript>import "./math" for Int
<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()</lang>
System.print()</syntaxhighlight>


{{out}}
{{out}}
Line 1,398: Line 1,398:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is prime
<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;
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}