First 9 prime Fibonacci number: Difference between revisions

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

edits