Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Wagstaff_primes

View file

@ -0,0 +1,22 @@
;Definition
A ''Wagstaff prime'' is a prime number of the form ''(2^p + 1)/3'' where the exponent ''p'' is an odd prime.
;Example
(2^5 + 1)/3 = 11 is a Wagstaff prime because both 5 and 11 are primes.
;Task
Find and show here the first ''10'' Wagstaff primes and their corresponding exponents ''p''.
;Stretch (requires arbitrary precision integers)
Find and show here the exponents ''p'' corresponding to the next ''14'' Wagstaff primes (not the primes themselves) and any more that you have the patience for.
When testing for primality, you may use a method which determines that a large number is probably prime with reasonable certainty.
;Note
It can be shown (see talk page) that ''(2^p + 1)/3'' is always integral if ''p'' is odd. So there's no need to check for that prior to checking for primality.
;References
* [[wp:Wagstaff prime|Wikipedia - Wagstaff prime]]
* [[oeis:A000979|OEIS:A000979 - Wagstaff primes]]
<br><br>

View file

@ -0,0 +1,43 @@
BEGIN # find some Wagstaff primes: primes of the form ( 2^p + 1 ) / 3 #
# where p is an odd prime #
INT max wagstaff = 10; # number of Wagstaff primes to find #
INT w count := 0; # numbdr of Wagstaff primes found so far #
# sieve the primes up to 200, hopefully enough... #
[ 0 : 200 ]BOOL primes;
primes[ 0 ] := primes[ 1 ] := FALSE;
primes[ 2 ] := TRUE;
FOR i FROM 3 BY 2 TO UPB primes DO primes[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO UPB primes DO primes[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( UPB primes ) DO
IF primes[ i ] THEN
FOR s FROM i * i BY i + i TO UPB primes DO primes[ s ] := FALSE OD
FI
OD;
# attempt to find the Wagstaff primes #
LONG INT power of 2 := 2; # 2^1 #
FOR p FROM 3 BY 2 WHILE w count < max wagstaff DO
power of 2 *:= 4;
IF primes[ p ] THEN
LONG INT w := ( power of 2 + 1 ) OVER 3;
# check w is prime - trial division #
BOOL is prime := TRUE;
LONG INT n := 3;
WHILE n * n <= w AND is prime DO
is prime := w MOD n /= 0;
n +:= 2
OD;
IF is prime THEN
# have another Wagstaff prime #
w count +:= 1;
print( ( whole( w count, -2 )
, ": "
, whole( p, -4 )
, ": "
, whole( w, 0 )
, newline
)
)
FI
FI
OD
END

View file

@ -0,0 +1,81 @@
begin % find some Wagstaff primes: primes of the form ( 2^p + 1 ) / 3 %
% where p is an odd prime %
integer wCount;
% returns true if d exactly divides v, false otherwise %
logical procedure divides( long real value d, v ) ;
begin
long real q, p10;
q := v / d;
p10 := 1;
while p10 * 10 < q do begin
p10 := p10 * 10
end while_p10_lt_q ;
while p10 >= 1 do begin
while q >= p10 do q := q - p10;
p10 := p10 / 10
end while_p10_ge_1 ;
q = 0
end divides ;
% prints v as a 14 digit integer number %
procedure printI14( long real value v ) ;
begin
integer iv;
long real r;
r := abs( v );
if v < 0 then writeon( s_w := 0, "-" );
iv := truncate( r / 1000000 );
if iv < 1 then begin
writeon( i_w := 8, s_w := 0, " ", truncate( r ) )
end
else begin
string(6) sValue;
writeon( i_w := 8, s_w := 0, iv );
iv := truncate( abs( r ) - ( iv * 1000000.0 ) );
for sPos := 5 step -1 until 0 do begin
sValue( sPos // 1 ) := code( ( iv rem 10 ) + decode( "0" ) );
iv := iv div 10
end for_sPos;
writeon( s_w := 0, sValue )
end if_iv_lt_1__
end printI ;
wCount := 0;
% find the Wagstaff primes using long reals to hold the numbers, which %
% accurately represent integers up to 2^53, so only consider primes < 53 %
for p := 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 do begin
long real w, powerOfTwo;
logical isPrime;
powerOfTwo := 1;
for i := 1 until p do powerOfTwo := powerOfTwo * 2;
w := ( powerOfTwo + 1 ) / 3;
isPrime := not divides( 2, w );
if isPrime then begin
integer f, toNext;
long real f2;
f := 3;
f2 := 9;
toNext := 16;
while isPrime and f2 <= w do begin
isPrime := not divides( f, w );
f := f + 2;
f2 := f2 + toNext;
toNext := toNext + 8
end while_isPrime_and_f2_le_x
end if_isPrime ;
if isPrime then begin
wCount := wCount + 1;
write( i_w := 3, s_w := 0, wCount, ": ", p, " " );
if p >= 32 then printI14( w )
else begin
integer iw;
iw := truncate( w );
writeon( i_w := 14, s_w := 0, iw )
end of_p_ge_32__ ;
if wCount >= 10 then goto done % stop at 10 Wagstaff primes %
end if_isPrime
end for_p ;
done:
end.

View file

@ -0,0 +1,15 @@
wagstaff?: function [e][
and? -> prime? e -> prime? (1+2^e)/3
]
summarize: function [n][
n: ~"|n|"
s: size n
if s > 20 -> n: ((take n 10)++"...")++drop n s-10
n ++ ~" (|s| digits)"
]
exponents: select.first:24 range.step:2 1 ∞ => wagstaff?
loop.with:'i exponents 'x -> print [
pad ~"|i+1|:" 3 pad ~"|x| -" 6 summarize (1+2^x)/3
]

View file

@ -0,0 +1,29 @@
function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 3 = 0 then return v = 3
d = 5
while d * d <= v
if v mod d = 0 then return False else d += 2
end while
return True
end function
subroutine Wagstaff(num)
pri = 1
wcount = 0
wag = 0
while wcount < num
pri += 2
if isPrime(pri) then
wag = (2 ^ pri + 1) / 3
if isPrime(wag) then
wcount += 1
print rjust(wcount,2); ": "; rjust(pri,2); " => "; int(wag)
end if
end if
end while
end subroutine
call Wagstaff(9) #BASIC-256 does not allow larger numbers
end

View file

@ -0,0 +1,34 @@
#include <gmpxx.h>
#include <primesieve.hpp>
#include <iostream>
using big_int = mpz_class;
std::string to_string(const big_int& num, size_t n) {
std::string str = num.get_str();
size_t len = str.size();
if (len > n) {
str = str.substr(0, n / 2) + "..." + str.substr(len - n / 2);
str += " (";
str += std::to_string(len);
str += " digits)";
}
return str;
}
bool is_probably_prime(const big_int& n) {
return mpz_probab_prime_p(n.get_mpz_t(), 25) != 0;
}
int main() {
const big_int one(1);
primesieve::iterator pi;
pi.next_prime();
for (int i = 0; i < 24;) {
uint64_t p = pi.next_prime();
big_int n = ((one << p) + 1) / 3;
if (is_probably_prime(n))
std::cout << ++i << ": " << p << " - " << to_string(n, 30) << '\n';
}
}

View file

@ -0,0 +1,36 @@
#include <stdio.h>
#include <string.h>
#include <gmp.h>
int main() {
const int limit = 29;
int count = 0;
char tmp[40];
mpz_t p, w;
mpz_init_set_ui(p, 1);
mpz_init(w);
while (count < limit) {
mpz_nextprime(p, p);
mpz_set_ui(w, 1);
unsigned long ulp = mpz_get_ui(p);
mpz_mul_2exp(w, w, ulp);
mpz_add_ui(w, w, 1);
mpz_tdiv_q_ui(w, w, 3);
if (mpz_probab_prime_p(w, 15) > 0) {
++count;
char *ws = mpz_get_str(NULL, 10, w);
size_t le = strlen(ws);
if (le < 34) {
strcpy(tmp, ws);
} else {
strncpy(tmp, ws, 15);
strcpy(tmp + 15, "...");
strncpy(tmp + 18, ws + le - 15, 16);
}
printf("%5lu: %s", ulp, tmp);
if (le >=34) printf( " (%ld digits)", le);
printf("\n");
}
}
return 0;
}

View file

@ -0,0 +1,19 @@
procedure WagstaffPrimes(Memo: TMemo);
{Finds Wagstaff primes up to 6^64}
var P,B,R: int64;
var Cnt: integer;
begin
{B is int64 to force 64-bit arithmetic}
P:=0; B:=1; Cnt:=0;
Memo.Lines.Add(' #: P (2^P + 1)/3');
while P<64 do
begin
R:=((B shl P) + 1) div 3;
if IsPrime(P) and IsPrime(R) then
begin
Inc(Cnt);
Memo.Lines.Add(Format('%2d: %2d %24.0n',[Cnt,P,R+0.0]));
end;
Inc(P);
end;
end;

View file

@ -0,0 +1,31 @@
proc prime v . r .
r = 1
if v mod 2 = 0 or v mod 3 = 0
if v <> 2 and v <> 3
r = 0
.
break 1
.
d = 5
while d * d <= v
if v mod d = 0
r = 0
break 2
.
d += 2
.
.
pri = 1
nwag = 0
while nwag <> 10
pri += 2
call prime pri r
if r = 1
wag = (pow 2 pri + 1) / 3
call prime wag r
if r = 1
nwag += 1
print pri & " => " & wag
.
.
.

View file

@ -0,0 +1,4 @@
// Wagstaff primes. Nigel Galloway: September 15th., 2022
let isWagstaff n=let mutable g=(1I+2I**n)/3I in if Open.Numeric.Primes.MillerRabin.IsProbablePrime &g then Some (n,g) else None
primes32()|>Seq.choose isWagstaff|>Seq.take 10|>Seq.iter(fun (n,g)->printfn $"%d{n}->%A{g}")
primes32()|>Seq.choose isWagstaff|>Seq.skip 10|>Seq,take 14|>Seq.iter(fun(n,_)->printf $"%d{n} "); printfn ""

View file

@ -0,0 +1,27 @@
Function isPrime(Byval num As Ulongint) As Boolean
If num < 2 Then Return False
If num Mod 2 = 0 Then Return num = 2
If num Mod 3 = 0 Then Return num = 3
Dim d As Uinteger = 5
While d * d <= num
If num Mod d = 0 Then Return False Else d += 2
Wend
Return True
End Function
Sub Wagstaff(num As Ulongint)
Dim As Ulongint pri = 1, wcount = 0, wag
While wcount < num
pri += 2
If isPrime(pri) Then
wag = (2 ^ pri + 1) / 3
If isPrime(wag) Then
wcount += 1
Print Using "###: ### => ##,###,###,###,###"; wcount; pri; wag
End If
End If
Wend
End Sub
Wagstaff(10)
Sleep

View file

@ -0,0 +1,41 @@
package main
import (
"fmt"
big "github.com/ncw/gmp"
"rcu"
)
func main() {
const limit = 29
count := 0
p := 1
one := big.NewInt(1)
three := big.NewInt(3)
w := new(big.Int)
for count < limit {
for {
p += 2
if rcu.IsPrime(p) {
break
}
}
w.SetUint64(1)
w.Lsh(w, uint(p))
w.Add(w, one)
w.Quo(w, three)
if w.ProbablyPrime(15) {
count++
ws := w.String()
le := len(ws)
if le >= 34 {
ws = ws[0:15] + "..." + ws[le-15:]
}
fmt.Printf("%5d: %s", p, ws)
if le >= 34 {
fmt.Printf(" (%d digits)", le)
}
println()
}
}
}

View file

@ -0,0 +1,18 @@
(,.f)p:I.1 p:(f=.3%~1+2x^])p:i.60
3 3
5 11
7 43
11 683
13 2731
17 43691
19 174763
23 2796203
31 715827883
43 2932031007403
61 768614336404564651
79 201487636602438195784363
101 845100400152152934331135470251
127 56713727820156410577229101238628035243
167 62357403192785191176690552862561408838653121833643
191 1046183622564446793972631570534611069350392574077339085483
199 267823007376498379256993682056860433753700498963798805883563

View file

@ -0,0 +1,38 @@
{{
T0=. 6!:1''
f=. 3%~1+2x^]
c=. 0
echo 'count power seconds'
for_p. p:i.1e4 do.
if. 1 p: f p do.
c=. c+1
echo 5 6 8j3":c, p, (6!:1'')-T0
if. 24 <: c do. return. end.
end.
end.
}}_
count power seconds
1 3 0.004
2 5 0.006
3 7 0.008
4 11 0.013
5 13 0.018
6 17 0.021
7 19 0.025
8 23 0.028
9 31 0.030
10 43 0.033
11 61 0.035
12 79 0.039
13 101 0.044
14 127 0.052
15 167 0.062
16 191 0.075
17 199 0.089
18 313 0.120
19 347 0.167
20 701 0.436
21 1709 4.140
22 2617 16.035
23 3539 45.089
24 5807 181.280

View file

@ -0,0 +1,18 @@
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
BigInteger d = new BigInteger("3"), a;
int lmt = 25, sl, c = 0;
for (int i = 3; i < 5808; ) {
a = BigInteger.ONE.shiftLeft(i).add(BigInteger.ONE).divide(d);
if (a.isProbablePrime(1)) {
System.out.printf("%2d %4d ", ++c, i);
String s = a.toString(); sl = s.length();
if (sl < lmt) System.out.println(a);
else System.out.println(s.substring(0, 11) + ".." + s.substring(sl - 11, sl) + " " + sl + " digits");
}
i = BigInteger.valueOf(i).nextProbablePrime().intValue();
}
}
}

