Iccanobif primes: Difference between revisions

Added Sidef
(→‎{{header|Wren}}: Realigned with Raku output.)
(Added Sidef)
 
(40 intermediate revisions by 18 users not shown)
Line 13:
 
;See also
;* [[oeis:A03697A036797|OEIS:A03697A036797 - Iccanobif (or iccanobiF) primes: primes which are Fibonacci numbers when reversed]]
 
 
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # show the first 10 prime Iccanobif (reversed Fibonacci) numbers #
# returns n with the digits reversed #
OP REVERSE = ( INT n )INT:
BEGIN
INT reverse := 0;
INT v := ABS n;
WHILE v > 0 DO
reverse *:= 10 +:= v MOD 10;
v OVERAB 10
OD;
IF n < 0 THEN - reverse ELSE reverse FI
END # REVERSE # ;
# returns TRUE if n is prime, FALSE otherwise - uses trial division #
PROC is prime = ( LONG INT n )BOOL:
IF n < 3 THEN n = 2
ELIF n MOD 3 = 0 THEN n = 3
ELIF NOT ODD n THEN FALSE
ELSE
BOOL is a prime := TRUE;
INT f := 5;
INT f2 := 25;
INT to next := 24;
WHILE f2 <= n AND is a prime DO
is a prime := n MOD f /= 0;
f +:= 2;
f2 +:= to next;
to next +:= 8
OD;
is a prime
FI # is prime # ;
# task #
INT p count := 0;
INT prev := 0;
INT curr := 1;
WHILE p count < 10 DO
INT next = prev + curr;
prev := curr;
curr := next;
INT rev := REVERSE curr;
IF is prime( rev ) THEN
# have a prime iccanobif number #
p count +:= 1;
print( ( " ", whole( rev, 0 ) ) )
FI
OD
END
</syntaxhighlight>
{{out}}
<pre>
2 3 5 31 43 773 7951 64901 52057 393121
</pre>
 
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<syntaxhighlight lang="algolw">
begin % show the first 10 prime Iccanobif (reversed Fibonacci) numbers %
 
% returns n with the digits reversed %
integer procedure reverse ( integer value n ) ;
begin
integer rev, v;
rev := 0;
v := abs n;
while v > 0 do begin
rev := ( rev * 10 ) + ( v rem 10 );
v := v div 10
end while_v_gt_0 ;
if n < 0 then - rev else rev
end reverse ;
 
% returns true if n is prime, false otherwise, uses trial division %
logical procedure isPrime ( integer value n ) ;
if n < 3 then n = 2
else if n rem 3 = 0 then n = 3
else if not odd( n ) then false
else begin
logical prime;
integer f, f2, toNext;
prime := true;
f := 5;
f2 := 25;
toNext := 24; % note: ( 2n + 1 )^2 - ( 2n - 1 )^2 = 8n %
while f2 <= n and prime do begin
prime := n rem f not = 0;
f := f + 2;
f2 := toNext;
toNext := toNext + 8
end while_f2_le_n_and_prime ;
prime
end isPrime ;
 
begin % task %
integer pCount, curr, prev, next, rev;
pCount := 0;
prev := 0;
curr := 1;
while pCount < 10 do begin
next := prev + curr;
prev := curr;
curr := next;
rev := reverse( curr );
if isPrime( rev ) then begin
% have a prime iccanobif number %
pCount := pCount + 1;
writeon( i_w := 1, s_w := 0, " ", rev )
end if_isPrime_rev
end while_pCount_lt_10
end
end.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 31 43 773 7951 64901 52057 393121
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">summarize: function [n :string][
;; description: « returns a summary of a numeric string
s: size n
if s > 20 -> n: ((take n 10)++"...")++drop.times:s-10 n
n ++ ~" (|s| digits)"
]
 
[a b count]: [0 1 0]
print "First 27 Iccanobif primes:"
while -> count < 27 [
if prime? to :integer r: <= reverse ~"|a|" [
print [pad ~"|count+1|" 2 "->" summarize r]
inc 'count
]
[a b]: @[b a+b]
]</syntaxhighlight>
 
{{out}}
 
<pre>First 27 Iccanobif primes:
1 -> 2 (1 digits)
2 -> 3 (1 digits)
3 -> 5 (1 digits)
4 -> 31 (2 digits)
5 -> 43 (2 digits)
6 -> 773 (3 digits)
7 -> 7951 (4 digits)
8 -> 64901 (5 digits)
9 -> 52057 (5 digits)
10 -> 393121 (6 digits)
11 -> 56577108676171 (14 digits)
12 -> 9406476074...3258103531 (21 digits)
13 -> 5237879497...9575442761 (37 digits)
14 -> 9026258083...2307801963 (40 digits)
15 -> 1990033567...3266446403 (80 digits)
16 -> 7784113736...3685331923 (104 digits)
17 -> 3772258590...2830756131 (137 digits)
18 -> 7573619389...4714305761 (330 digits)
19 -> 1789033684...5235035913 (406 digits)
20 -> 9232716310...6047302507 (409 digits)
21 -> 5042015781...7362214481 (503 digits)
22 -> 3051101247...1330018201 (888 digits)
23 -> 4681854704...4645856321 (1020 digits)
24 -> 8710134785...8865227391 (1122 digits)
25 -> 1745165602...1843652461 (1911 digits)
26 -> 4898934056...4215909399 (1947 digits)
27 -> 1274692768...7994940101 (2283 digits)</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">#include "isprime.kbs"
 
