Repunit primes: Difference between revisions

Content added Content deleted
(Added a Scheme implementation.)
m (syntax highlighting fixup automation)
Line 52: Line 52:
{{libheader|GMP}}
{{libheader|GMP}}
{{libheader|Primesieve}}
{{libheader|Primesieve}}
<lang cpp>#include <future>
<syntaxhighlight lang="cpp">#include <future>
#include <iomanip>
#include <iomanip>
#include <iostream>
#include <iostream>
Line 86: Line 86:
std::cout << '\n';
std::cout << '\n';
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 131: Line 131:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Repunit primes. Nigel Galloway: January 24th., 2022
// 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
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 "")
[2..16]|>List.iter(fun n->printf $"Base %d{n}: "; rUnitP(n)|>Seq.iter(printf "%d "); printfn "")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 160: Line 160:
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
Took 11 minutes 25 seconds which is much the same as Wren's GMP wrapper.
Took 11 minutes 25 seconds which is much the same as Wren's GMP wrapper.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 183: Line 183:
fmt.Printf("Base %2d: %v\n", b, rPrimes)
fmt.Printf("Base %2d: %v\n", b, rPrimes)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 225: Line 225:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


repunitprimeinbase(n, base) = isprime(evalpoly(BigInt(base), [1 for _ in 1:n]))
repunitprimeinbase(n, base) = isprime(evalpoly(BigInt(base), [1 for _ in 1:n]))
Line 232: Line 232:
println(rpad("Base $b:", 9), filter(n -> repunitprimeinbase(n, b), 1:2700))
println(rpad("Base $b:", 9), filter(n -> repunitprimeinbase(n, b), 1:2700))
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Base 2: [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281]
Base 2: [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281]
Line 276: Line 276:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[RepUnitPrimeQ]
<syntaxhighlight lang="mathematica">ClearAll[RepUnitPrimeQ]
RepUnitPrimeQ[b_][n_] := PrimeQ[FromDigits[ConstantArray[1, n], b]]
RepUnitPrimeQ[b_][n_] := PrimeQ[FromDigits[ConstantArray[1, n], b]]
ClearAll[RepUnitPrimeQ]
ClearAll[RepUnitPrimeQ]
Line 285: Line 285:
{b, 2, 16}
{b, 2, 16}
]
]
</syntaxhighlight>
</lang>
Searching bases 2–16 for repunits of size 2700:
Searching bases 2–16 for repunits of size 2700:
{{out}}
{{out}}
Line 306: Line 306:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use ntheory <is_prime fromdigits>;
use ntheory <is_prime fromdigits>;
Line 316: Line 316:
for my $base (2..16) {
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
printf "Base %2d: %s\n", $base, join ' ', grep { is_prime $_ and is_prime fromdigits(('1'x$_), $base) and " $_" } 1..$limit
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Repunit prime digits (up to 1000) in:
<pre>Repunit prime digits (up to 1000) in:
Line 336: Line 336:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: Line 367:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
Note the first half (base<=16) is from a {2700,16} run and the rest (base>16) from a {1000,36} run.
Note the first half (base<=16) is from a {2700,16} run and the rest (base>16) from a {1000,36} run.
Line 409: Line 409:


=={{header|Python}}==
=={{header|Python}}==
<lang python>from sympy import isprime
<syntaxhighlight lang="python">from sympy import isprime
for b in range(2, 17):
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))])</lang>
print(b, [n for n in range(2, 1001) if isprime(n) and isprime(int('1'*n, base=b))])</syntaxhighlight>
{{out}}
{{out}}
<pre>2 [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607]
<pre>2 [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607]
Line 431: Line 431:
=={{header|Raku}}==
=={{header|Raku}}==


<lang perl6>my $limit = 2700;
<syntaxhighlight lang="raku" line>my $limit = 2700;


say "Repunit prime digits (up to $limit) in:";
say "Repunit prime digits (up to $limit) in:";
Line 437: Line 437:
.put for (2..16).hyper(:1batch).map: -> $base {
.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 )
$base.fmt("Base %2d: ") ~ (1..$limit).grep(&is-prime).grep( (1 x *).parse-base($base).is-prime )
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Repunit prime digits (up to 2700) in:
<pre>Repunit prime digits (up to 2700) in:
Line 485: Line 485:
<br />
<br />
'''Primality Test'''
'''Primality Test'''
<lang scheme>; Test whether any integer is a probable prime.
<syntaxhighlight lang="scheme">; Test whether any integer is a probable prime.
(define prime<probably>?
(define prime<probably>?
(lambda (n)
(lambda (n)
Line 526: Line 526:
(or (= n 2)
(or (= n 2)
(and (odd? n)
(and (odd? n)
(pseudoprime? n 50))))))</lang>
(pseudoprime? n 50))))))</syntaxhighlight>
'''The Task'''
'''The Task'''
<lang scheme>; Return list of the Repunit Primes in the given base up to the given limit.
<syntaxhighlight lang="scheme">; Return list of the Repunit Primes in the given base up to the given limit.
(define repunit_primes
(define repunit_primes
(lambda (base limit)
(lambda (base limit)
Line 546: Line 546:
(do ((base 2 (1+ base)))
(do ((base 2 (1+ base)))
((> base max-base))
((> base max-base))
(printf "Base ~2d: ~a~%" base (repunit_primes base max-digits))))</lang>
(printf "Base ~2d: ~a~%" base (repunit_primes base max-digits))))</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 568: Line 568:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var limit = 1000
<syntaxhighlight lang="ruby">var limit = 1000


say "Repunit prime digits (up to #{limit}) in:"
say "Repunit prime digits (up to #{limit}) in:"
Line 575: Line 575:
printf("Base %2d: %s\n", n,
printf("Base %2d: %s\n", n,
{|k| is_prime((n**k - 1) / (n-1)) }.grep(1..limit))
{|k| is_prime((n**k - 1) / (n-1)) }.grep(1..limit))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 607: Line 607:


Still takes a while - 11 minutes 20 seconds to get up to base 36 with a limit of 2700.
Still takes a while - 11 minutes 20 seconds to get up to base 36 with a limit of 2700.
<lang ecmascript>/* repunit_primes.wren */
<syntaxhighlight lang="ecmascript">/* repunit_primes.wren */


import "./gmp" for Mpz
import "./gmp" for Mpz
Line 624: Line 624:
}
}
Fmt.print("Base $2d: $n", b, rPrimes)
Fmt.print("Base $2d: $n", b, rPrimes)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}