View file

@ -0,0 +1,41 @@
using Primes
function wagstaffpair(p::Integer)
isodd(p) || return (false, nothing)
isprime(p) || return (false, nothing)
m = (2^big(p) + 1) ÷ 3
isprime(m) || return (false, nothing)
return (true, m)
end
function findn_wagstaff_pairs(n_to_find::T) where T <: Integer
pairs = Tuple{T, BigInt}[]
count = 0
i = 2
while count < n_to_find
iswag, m = wagstaffpair(i)
iswag && push!(pairs, (i, m))
count += iswag
i += 1
end
return pairs
end
function println_wagstaff(pair; max_digit_display::Integer=20)
p, m = pair
mstr = string(m)
if length(mstr) > max_digit_display
umiddle = cld(max_digit_display, 2)
lmiddle = fld(max_digit_display, 2)
mstr = join((mstr[1:umiddle], "...", mstr[end-lmiddle+1:end],
" ($(length(mstr)) digits)"))
end
println("p = $p, m = $mstr")
end
foreach(println_wagstaff, findn_wagstaff_pairs(24))

View file

@ -0,0 +1,18 @@
import std/strformat
import integers
func compress(str: string; size: int): string =
if str.len <= 2 * size: str
else: &"{str[0..<size]}...{str[^size..^1]} ({str.len} digits)"
echo "First 24 Wagstaff primes:"
let One = newInteger(1)
var count = 0
var p = 3
while count < 24:
if p.isPrime:
let n = (One shl p + 1) div 3
if n.isPrime:
inc count
echo &"{p:4}: {compress($n, 15)}"
inc p, 2