cnt = 0 : prev = 0 : curr = 1
 
print "First 10 iccanobiF primes:"
while cnt < 10
sgte = prev + curr
prev = curr
curr = sgte
rev = reverseNumber(curr)
if isPrime(rev) then
# have a prime iccanobif number
cnt += 1
print rev; " ";
end if
end while
end
 
function reverseNumber(num)
if num < 10 then return num
reverse = 0
while num > 0
reverse = 10 * reverse + num mod 10
num = int(num / 10)
end while
return reverse
end function
</syntaxhighlight>
 
 
==={{header|FreeBASIC}}===
{{trans|ALGOL 68}}
<syntaxhighlight lang="vb">#include "isprime.bas"
 
' returns num with the digits reversed
Function reverseNumber(num As Uinteger) As Uinteger
If num < 10 Then Return num
Dim As Integer reverse = 0
While num > 0
reverse = 10 * reverse + (num Mod 10)
num \= 10
Wend
Return reverse
End Function
 
Dim As Byte cnt = 0
Dim As Uinteger prev = 0, curr = 1
Dim As Uinteger sgte, rev
Print "First 11 iccanobiF primes:"
While cnt < 11
sgte = prev + curr
prev = curr
curr = sgte
rev = reverseNumber(curr)
If isPrime(rev) Then
' have a prime iccanobif number
cnt += 1
Print rev; " ";
End If
Wend
 
Sleep</syntaxhighlight>
{{out}}
<pre>First 11 iccanobiF primes:
2 3 5 31 43 773 7951 64901 52057 393121 56577108676171</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim cnt As Short = 0, prev As Long = 0, curr As Long = 1
Dim sgte As Long, rev As Long
Print "First 11 iccanobiF primes:"
While cnt < 11
sgte = prev + curr
prev = curr
curr = sgte
rev = reverseNumber(curr)
If isPrime(rev) Then
' have a prime iccanobif number
cnt += 1
Print rev; " ";
End If
Wend
Print
 
End
 
Function reverseNumber(num As Long) As Long
If num < 10 Then Return num
Dim reverse As Long = 0
While num > 0
reverse = 10 * reverse + (num Mod 10)
num \= 10
Wend
Return reverse
End Function
 
Sub isPrime(ValorEval As Long) As Boolean
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
If ValorEval Mod 3 = 0 Then Return ValorEval = 3
Dim d As Long = 5
While d * d <= ValorEval
If ValorEval Mod d = 0 Then Return False Else d += 2
Wend
Return True
End Function
</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">cnt = 0
prev = 0
curr = 1
print "First 11 iccanobiF primes:"
while cnt < 11
sgte = prev + curr
prev = curr
curr = sgte
rev = reverseNumber(curr)
if isPrime(rev) then
' have a prime iccanobif number
cnt = cnt + 1
print rev; " ";
end if
wend
end
 
function isPrime(n)
if n < 2 then isPrime = 0 : goto [exit]
if n = 2 then isPrime = 1 : goto [exit]
if n mod 2 = 0 then isPrime = 0 : goto [exit]
isPrime = 1
for i = 3 to int(n^.5) step 2
if n mod i = 0 then isPrime = 0 : goto [exit]
next i
[exit]
end function
 
function reverseNumber(num)
if num < 10 then reverseNumber = num: goto [exit]
reverse = 0
while num > 0
reverse = 10 * reverse + (num mod 10)
num = int(num / 10)
wend
reverseNumber = reverse
[exit]
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">XIncludeFile "isprime.pb"
 
Procedure.i reverseNumber(num.i)
If num < 10 : ProcedureReturn num : EndIf
reverse.i = 0
While num > 0
reverse = 10 * reverse + num % 10
num = Int(num / 10)
Wend
ProcedureReturn reverse
EndProcedure
 
OpenConsole()
cnt.b = 0 : prev.i = 0 : curr.i = 1
 
PrintN("First 11 iccanobiF primes:")
While cnt < 11
sgte = prev + curr
prev = curr
curr = sgte
rev = reverseNumber(curr)
If isPrime(rev):
; have a prime iccanobif number
cnt + 1
Print(Str(rev) + " ")
EndIf
Wend
 
Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">//import isprime
 
