Repunit primes: Difference between revisions

Add PARI/GP implementation
(Added Algol 68)
(Add PARI/GP implementation)
 
(11 intermediate revisions by 8 users not shown)
Line 51:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">
BEGIN # find repunit (all digits are 1 ) primes in various bases #
Line 303 ⟶ 304:
Base 35: [313 1297]
Base 36: [2]
</pre>
 
=={{header|J}}==
Slow (runs a few minutes):
<syntaxhighlight lang="j">repunitp=. 1 p: ^&x: %&<: [
 
(2 + i. 15) ([ ;"0 repunitp"0/ <@# ]) i.&.(p:inv) 1000</syntaxhighlight>
{{out}}
<pre>
┌──┬─────────────────────────────────────────┐
│2 │2 3 5 7 13 17 19 31 61 89 107 127 521 607│
├──┼─────────────────────────────────────────┤
│3 │3 7 13 71 103 541 │
├──┼─────────────────────────────────────────┤
│4 │2 │
├──┼─────────────────────────────────────────┤
│5 │3 7 11 13 47 127 149 181 619 929 │
├──┼─────────────────────────────────────────┤
│6 │2 3 7 29 71 127 271 509 │
├──┼─────────────────────────────────────────┤
│7 │5 13 131 149 │
├──┼─────────────────────────────────────────┤
│8 │3 │
├──┼─────────────────────────────────────────┤
│9 │ │
├──┼─────────────────────────────────────────┤
│10│2 19 23 317 │
├──┼─────────────────────────────────────────┤
│11│17 19 73 139 907 │
├──┼─────────────────────────────────────────┤
│12│2 3 5 19 97 109 317 353 701 │
├──┼─────────────────────────────────────────┤
│13│5 7 137 283 883 991 │
├──┼─────────────────────────────────────────┤
│14│3 7 19 31 41 │
├──┼─────────────────────────────────────────┤
│15│3 43 73 487 │
├──┼─────────────────────────────────────────┤
│16│2 │
└──┴─────────────────────────────────────────┘
</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.math.BigInteger;
 
public final class RepunitPrimes {
 
public static void main(String[] aArgs) {
final int limit = 2_700;
System.out.println("Repunit primes, up to " + limit + " digits, in:");
for ( int base = 2; base <= 16; base++ ) {
System.out.print(String.format("%s%2s%s", "Base ", base, ": "));
String repunit = "";
while ( repunit.length() < limit ) {
repunit += "1";
if ( BigInteger.valueOf(repunit.length()).isProbablePrime(CERTAINTY_LEVEL) ) {
BigInteger value = new BigInteger(repunit, base);
if ( value.isProbablePrime(CERTAINTY_LEVEL) ) {
System.out.print(repunit.length() + " ");
}
}
}
System.out.println();
}
}
private static final int CERTAINTY_LEVEL = 20;
 
}
</syntaxhighlight>
{{ out }}
<pre>
Repunit primes, up to 2700 digits, in:
Base 2: 2 3 5 7 13 17 19 31 61 89 107 127 521 607 1279 2203 2281
Base 3: 3 7 13 71 103 541 1091 1367 1627
Base 4: 2
Base 5: 3 7 11 13 47 127 149 181 619 929
Base 6: 2 3 7 29 71 127 271 509 1049
Base 7: 5 13 131 149 1699
Base 8: 3
Base 9:
Base 10: 2 19 23 317 1031
Base 11: 17 19 73 139 907 1907 2029
Base 12: 2 3 5 19 97 109 317 353 701
Base 13: 5 7 137 283 883 991 1021 1193
Base 14: 3 7 19 31 41 2687
Base 15: 3 43 73 487 2579
Base 16: 2
</pre>
 
Line 384 ⟶ 474:
Base 15: {3,43,73,487,2579}
Base 16: {2}</pre>
 
=={{header|Nim}}==
{{libheader|Nim-Integers}}
<syntaxhighlight lang="Nim">
import std/strformat
import integers
 
for base in 2..16:
stdout.write &"{base:>2}:"
var rep = ""
while true:
rep.add '1'
if rep.len > 2700: break
if not rep.len.isPrime: continue
let val = newInteger(rep, base)
if val.isPrime():
stdout.write ' ', rep.len
echo()
</syntaxhighlight>
 
{{out}}
<pre> 2: 2 3 5 7 13 17 19 31 61 89 107 127 521 607 1279 2203 2281
3: 3 7 13 71 103 541 1091 1367 1627
4: 2
5: 3 7 11 13 47 127 149 181 619 929
6: 2 3 7 29 71 127 271 509 1049
7: 5 13 131 149 1699
8: 3
9:
10: 2 19 23 317 1031
11: 17 19 73 139 907 1907 2029
12: 2 3 5 19 97 109 317 353 701
13: 5 7 137 283 883 991 1021 1193
14: 3 7 19 31 41 2687
15: 3 43 73 487 2579
16: 2
</pre>
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
default(parisizemax, "128M")
default(parisize, "64M");
 
 
repunitprimeinbase(n, base) = {
repunit = sum(i=0, n-1, base^i); /* Construct the repunit */
return(isprime(repunit)); /* Check if it's prime */
}
 
{
for(b=2, 40,
print("Base ", b, ": ");
for(n=1, 2700,
if(repunitprimeinbase(n, b), print1(n, " "))
);
print(""); /* Print a newline */
)
}
</syntaxhighlight>
 
=={{header|Perl}}==
Line 509 ⟶ 659:
15 [3, 43, 73, 487]
16 [2]</pre>
 
=={{header|Quackery}}==
 
<code>prime</code> is defined at [[Miller–Rabin primality test#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ base put
1 temp put
[] 1 1000 times
[ temp share prime if
[ dup prime if
[ dip [ i^ 1+ join ] ] ]
base share * 1+
1 temp tally ]
drop
temp release
base release ] is repunitprimes ( n --> [ )
 
15 times
[ i^ 2 +
dup 10 < if sp
dup echo
say ": "
repunitprimes echo
cr ]</syntaxhighlight>
 
{{out}}
 
<pre> 2: [ 2 3 5 7 13 17 19 31 61 89 107 127 521 607 ]
3: [ 3 7 13 71 103 541 ]
4: [ 2 ]
5: [ 3 7 11 13 47 127 149 181 619 929 ]
6: [ 2 3 7 29 71 127 271 509 ]
7: [ 5 13 131 149 ]
8: [ 3 ]
9: [ ]
10: [ 2 19 23 317 ]
11: [ 17 19 73 139 907 ]
12: [ 2 3 5 19 97 109 317 353 701 ]
13: [ 5 7 137 283 883 991 ]
14: [ 3 7 19 31 41 ]
15: [ 3 43 73 487 ]
16: [ 2 ]</pre>
 
=={{header|Raku}}==
Line 557 ⟶ 749:
Base 35: 313 1297
Base 36: 2</pre>
=={{header|RPL}}==
{{works with|HP|49}}
≪ 0 1 4 ROLL '''START''' OVER * 1 + '''NEXT''' NIP
≫ '<span style="color:blue">REPUB</span>' STO <span style="color:grey">@ ( n b → Rb(n) )</span>
≪ 16 2 '''FOR''' b
{ }
2 1000 '''FOR''' n
'''IF''' n b <span style="color:blue">REPUB</span> ISPRIME? '''THEN''' n + '''END'''
'''NEXT'''
-1 '''STEP'''
"Done."
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
16: {2}
15: {3 43 73 487}
14: {3 7 19 31 41}
13: {5 7 137 283 883 991}
12: {2 3 5 19 97 109 317 353 701}
11: {17 19 73 139 907}
10: {2 19 23 317}
9: { }
8: {3}
7: {5 13 131 149}
6: {2 3 7 29 71 127 271 509}
5: {3 7 11 13 47 127 149 181 619 929}
4: {2}
3: {3 7 13 71 103 541}
2: {2 3 5 7 13 17 19 31 61 89 107 127 521 607}
1: "Done."
</pre>
 
=={{header|Ruby}}==
Ruby's standard lib 'Prime' is boring slow for this. GMP to the rescue. GMP bindings is a gem, not in std lib.
<syntaxhighlight lang="ruby">require 'prime'
require 'gmp'
 
(2..16).each do |base|
res = Prime.each(1000).select {|n| GMP::Z(("1" * n).to_i(base)).probab_prime? > 0}
puts "Base #{base}: #{res.join(" ")}"
end
</syntaxhighlight>
{{out}}
<pre>Base 2: 2 3 5 7 13 17 19 31 61 89 107 127 521 607
Base 3: 3 7 13 71 103 541
Base 4: 2
Base 5: 3 7 11 13 47 127 149 181 619 929
Base 6: 2 3 7 29 71 127 271 509
Base 7: 5 13 131 149
Base 8: 3
Base 9:
Base 10: 2 19 23 317
Base 11: 17 19 73 139 907
Base 12: 2 3 5 19 97 109 317 353 701
Base 13: 5 7 137 283 883 991
Base 14: 3 7 19 31 41
Base 15: 3 43 73 487
Base 16: 2
</pre>
 
=={{header|Scheme}}==
{{works with|Chez Scheme}}
Line 688 ⟶ 941:
 
Still takes a while - 11 minutes 20 seconds to get up to base 36 with a limit of 2700.
<syntaxhighlight lang="ecmascriptwren">/* repunit_primesRepunit_primes.wren */
 
import "./gmp" for Mpz
337

edits