View file

@ -0,0 +1,12 @@
let is_prime n =
let rec test x =
let q = n / x in x > q || x * q <> n && n mod (x + 2) <> 0 && test (x + 6)
in if n < 5 then n lor 1 = 3 else n land 1 <> 0 && n mod 3 <> 0 && test 5
let is_wagstaff n =
let w = succ (1 lsl n) / 3 in
if is_prime n && is_prime w then Some (n, w) else None
let () =
let show (p, w) = Printf.printf "%u -> %u%!\n" p w in
Seq.(ints 3 |> filter_map is_wagstaff |> take 11 |> iter show)

View file

@ -0,0 +1,51 @@
\\ compiler: gp2c option: gp2c-run -g wprp.gp
/* wprp(p): input odd prime p > 5 . */
/* returns 1 if (2^p+1)/3 is a Wagstaff probable prime. */
wprp(p) = {
/* trial division up to a reasonable depth (time ratio tdiv/llr ≈ 0.2) */
my(l=log(p), ld=log(l));
forprimestep(q = 1, sqr(ld)^(l/log(2))\4, p+p,
if(Mod(2,q)^p == -1, return)
);
/* From R. & H. LIFCHITZ July 2000 */
/* see: http://primenumbers.net/Documents/TestNP.pdf */
/* if (2^p+1)/3 is prime ==> 25^2^(p-1) ≡ 25 (mod 2^p+1) */
/* Lucas-Lehmer-Riesel test with fast modular reduction. */
my(s=25, m=2^p-1);
for(i = 2, p,
s = sqr(s);
s = bitand(s,m) - s>>p
);
s==25
}; /* end wprp */
/* get exponents of Wagstaff prps in range [a,b] */
wprprun(a, b) = {
my(t=0, c=0, thr=default(nbthreads));
a = max(a,3);
gettime();
if(a <= 5,
if(a == 3, c++; p = 3; printf("#%d\tW%d\t%2dmin, %2d,%03d ms\n", c, p, t\60000%60, t\1000%60, t%1000));
c++; p = 5; printf("#%d\tW%d\t%2dmin, %2d,%03d ms\n", c, p, t\60000%60, t\1000%60, t%1000);
a = 7
);
parforprime(p= a, b, wprp(p), d, /* wprp(p) -> d copy from parallel world into real world. */
if(d,
t += gettime()\thr;
c++;
printf("#%d\tW%d\t%2dmin, %2d,%03d ms\n", c, p, t\60000%60, t\1000%60, t%1000)
)
)
}; /* end wprprun */
/* if running wprp as script */
\\ export(wprp);
wprprun(2, 42737)