cnt = 0 : prev = 0 : curr = 1
print "First 10 iccanobiF primes:"
while cnt < 10
sgte = prev + curr
prev = curr
curr = sgte
rev = reverseNumber(curr)
if isPrime(rev) then
// have a prime iccanobif number
cnt = cnt + 1
print rev, " ";
fi
wend
print
end
 
sub reverseNumber(num)
local revers
if num < 10 return num
revers = 0
while num > 0
revers = 10 * revers + mod(num, 10)
num = int(num / 10)
wend
return revers
end sub</syntaxhighlight>
 
=={{header|C}}==
{{trans|Wren}}
{{libheader|GMP}}
There's a big jump in digit count between the 29th and 30th numbers and consequently the latter is very slow indeed to emerge.
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <gmp.h>
 
char *reverse(char *s) {
int i, j, len = strlen(s);
char t;
for (i = 0, j = len - 1; i < j; ++i, --j) {
t = s[i];
s[i] = s[j];
s[j] = t;
}
return s;
}
 
int main() {
int count = 0;
size_t len;
char *s, a[44];
mpz_t fib, p, prev, curr;
mpz_init(fib);
mpz_init(p);
mpz_init_set_ui(prev, 0);
mpz_init_set_ui(curr, 1);
printf("First 30 Iccanobif primes:\n");
while (count < 30) {
mpz_add(fib, curr, prev);
s = mpz_get_str(NULL, 10, fib);
mpz_set_str(p, reverse(s), 10);
if (mpz_probab_prime_p(p, 15) > 0) {
++count;
s = mpz_get_str(NULL, 10, p);
len = strlen(s);
if (len > 40) {
strncpy(a, s, 20);
strcpy(a + 20, "...");
strncpy(a + 23, s + len - 20, 21);
}
printf("%2d: %s (%ld digits)\n", count, len <= 40 ? s : a, len);
}
mpz_set(prev, curr);
mpz_set(curr, fib);
}
mpz_clear(fib);
mpz_clear(p);
mpz_clear(prev);
mpz_clear(curr);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Same as Wren example.
</pre>
 
=={{header|C++}}==
{{libheader|GMP}}
<syntaxhighlight lang="cpp">#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
 
#include <gmpxx.h>
 
using big_int = mpz_class;
 
bool is_probably_prime(const big_int& n) {
return mpz_probab_prime_p(n.get_mpz_t(), 15) != 0;
}
 
big_int reverse(const big_int& n) {
auto str = n.get_str();
std::reverse(str.begin(), str.end());
return big_int(str, 10);
}
 
std::string to_string(const big_int& num, size_t max_digits) {
std::string str = num.get_str();
size_t len = str.size();
if (len > max_digits) {
str.replace(max_digits / 2, len - max_digits, "...");
str += " (";
str += std::to_string(len);
str += " digits)";
}
return str;
}
 
int main() {
big_int f0 = 0, f1 = 1;
std::cout << "First 30 Iccanobif primes:\n";
for (int count = 0; count < 30;) {
big_int f = f0 + f1;
auto p = reverse(f);
if (is_probably_prime(p)) {
++count;
std::cout << std::setw(2) << count << ": " << to_string(p, 40)
<< '\n';
}
f0 = f1;
f1 = f;
}
}</syntaxhighlight>
 
{{out}}
<pre>
First 30 Iccanobif primes:
1: 2
2: 3
3: 5
4: 31
5: 43
6: 773
7: 7951
8: 64901
9: 52057
10: 393121
11: 56577108676171
12: 940647607443258103531
13: 5237879497657222310489731409575442761
14: 9026258083384996860449366072142307801963
15: 19900335674812302969...34431012073266446403 (80 digits)
16: 77841137362967479985...52312097783685331923 (104 digits)
17: 37722585901567604188...29174997072830756131 (137 digits)
18: 75736193894876131595...50767238644714305761 (330 digits)
19: 17890336847332837620...13175300695235035913 (406 digits)
20: 92327163101729115305...27061468856047302507 (409 digits)
21: 50420157810698056253...67335124247362214481 (503 digits)
22: 30511012474739380092...69296158361330018201 (888 digits)
23: 46818547042693694555...08664543144645856321 (1020 digits)
24: 87101347853037819884...20128396998865227391 (1122 digits)
25: 17451656022543765336...20100243761843652461 (1911 digits)
26: 48989340566288399474...02930339234215909399 (1947 digits)
27: 12746927684958209654...53436989647994940101 (2283 digits)
28: 35746826582658751012...25010735912438195633 (3727 digits)
29: 87987175281297657706...48748727893681871587 (4270 digits)
30: 81807376367113798363...13687506007959668569 (10527 digits)
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
local fn ReverseInteger( n as NSUInteger )
NSUInteger reverse = 0, r
while (n != 0)
r = n mod 10
reverse = reverse * 10 + r
n /= 10
wend
end fn = reverse
 
local fn IccanobifPrimes( limit as NSUInteger )
NSUInteger cnt = 0, prev = 0, curr = 1, sgte, rev
printf @"First 11 IccanobiF primes:"
while (cnt < limit)
sgte = prev + curr
prev = curr
curr = sgte
rev = fn ReverseInteger(curr)
if fn IsPrime(rev)
// Iccanobif prime found
cnt++
printf @"%2lu. %lu", cnt, rev
end if
wend
end fn
 
fn IccanobifPrimes( 11 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
First 11 Iccanobif primes:
1. 2
2. 3
3, 5
4. 31
5. 43
6, 773
7. 7951
8. 64901
9. 52057
10. 393121
11. 56577108676171
</pre>
 
=={{header|J}}==
Implementation:
<syntaxhighlight lang="j"> (#~ 1&p:) |.&.":"0 (, _2 +/@{. ])^:(70) 1
2 3 5 31 43 773 7951 64901 52057 393121 56577108676171</syntaxhighlight>
In other words: 70 numbers from the Fibonacci sequence, reverse their digits, and keep those which are prime.
 
Stretch:
<syntaxhighlight lang="j"> #@":@> 10}. (#~ 1&p:) |.&.(10&#.inv)"0 (, _2 +/@{. ])^:20000]1x
14 21 37 40 80 104 137 330 406 409 503 888 1020 1122 1911 1947 2283 3727</syntaxhighlight>
For the stretch goal we use extended precision integers instead of the usual fixed width representation, and reverse the digits numerically rather than relying on the character representation of the numbers (though it's simplest to rely on the character representation for counting the digits of the resulting primes).
 
=={{header|jq}}==
'''Works with jq and gojq, the C and Go implementations of jq'''
 
The following program will also work using jaq, the Rust
implementation of jq, provided the
adjustments described in the Addendum are made.
 
gojq supports infinite-precision integer arithmetic, but the `sqrt` algorithm
presented here is insufficient for computing the 12th Iccanobif prime
in a reasonable time.
<syntaxhighlight lang=jq>
def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
else
($n | sqrt) as $rt
| 23
| until( . > $rt or ($n % . == 0); .+2)
| . > $rt
end;
 
# Output: an indefinitely long stream of fibonacci numbers subject to
# integer arithmetic limitations if any
def fib: [0,1]|while(1;[last,add])[1];
 
def reverseNumber: tostring | explode | reverse | implode | tonumber;
 
"First 11 Iccanobif primes:",
limit(11; fib | tostring | reverseNumber | select(is_prime))
</syntaxhighlight>
{{output}}
<pre>
First 11 Iccanobif primes:
2
3
5
31
43
773
7951
64901
52057
393121
56577108676171
</pre>
 
===Addendum: jaq version===
jaq does not have indefinite-precision integer arithmetic, so
here we'll just briefly summarize the tweaks needed:
 
(1) Use `isqrt` as defined at [[Isqrt_(integer_square_root)_of_X#jq]]
but with the addition of `floor` at the end of the def of `idivide`.
 
(2) Replace reverseNumber so that leading 0s do not appear in the
reversed string:
<syntaxhighlight lang=jq>
# Input: an array of codepoints
# 48 is the codepoint of "0"
def rmLeadingZeros:
if .[0] == 48 then .[1:] | rmLeadingZeros else . end;
def reverseNumber: tostring | explode | reverse | rmLeadingZeros | implode | tonumber;
</syntaxhighlight>
 
=={{header|Julia}}==
{{trans|Python}}
<syntaxhighlight lang="julia">using Primes
 
""" Print the series of iccanobif prime numbers up to wanted """
function iccanobifs(wanted)
digbuf = zeros(Int, 11000)
fib, prev, prevprev, fcount = big"0", big"1", big"0", 0
println("First $wanted Iccanobif primes:")
while fcount < wanted
fib = prev + prevprev
prevprev = prev
prev = fib
digits!(digbuf, fib)
candidate = evalpoly(big"10", reverse(digbuf[begin:findlast(!iszero, digbuf)]))
if isprime(candidate)
fcount += 1
dlen = ndigits(candidate)
if dlen < 90
println(candidate, " ($dlen digit$(dlen == 1 ? "" : "s"))")
else
s = string(candidate)
println(s[1:30], " ... ", s[end-29:end], " ($dlen digits)")
end
end
end
end
 
iccanobifs(30)
</syntaxhighlight>{{out}}
<pre>
First 30 Iccanobif primes:
2 (1 digit)
3 (1 digit)
5 (1 digit)
31 (2 digits)
43 (2 digits)
773 (3 digits)
7951 (4 digits)
64901 (5 digits)
52057 (5 digits)
393121 (6 digits)
56577108676171 (14 digits)
940647607443258103531 (21 digits)
5237879497657222310489731409575442761 (37 digits)
9026258083384996860449366072142307801963 (40 digits)
19900335674812302969315720344396951060628175943800862267761734431012073266446403 (80 digits)
778411373629674799853537498387 ... 906414225852312097783685331923 (104 digits)
377225859015676041888905465423 ... 942640418929174997072830756131 (137 digits)
757361938948761315956093082097 ... 105343825250767238644714305761 (330 digits)
178903368473328376208382371633 ... 139766460613175300695235035913 (406 digits)
923271631017291153059188123189 ... 439342926827061468856047302507 (409 digits)
504201578106980562530763299184 ... 034364678167335124247362214481 (503 digits)
305110124747393800923565587415 ... 827995099969296158361330018201 (888 digits)
468185470426936945550027667953 ... 673037342708664543144645856321 (1020 digits)
871013478530378198843208828928 ... 472170748420128396998865227391 (1122 digits)
174516560225437653361964336594 ... 630820185220100243761843652461 (1911 digits)
489893405662883994748316933771 ... 474664296802930339234215909399 (1947 digits)
127469276849582096547381559312 ... 119580690153436989647994940101 (2283 digits)
357468265826587510126602192036 ... 869346589325010735912438195633 (3727 digits)
879871752812976577066489068488 ... 466056251048748727893681871587 (4270 digits)
818073763671137983636050093057 ... 882798314213687506007959668569 (10527 digits)
</pre>
 
=={{header|Lua}}==
isPrime routine from the [[Primality by trial division]] task.
<syntaxhighlight lang="lua">
function isPrime( n )
if n <= 1 or ( n ~= 2 and n % 2 == 0 ) then
return false
end
 
for i = 3, math.sqrt(n), 2 do
if n % i == 0 then
return false
end
end
 
return true
end
 
function reverseDigits( n )
return tonumber( string.reverse( tostring( n ) ) )
end
 
do
local pCount = 0
local prev = 0
local curr = 1
while pCount < 10 do
local nextF = prev + curr
prev = curr
curr = nextF
local rev = reverseDigits( curr )
if isPrime( rev ) then
pCount = pCount + 1
io.write( " ", rev )
end
end
end
</syntaxhighlight>
{{out}}
<pre>
2 3 5 31 43 773 7951 64901 52057 393121
</pre>
 
=={{header|MiniScript}}==
{{Trans|Lua}}
Using code frommthe [[Reverse a string]] task.
<syntaxhighlight lang="miniscript">
isPrime = function ( n )
if n <= 1 or ( n != 2 and n % 2 == 0 ) then
return false
end if
 
for i in range(3, sqrt(n), 2)
if n % i == 0 then
return false
end if
end for
 
return true
end function
 
reverseString = function( str )
revStr = ""
for i in range(str.len-1, 0)
revStr = revStr + str[i]
end for
return revStr
end function
reverseDigits = function( n )
return val( reverseString( str( n ) ) )
end function
 
pCount = 0
prev = 0
curr = 1
out = []
while pCount < 10
nextF = prev + curr
prev = curr
curr = nextF
rev = reverseDigits( curr )
if isPrime( rev ) then
pCount = pCount + 1
out.push( rev )
end if
end while
print out.join
</syntaxhighlight>
{{out}}
<pre>
2 3 5 31 43 773 7951 64901 52057 393121
</pre>
 
=={{header|Nim}}==
{{libheader|Nim-Integers}}
<syntaxhighlight lang="Nim">import std/strformat
import integers
 
func reversed(n: Integer): Integer =
## Return the "reversed" value of "n".
result = newInteger()
var n = n
while n != 0:
result = 10 * result + n mod 10
n = n div 10
 
iterator fib(): Integer =
## Yield the successive values of Fibonacci sequence.
var prev, curr = newInteger(1)
yield prev
while true:
yield curr
swap curr, prev
curr += prev
 
func compressed(str: string; size: int): string =
## Return a compressed value for long strings of digits.
if str.len <= 2 * size: str
else: &"{str[0..<size]}...{str[^size..^1]}"
 
func digitCount(s: string): string =
## Return the string which describes the number of digits.
result = $s.len & " digit"
if s.len > 1: result.add 's'
 
echo "First 25 Iccanobif primes:"
var count = 0
for n in fib():
let r = reversed(n)
if r.isPrime:
inc count
let s = $r
echo &"{count:>2}: {s.compressed(20)} ({s.digitCount()})"
if count == 25: break
</syntaxhighlight>
 
{{out}}
<pre>First 25 Iccanobif primes:
1: 2 (1 digit)
2: 3 (1 digit)
3: 5 (1 digit)
4: 31 (2 digits)
5: 43 (2 digits)
6: 773 (3 digits)
7: 7951 (4 digits)
8: 64901 (5 digits)
9: 52057 (5 digits)
10: 393121 (6 digits)
11: 56577108676171 (14 digits)
12: 940647607443258103531 (21 digits)
13: 5237879497657222310489731409575442761 (37 digits)
14: 9026258083384996860449366072142307801963 (40 digits)
15: 19900335674812302969...34431012073266446403 (80 digits)
16: 77841137362967479985...52312097783685331923 (104 digits)
17: 37722585901567604188...29174997072830756131 (137 digits)
18: 75736193894876131595...50767238644714305761 (330 digits)
19: 17890336847332837620...13175300695235035913 (406 digits)
20: 92327163101729115305...27061468856047302507 (409 digits)
21: 50420157810698056253...67335124247362214481 (503 digits)
22: 30511012474739380092...69296158361330018201 (888 digits)
23: 46818547042693694555...08664543144645856321 (1020 digits)
24: 87101347853037819884...20128396998865227391 (1122 digits)
25: 17451656022543765336...20100243761843652461 (1911 digits)
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>use v5.36;
use ntheory qw<is_prime lucasu>;
 
sub abbr ($d,$w) { my $l = length $d; $l < $w+1 ? $d : substr($d,0,$w/2) . '..' . substr($d,-$w/2) . " ($l digits)" }
 
my($n,$cnt) = (0,0);
do {
my $f = lucasu(1, -1, $n++);
my $p = join '', reverse split '', $f;
printf "%-2d: %s\n", ++$cnt, abbr($p,50) if is_prime $p;
} until $cnt == 25;</syntaxhighlight>
{{out}}
<pre>
1 : 2
2 : 3
3 : 5
4 : 31
5 : 43
6 : 773
7 : 7951
8 : 64901
9 : 52057
10: 393121
11: 56577108676171
12: 940647607443258103531
13: 5237879497657222310489731409575442761
14: 9026258083384996860449366072142307801963
15: 1990033567481230296931572..7761734431012073266446403 (80 digits)
16: 7784113736296747998535374..4225852312097783685331923 (104 digits)
17: 3772258590156760418889054..0418929174997072830756131 (137 digits)
18: 7573619389487613159560930..3825250767238644714305761 (330 digits)
19: 1789033684733283762083823..6460613175300695235035913 (406 digits)
20: 9232716310172911530591881..2926827061468856047302507 (409 digits)
21: 5042015781069805625307632..4678167335124247362214481 (503 digits)
22: 3051101247473938009235655..5099969296158361330018201 (888 digits)
23: 4681854704269369455500276..7342708664543144645856321 (1020 digits)
24: 8710134785303781988432088..0748420128396998865227391 (1122 digits)
25: 1745165602254376533619643..0185220100243761843652461 (1911 digits)
</pre>
 
=={{header|Phix}}==
{{trans|C}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<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: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">mpz</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">fib</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 30 Iccanobif primes:\n"</span><span style="color: #0000FF;">);</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><</span><span style="color: #000000;">30</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fib</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fib</span><span style="color: #0000FF;">)),</span> <span style="color: #000000;">s</span>
<span style="color: #7060A8;">mpz_set_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;"><</span><span style="color: #000000;">2</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"s"</span><span style="color: #0000FF;">)}</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d: %s (%d digit%s)\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">fib</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
First 30 Iccanobif primes:
1: 2 (1 digit)
2: 3 (1 digit)
3: 5 (1 digit)
4: 31 (2 digits)
5: 43 (2 digits)
6: 773 (3 digits)
7: 7951 (4 digits)
8: 64901 (5 digits)
9: 52057 (5 digits)
10: 393121 (6 digits)
11: 56577108676171 (14 digits)
12: 940647607443258103531 (21 digits)
13: 5237879497657222310489731409575442761 (37 digits)
14: 9026258083384996860449366072142307801963 (40 digits)
15: 19900335674812302969...34431012073266446403 (80 digits)
16: 77841137362967479985...52312097783685331923 (104 digits)
17: 37722585901567604188...29174997072830756131 (137 digits)
18: 75736193894876131595...50767238644714305761 (330 digits)
19: 17890336847332837620...13175300695235035913 (406 digits)
20: 92327163101729115305...27061468856047302507 (409 digits)
21: 50420157810698056253...67335124247362214481 (503 digits)
22: 30511012474739380092...69296158361330018201 (888 digits)
23: 46818547042693694555...08664543144645856321 (1020 digits)
24: 87101347853037819884...20128396998865227391 (1122 digits)
25: 17451656022543765336...20100243761843652461 (1911 digits)
26: 48989340566288399474...02930339234215909399 (1947 digits)
27: 12746927684958209654...53436989647994940101 (2283 digits)
28: 35746826582658751012...25010735912438195633 (3727 digits)
<killed>
</pre>
Very slow, that's as far as I was prepared to listen to it whine away...
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
As 8080 PL/M only handles 8 and 16 bit unsigned integers, this stops when the reversed number would be over 65530 and so finds only 8 Iccanobif primes.
<syntaxhighlight lang="plm">
100H: /* SHOW SOME PRIME ICCANOBIF (REVERSED FIBONACCI) NUMBERS */
 
/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
 
/* RETURNS TRUE IF N IS PRIME, FALSE OTHERWISE, USES TRIAL DIVISION */
IS$PRIME: PROCEDURE( N )BYTE;
DECLARE N ADDRESS;
DECLARE PRIME BYTE;
IF N < 3 THEN PRIME = N = 2;
ELSE IF N MOD 3 = 0 THEN PRIME = N = 3;
ELSE IF N MOD 2 = 0 THEN PRIME = 0;
ELSE DO;
DECLARE ( F, F2, TO$NEXT ) ADDRESS;
PRIME = 1;
F = 5;
F2 = 25;
TO$NEXT = 24; /* NOTE: ( 2N + 1 )^2 - ( 2N - 1 )^2 = 8N */
DO WHILE F2 <= N AND PRIME;
PRIME = N MOD F <> 0;
F = F + 2;
F2 = F2 + TO$NEXT;
TO$NEXT = TO$NEXT + 8;
END;
END;
RETURN PRIME;
END IS$PRIME;
 
/* TASK */
 
DECLARE ( PREV, CURR, NEXT, V, R ) ADDRESS;
DECLARE MORE BYTE;
 
PREV = 0;
CURR = 1;
MORE = 1;
DO WHILE MORE;
NEXT = PREV + CURR;
/* REVERSE THE DIGITS OF NEXT, STOP IF THE RESULT WOULD BE > 65530 */
R = 0;
V = NEXT;
DO WHILE V > 0 AND MORE;
R = ( R * 10 ) + V MOD 10;
V = V / 10;
IF R > 6553 AND V <> 0 THEN MORE = 0;
END;
IF MORE THEN DO;
/* THE REVERSE OF N WILL FIT 16 BITS */
PREV = CURR;
CURR = NEXT;
IF IS$PRIME( R ) THEN DO;
CALL PR$CHAR( ' ' );CALL PR$NUMBER( R );
END;
END;
END;
 
EOF
</syntaxhighlight>
{{out}}
<pre>
2 3 5 31 43 773 7951 64901
</pre>
 
=={{header|Python}}==
{{trans|Wren}}
<syntaxhighlight lang="python">""" rosettacode.org/wiki/Iccanobif_primes """
 
from sympy import isprime
 
 
def iccanobifs(wanted):
""" Print the series of iccanobif prime numbers up to wanted """
fib, prev, prevprev, fcount = 0, 1, 0, 0
print('First 30 Iccanobif primes:')
while fcount < wanted:
fib = prev + prevprev
prevprev = prev
prev = fib
dig = [int(c) for c in str(fib)]
candidate = sum(n * 10**i for i, n in enumerate(dig))
if isprime(candidate):
fcount += 1
dlen = len(str(candidate))
if dlen < 90:
print(candidate, f"({dlen} digit{'' if dlen == 1 else 's'})")
else:
s = str(candidate)
print(s[:30], "...", s[-29:], f'({dlen} digits)')
 
 
iccanobifs(30)
</syntaxhighlight>{{out}}
<pre>
First 30 Iccanobif primes:
2 (1 digit)
3 (1 digit)
5 (1 digit)
31 (2 digits)
43 (2 digits)
773 (3 digits)
7951 (4 digits)
64901 (5 digits)
52057 (5 digits)
393121 (6 digits)
56577108676171 (14 digits)
940647607443258103531 (21 digits)
5237879497657222310489731409575442761 (37 digits)
9026258083384996860449366072142307801963 (40 digits)
19900335674812302969315720344396951060628175943800862267761734431012073266446403 (80 digits)
778411373629674799853537498387 ... 06414225852312097783685331923 (104 digits)
377225859015676041888905465423 ... 42640418929174997072830756131 (137 digits)
757361938948761315956093082097 ... 05343825250767238644714305761 (330 digits)
178903368473328376208382371633 ... 39766460613175300695235035913 (406 digits)
923271631017291153059188123189 ... 39342926827061468856047302507 (409 digits)
504201578106980562530763299184 ... 34364678167335124247362214481 (503 digits)
305110124747393800923565587415 ... 27995099969296158361330018201 (888 digits)
468185470426936945550027667953 ... 73037342708664543144645856321 (1020 digits)
871013478530378198843208828928 ... 72170748420128396998865227391 (1122 digits)
174516560225437653361964336594 ... 30820185220100243761843652461 (1911 digits)
489893405662883994748316933771 ... 74664296802930339234215909399 (1947 digits)
127469276849582096547381559312 ... 19580690153436989647994940101 (2283 digits)
357468265826587510126602192036 ... 69346589325010735912438195633 (3727 digits)
879871752812976577066489068488 ... 66056251048748727893681871587 (4270 digits)
^C (took too long)
</pre>
 
=={{header|Quackery}}==
 
As with the other solutions, finds Fibonacci emirps (Fibonacci numbers which are primes when reversed) and reverses them.
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<code>from</code>, <code>incr</code> and <code>end</code> are defined at [[Loops/Increment loop index within loop body#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ 0
[ swap 10 /mod
rot 10 * +
over 0 = until ]
nip ] is revnum ( n --> n )
 
[] 1 1 from
[ dup revnum isprime if
[ tuck revnum
join swap ]
index swap incr
over size 10 = if end ]
drop echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 2 3 5 31 43 773 7951 64901 52057 393121 ]</pre>
 
=={{header|Raku}}==
Line 49 ⟶ 1,233:
26: 48989340566288399474..02930339234215909399 (digits: 1947)
27: 12746927684958209654..53436989647994940101 (digits: 2283)
28: 35746826582658751012..25010735912438195633 (digits: 3727)
29: 87987175281297657706..48748727893681871587 (digits: 4270)
30: 81807376367113798363..13687506007959668569 (digits: 10527)</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ 0
'''WHILE''' OVER '''REPEAT'''
SWAP 10 IDIV2
ROT 10 * +
'''END''' NIP
≫ '<span style="color:blue">REVINT</span>' STO
≪ 2 * → max
≪ { } 0 1
DO
SWAP OVER +
DUP <span style="color:blue">REVINT</span>
'''IF''' DUP ISPRIME? '''THEN'''
DUP →STR SIZE 5 ROLL ROT + SWAP + UNROT
'''ELSE''' DROP '''END'''
'''UNTIL''' 3 PICK SIZE max ≥ '''END''' DROP2
≫ ≫ '<span style="color:blue">ICCAN</span>' STO
 
15 <span style="color:blue">ICCAN</span>
{{out}}
<pre>
1: {2 1. 3 1. 5 1. 31 2. 43 2. 773 3. 7951 4. 64901 5. 52057 5. 393121 6. 56577108676171 14. 940647607443258103531 21. 5237879497657222310489731409575442761 37. 9026258083384996860449366072142307801963 40. 19900335674812302969315720344396951060628175943800862267761734431012073266446403 80.}
</pre>
Runs in 30 minutes on a HP-50g.
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var count = 25
var index = 0
 
for n in (1..Inf) {
var t = n.fib.flip -> is_prob_prime || next
var s = Str(t)
s = "#{s.first(20)}..#{s.last(20)} (#{s.len} digits)" if (s.len>50)
say "#{'%2d' % ++index}: #{s}"
count == index && break
}</syntaxhighlight>
 
{{out}}
<pre>
1: 2
2: 3
3: 5
4: 31
5: 43
6: 773
7: 7951
8: 64901
9: 52057
10: 393121
11: 56577108676171
12: 940647607443258103531
13: 5237879497657222310489731409575442761
14: 9026258083384996860449366072142307801963
15: 19900335674812302969..34431012073266446403 (80 digits)
16: 77841137362967479985..52312097783685331923 (104 digits)
17: 37722585901567604188..29174997072830756131 (137 digits)
18: 75736193894876131595..50767238644714305761 (330 digits)
19: 17890336847332837620..13175300695235035913 (406 digits)
20: 92327163101729115305..27061468856047302507 (409 digits)
21: 50420157810698056253..67335124247362214481 (503 digits)
22: 30511012474739380092..69296158361330018201 (888 digits)
23: 46818547042693694555..08664543144645856321 (1020 digits)
24: 87101347853037819884..20128396998865227391 (1122 digits)
25: 17451656022543765336..20100243761843652461 (1911 digits)
</pre>
 
Line 54 ⟶ 1,308:
{{libheader|Wren-gmp}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpz
import "./fmt" for Fmt
 
Line 62 ⟶ 1,316:
var curr = Mpz.one
var count = 0
System.print("First 2730 Iccanobif primes:")
while (count < 2730) {
fib.add(curr, prev)
var fs = fib.toString
Line 78 ⟶ 1,332:
{{out}}
<pre>
First 2730 Iccanobif primes:
1: 2 (1 digits)
2: 3 (1 digits)
Line 106 ⟶ 1,360:
26: 48989340566288399474...02930339234215909399 (1947 digits)
27: 12746927684958209654...53436989647994940101 (2283 digits)
28: 35746826582658751012...25010735912438195633 (3727 digits)
29: 87987175281297657706...48748727893681871587 (4270 digits)
30: 81807376367113798363...13687506007959668569 (10527 digits)
</pre>
2,747

edits