First 9 prime Fibonacci number: Difference between revisions

Added Oberon-07
(Added Oberon-07)
 
(9 intermediate revisions by 5 users not shown)
Line 45:
fib(47): 2971215073
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO REPORT prime n:
REPORT n>=2 AND NO d IN {2..floor (root n)} HAS n mod d = 0
 
PUT 1, 1 IN a, b
PUT 0 IN n
WHILE n<9:
IF prime a:
WRITE a/
PUT n+1 IN n
PUT b, a+b IN a, b</syntaxhighlight>
{{out}}
<pre>2
3
5
13
89
233
1597
28657
514229</pre>
 
=={{header|Ada}}==
Line 445 ⟶ 467:
<syntaxhighlight lang="cpp">#include <chrono>
#include <iostream>
#include <sstream>
#include <utility>
#include <primesieve.hpp>
Line 497 ⟶ 518:
std::string to_string(const big_int& n) {
std::string str = n.get_str();
ifsize_t len = (str.size() > 40) {;
if (len > 40) {
std::ostringstream os;
os << str.substrreplace(020, 20)len <<- 40, "..." << str.substr(str.size() - 20) << " (";
<< str.size() <<+= " digits)(";
return os.str += std::to_string(len);
str += " digits)";
}
return str;
Line 859 ⟶ 881:
28657
514229</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
prev = 1
val = 1
while cnt < 9
h = prev + val
prev = val
val = h
if isprim val = 1
write val & " "
cnt += 1
.
.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 13 89 233 1597 28657 514229
</pre>
 
=={{header|F_Sharp|F#}}==
Line 1,221 ⟶ 1,272:
25 F(9311) = 34232 … 76289 (1946 digits)
26 F(9677) = 10565 … 70357 (2023 digits)</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [(Stdout . lay . map show . take 9 . filter prime) fibo]
 
fibo :: [num]
fibo = 1 : 1 : [a + b | (a,b) <- zip2 fibo (tl fibo)]
 
prime :: num->bool
prime n = n=2 \/ n=3, if n<=4
prime n = False, if or [n mod 2=0, n mod 3=0]
prime n = and [n mod d ~= 0 | d <- [3, 5..entier (sqrt n)]]</syntaxhighlight>
{{out}}
<pre>2
3
5
13
89
233
1597
28657
514229</pre>
 
=={{header|Nim}}==
Line 1,259 ⟶ 1,332:
<pre>The first 9 prime Fibonacci numbers are:
2 3 5 13 89 233 1597 28657 514229
</pre>
 
=={{header|Oberon-07}}==
<syntaxhighlight lang="modula2">
MODULE PrimeFibonacciNumbers; (* show the first 9 prime fibonacci numbers *)
IMPORT Out, Math;
 
CONST toFind = 9;
VAR pCount, prev, curr, next :INTEGER;
 
(* returns true if n is prime, false otherwise - uses trial division *)
PROCEDURE isPrime( n :INTEGER ):BOOLEAN;
VAR i, rootN :INTEGER;
prime :BOOLEAN;
BEGIN
IF n < 3 THEN prime := n = 2
ELSIF ~ ODD( n ) THEN prime := FALSE
ELSE
prime := TRUE;
i := 3;
rootN := FLOOR( Math.sqrt( FLT( n ) ) );
WHILE ( i <= rootN ) & prime DO
prime := n MOD i # 0;
i := i + 2
END
END
RETURN prime
END isPrime ;
 
BEGIN
pCount := 0;
prev := 0;
curr := 1;
WHILE pCount < toFind DO
next := prev + curr;
prev := curr;
curr := next;
IF isPrime( curr ) THEN
pCount := pCount + 1;
Out.String( " " );Out.Int( curr, 0 )
END
END
END PrimeFibonacciNumbers.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 13 89 233 1597 28657 514229
</pre>
 
Line 1,409 ⟶ 1,529:
<syntaxhighlight lang="python">
2 things:
"""for the Fibonacci sequence: an even number is following after 2 odd numbers. Eliminate time to check whether it is prime or not because even numbers are not primes.
for prime numbers: it becomes bigger and bigger. The original algorithm will be slow for super big number. In this case, I use Miller Rabin primality test.
 
P/S: I am not surprised. It is fast but still cannot compare to other languages such as C++ or Rust or .... After all, Python is still slow :P"""
 
def miller_rabin(n, k=5):
Line 1,552 ⟶ 1,672:
20: 36684474316080978061473613646275630451100586901195229815270242868417768061193560857904335017879540515228143777781065869</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Gen 9 Prime Fibo (1 1)>>;
}
 
Gen {
0 s.Filter s.Gen (e.State) = ;
s.N s.Filter s.Gen (e.State),
<Mu s.Gen (e.State)>: (e.Next) e.Val,
<Mu s.Filter e.Val>: {
True = e.Val <Gen <- s.N 1> s.Filter s.Gen (e.Next)>;
False = <Gen s.N s.Filter s.Gen (e.Next)>;
};
};
 
Fibo {
(s.A s.B) = (s.B <+ s.A s.B>) s.A;
};
 
Prime {
0 = False; 1 = False;
2 = True; 3 = True;
s.N, <Mod s.N 2>: 0 = False;
s.N, <Mod s.N 3>: 0 = False;
s.N = <Prime1 s.N 5>;
};
 
Prime1 {
s.N s.D, <Compare s.N <* s.D s.D>>: '-' = True;
s.N s.D, <Mod s.N s.D>: 0 = False;
s.N s.D = <Prime1 s.N <+ 2 s.D>>;
};</syntaxhighlight>
{{out}}
<pre>2 3 5 13 89 233 1597 28657 514229</pre>
=={{header|Ring}}==
<syntaxhighlight lang="ring">
Line 1,710 ⟶ 1,864:
[2, 3, 5, 13, 89, 233, 1597, 28657, 514229]
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program prime_fibonacci;
[a, b] := [1, 1];
loop until seen = 9 do
if prime a then
print(a);
seen +:= 1;
end if;
[a, b] := [b, a+b];
end loop;
 
op prime(n);
if n<=4 then
return n in {2, 3};
end if;
if n mod 2 = 0 or n mod 3 = 0 then
return false;
end if;
d := 5;
loop while d*d <= n do
if n mod d = 0 then return false; end if;
d +:= 2;
if n mod d = 0 then return false; end if;
d +:= 4;
end loop;
return true;
end op;
end program;</syntaxhighlight>
{{out}}
<pre>2
3
5
13
89
233
1597
28657
514229</pre>
 
=={{header|Sidef}}==
Line 1,720 ⟶ 1,913:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var limit = 11 // as far as we can go without using BigInt
3,026

edits