View file

@ -0,0 +1,13 @@
use v5.36;
use bigint;
use ntheory 'is_prime';
sub abbr ($d) { my $l = length $d; $l < 61 ? $d : substr($d,0,30) . '..' . substr($d,-30) . " ($l digits)" }
my($p,@W) = 2;
until (@W == 30) {
next unless 0 != ++$p % 2;
push @W, $p if is_prime($p) and is_prime((2**$p + 1)/3)
}
printf "%2d: %5d - %s\n", $_+1, $W[$_], abbr( (2**$W[$_] + 1) / 3) for 0..$#W;

View file

@ -0,0 +1,35 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</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;">function</span> <span style="color: #000000;">isWagstaff</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">bLenOnly</span><span style="color: #0000FF;">=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_add_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_fdiv_q_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">)?</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bLenOnly</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">mpz_sizeinbase</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">):</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">comma_fill</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)):</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">wagstaff</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">wagstaff</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pdx</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">isWagstaff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">wagstaff</span> <span style="color: #0000FF;">&=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">}}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">pdx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 10 Wagstaff primes for the values of 'p' shown:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">printf</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"%2d: %s\n"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">wagstaff</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nTook %s\n\n"</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: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Wagstaff primes that we can find in 5 seconds:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</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: #000000;">5</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pdx</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">isWagstaff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%5d (%,d digits, %s)\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</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: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">pdx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--

