Repunit primes: Difference between revisions

Add PARI/GP implementation
(Added a Scheme implementation.)
(Add PARI/GP implementation)
 
(14 intermediate revisions by 10 users not shown)
Line 48:
 
 
 
=={{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 #
INT max base = 16;
INT max repunit digits = 1000;
PR precision 3000 PR # set precision of LONG LONG INT #
# 16^1000 has ~1200 digits but the primality test needs more #
PR read "primes.incl.a68" PR # include prime utilities #
[]BOOL prime = PRIMESIEVE max repunit digits;
FOR base FROM 2 TO max base DO
LONG LONG INT repunit := 1;
print( ( whole( base, -2 ), ":" ) );
FOR digits TO max repunit digits DO
IF prime[ digits ] THEN
IF is probably prime( repunit ) THEN
# found a prime repunit in the current base #
print( ( " ", whole( digits, 0 ) ) )
FI
FI;
repunit *:= base +:= 1
OD;
print( ( newline ) )
OD
END
</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|Arturo}}==
 
<syntaxhighlight lang="arturo">getRepunit: function [n,b][
result: 1
loop 1..dec n 'z ->
result: result + b^z
return result
]
 
loop 2..16 'base [
print [
pad (to :string base) ++ ":" 4
join.with:", " to [:string] select 2..1001 'x ->
and? -> prime? x
-> prime? getRepunit x base
]
]</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|C++}}==
{{libheader|GMP}}
{{libheader|Primesieve}}
<langsyntaxhighlight lang="cpp">#include <future>
#include <iomanip>
#include <iostream>
Line 86 ⟶ 168:
std::cout << '\n';
}
}</langsyntaxhighlight>
 
{{out}}
Line 131 ⟶ 213:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Repunit primes. Nigel Galloway: January 24th., 2022
let rUnitP(b:int)=let b=bigint b in primes32()|>Seq.takeWhile((>)1000)|>Seq.map(fun n->(n,((b**n)-1I)/(b-1I)))|>Seq.filter(fun(_,n)->Open.Numeric.Primes.MillerRabin.IsProbablePrime &n)|>Seq.map fst
[2..16]|>List.iter(fun n->printf $"Base %d{n}: "; rUnitP(n)|>Seq.iter(printf "%d "); printfn "")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 160 ⟶ 242:
{{libheader|Go-rcu}}
Took 11 minutes 25 seconds which is much the same as Wren's GMP wrapper.
<langsyntaxhighlight lang="go">package main
 
import (
Line 183 ⟶ 265:
fmt.Printf("Base %2d: %v\n", b, rPrimes)
}
}</langsyntaxhighlight>
 
{{out}}
Line 222 ⟶ 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>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
repunitprimeinbase(n, base) = isprime(evalpoly(BigInt(base), [1 for _ in 1:n]))
Line 232 ⟶ 403:
println(rpad("Base $b:", 9), filter(n -> repunitprimeinbase(n, b), 1:2700))
end
</langsyntaxhighlight>{{out}}
<pre>
Base 2: [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281]
Line 276 ⟶ 447:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[RepUnitPrimeQ]
RepUnitPrimeQ[b_][n_] := PrimeQ[FromDigits[ConstantArray[1, n], b]]
ClearAll[RepUnitPrimeQ]
Line 285 ⟶ 456:
{b, 2, 16}
]
</syntaxhighlight>
</lang>
Searching bases 2–16 for repunits of size 2700:
{{out}}
Line 303 ⟶ 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}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory <is_prime fromdigits>;
Line 316 ⟶ 547:
for my $base (2..16) {
printf "Base %2d: %s\n", $base, join ' ', grep { is_prime $_ and is_prime fromdigits(('1'x$_), $base) and " $_" } 1..$limit
}</langsyntaxhighlight>
{{out}}
<pre>Repunit prime digits (up to 1000) in:
Line 336 ⟶ 567:
 
=={{header|Phix}}==
<!--<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 367 ⟶ 598:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
Note the first half (base<=16) is from a {2700,16} run and the rest (base>16) from a {1000,36} run.
Line 409 ⟶ 640:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from sympy import isprime
for b in range(2, 17):
print(b, [n for n in range(2, 1001) if isprime(n) and isprime(int('1'*n, base=b))])</langsyntaxhighlight>
{{out}}
<pre>2 [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607]
Line 428 ⟶ 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}}==
 
<syntaxhighlight lang="raku" perl6line>my $limit = 2700;
 
say "Repunit prime digits (up to $limit) in:";
Line 437 ⟶ 710:
.put for (2..16).hyper(:1batch).map: -> $base {
$base.fmt("Base %2d: ") ~ (1..$limit).grep(&is-prime).grep( (1 x *).parse-base($base).is-prime )
}</langsyntaxhighlight>
{{out}}
<pre>Repunit prime digits (up to 2700) in:
Line 476 ⟶ 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 485 ⟶ 819:
<br />
'''Primality Test'''
<langsyntaxhighlight lang="scheme">; Test whether any integer is a probable prime.
(define prime<probably>?
(lambda (n)
Line 526 ⟶ 860:
(or (= n 2)
(and (odd? n)
(pseudoprime? n 50))))))</langsyntaxhighlight>
'''The Task'''
<langsyntaxhighlight lang="scheme">; Return list of the Repunit Primes in the given base up to the given limit.
(define repunit_primes
(lambda (base limit)
Line 546 ⟶ 880:
(do ((base 2 (1+ base)))
((> base max-base))
(printf "Base ~2d: ~a~%" base (repunit_primes base max-digits))))</langsyntaxhighlight>
{{out}}
<pre>
Line 568 ⟶ 902:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var limit = 1000
 
say "Repunit prime digits (up to #{limit}) in:"
Line 575 ⟶ 909:
printf("Base %2d: %s\n", n,
{|k| is_prime((n**k - 1) / (n-1)) }.grep(1..limit))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 607 ⟶ 941:
 
Still takes a while - 11 minutes 20 seconds to get up to base 36 with a limit of 2700.
<langsyntaxhighlight ecmascriptlang="wren">/* repunit_primesRepunit_primes.wren */
 
import "./gmp" for Mpz
Line 624 ⟶ 958:
}
Fmt.print("Base $2d: $n", b, rPrimes)
}</langsyntaxhighlight>
 
{{out}}
337

edits