Jump to content

Repunit primes: Difference between revisions

m
syntax highlighting fixup automation
(Added a Scheme implementation.)
m (syntax highlighting fixup automation)
Line 52:
{{libheader|GMP}}
{{libheader|Primesieve}}
<langsyntaxhighlight lang="cpp">#include <future>
#include <iomanip>
#include <iostream>
Line 86:
std::cout << '\n';
}
}</langsyntaxhighlight>
 
{{out}}
Line 131:
=={{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:
{{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:
fmt.Printf("Base %2d: %v\n", b, rPrimes)
}
}</langsyntaxhighlight>
 
{{out}}
Line 225:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
repunitprimeinbase(n, base) = isprime(evalpoly(BigInt(base), [1 for _ in 1:n]))
Line 232:
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:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[RepUnitPrimeQ]
RepUnitPrimeQ[b_][n_] := PrimeQ[FromDigits[ConstantArray[1, n], b]]
ClearAll[RepUnitPrimeQ]
Line 285:
{b, 2, 16}
]
</syntaxhighlight>
</lang>
Searching bases 2–16 for repunits of size 2700:
{{out}}
Line 306:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory <is_prime fromdigits>;
Line 316:
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:
 
=={{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:
<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:
 
=={{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 431:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>my $limit = 2700;
 
say "Repunit prime digits (up to $limit) in:";
Line 437:
.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 485:
<br />
'''Primality Test'''
<langsyntaxhighlight lang="scheme">; Test whether any integer is a probable prime.
(define prime<probably>?
(lambda (n)
Line 526:
(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:
(do ((base 2 (1+ base)))
((> base max-base))
(printf "Base ~2d: ~a~%" base (repunit_primes base max-digits))))</langsyntaxhighlight>
{{out}}
<pre>
Line 568:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var limit = 1000
 
say "Repunit prime digits (up to #{limit}) in:"
Line 575:
printf("Base %2d: %s\n", n,
{|k| is_prime((n**k - 1) / (n-1)) }.grep(1..limit))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 607:
 
Still takes a while - 11 minutes 20 seconds to get up to base 36 with a limit of 2700.
<langsyntaxhighlight lang="ecmascript">/* repunit_primes.wren */
 
import "./gmp" for Mpz
Line 624:
}
Fmt.print("Base $2d: $n", b, rPrimes)
}</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.