View file

@ -0,0 +1,18 @@
""" Rosetta code Wagstaff_primes task """
from sympy import isprime
def wagstaff(N):
""" find first N Wagstaff primes """
pri, wcount = 1, 0
while wcount < N:
pri += 2
if isprime(pri):
wag = (2**pri + 1) // 3
if isprime(wag):
wcount += 1
print(f'{wcount: 3}: {pri: 5} => ',
f'{wag:,}' if wcount < 11 else f'[{len(str(wag))} digit number]')
wagstaff(24)

View file

@ -0,0 +1,16 @@
# First 20
my @wagstaff = (^).grep: { .is-prime && ((1 + 1 +< $_)/3).is-prime };
say ++$ ~ ": $_ - {(1 + 1 +< $_)/3}" for @wagstaff[^20];
say .fmt("\nTotal elapsed seconds: (%.2f)\n") given (my $elapsed = now) - INIT now;
# However many I have patience for
my atomicint $count = 20;
hyper for @wagstaff[20] .. * {
next unless .is-prime;
say ++$count ~ ": $_ ({sprintf "%.2f", now - $elapsed})" and $elapsed = now if is-prime (1 + 1 +< $_)/3;
}

View file

@ -0,0 +1,15 @@
require 'prime'
require 'gmp'
wagstaffs = Enumerator.new do |y|
odd_primes = Prime.each
odd_primes.next #skip 2
loop do
p = odd_primes.next
candidate = (2 ** p + 1)/3
y << [p, candidate] unless GMP::Z.new(candidate).probab_prime?.zero?
end
end
10.times{puts "%5d - %s" % wagstaffs.next}
14.times{puts "%5d" % wagstaffs.next.first}

