Iccanobif primes: Difference between revisions

Added Sidef
m (→‎{{header|Lua}}: Avoid redefining "next")
(Added Sidef)
 
(9 intermediate revisions by 8 users not shown)
Line 139:
;; description: « returns a summary of a numeric string
s: size n
if s > 20 -> n: ((take n 10)++"...")++drop n .times:s-10 n
n ++ ~" (|s| digits)"
]
Line 473:
<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>
 
Line 537 ⟶ 621:
 
=={{header|J}}==
 
Implementation:
<syntaxhighlight lang="j"> (#~ 1&p:) |.&.":"0 (, _2 +/@{. ])^:(70) 1
 
2 3 5 31 43 773 7951 64901 52057 393121 56577108676171</syntaxhighlight>
<syntaxhighlight lang=J> (#~ 1 p:])|.&.|:"0 (, _2 +/@{. ])^:70]1
In other words: 70 numbers from the Fibonacci sequence, reverse their digits, and keep those which are prime.
2 3 5 13 89 233 1597 28657 514229 433494437 2971215073
</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
 
<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).
 
Line 729 ⟶ 807:
<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>
 
Line 805 ⟶ 934:
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>use strictv5.36;
use warnings;
use ntheory qw<is_prime lucasu>;
 
Line 1,046 ⟶ 1,174:
^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 1,082 ⟶ 1,236:
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>
 
=={{header|Wren}}==
{{libheader|Wren-gmp}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpz
import "./fmt" for Fmt
 
2,747

edits