View file

@ -0,0 +1,44 @@
import "./math" for Int
import "./gmp" for Mpz
import "./fmt" for Fmt
var isWagstaff = Fn.new { |p|
var w = (2.pow(p) + 1) / 3 // always integral
if (!Int.isPrime(w)) return [false, null]
return [true, [p, w]]
}
var isBigWagstaff = Fn.new { |p|
var w = Mpz.one.lsh(p).add(1).div(3)
return w.probPrime(15) > 0
}
var start = System.clock
var p = 1
var wagstaff = []
while (wagstaff.count < 10) {
while (true) {
p = p + 2
if (Int.isPrime(p)) break
}
var res = isWagstaff.call(p)
if (res[0]) wagstaff.add(res[1])
}
System.print("First 10 Wagstaff primes for the values of 'p' shown:")
for (i in 0..9) Fmt.print("$2d: $d", wagstaff[i][0], wagstaff[i][1])
System.print("\nTook %(System.clock - start) seconds")
var limit = 19
var count = 0
System.print("\nValues of 'p' for the next %(limit) Wagstaff primes and")
System.print("overall time taken to reach them to higher second:")
while (count < limit) {
while (true) {
p = p + 2
if (Int.isPrime(p)) break
}
if (isBigWagstaff.call(p)) {
Fmt.print("$5d ($3d secs)", p, (System.clock - start).ceil)
count = count + 1
}
}

View file

@ -0,0 +1,28 @@
func IsPrime(N); \Return 'true' if N is prime
real N; int I;
[if N <= 2. then return N = 2.;
if Mod(N, 2.) = 0. then \even\ return false;
for I:= 3 to fix(sqrt(N)) do
[if Mod(N, float(I)) = 0. then return false;
I:= I+1;
];
return true;
];
real P, Q; int C;
[P:= 2.; C:= 0;
Format(1, 0);
repeat if IsPrime(P) then
[Q:= Pow(2., P) + 1.;
if Mod(Q, 3.) = 0. and IsPrime(Q/3.) then
[Text(0, "(2^^");
RlOut(0, P);
Text(0, " + 1)/3 = ");
RlOut(0, Q/3.);
CrLf(0);
C:= C+1;
];
];
P:= P+1.;
until C >= 10;
]