Jump to content

Wagstaff primes: Difference between revisions

→‎{{header|J}}: added Java version
(Added Perl)
(→‎{{header|J}}: added Java version)
Line 119:
31 715827883
43 2932031007403</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang=java>import java.math.BigInteger;
 
public class Main {
public static void main(String[] args) {
BigInteger d = new BigInteger("3"), a, ii;
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();
}
}
}</syntaxhighlight>
{{out}}Takes under 30 seconds @ Tio.run
<pre>
1 3 3
2 5 11
3 7 43
4 11 683
5 13 2731
6 17 43691
7 19 174763
8 23 2796203
9 31 715827883
10 43 2932031007403
11 61 768614336404564651
12 79 201487636602438195784363
13 101 84510040015..31135470251 30 digits
14 127 56713727820..38628035243 38 digits
15 167 62357403192..53121833643 50 digits
16 191 10461836225..77339085483 58 digits
17 199 26782300737..98805883563 60 digits
18 313 55624662393..18130434731 94 digits
19 347 95562442332..21903606443 104 digits
20 701 35067572676..67823854251 211 digits
21 1709 96192527248..99070528171 514 digits
22 2617 20815047099..33435947691 788 digits
23 3539 73796098201..86486497963 1065 digits
24 5807 40184962373..86663568043 1748 digits
</pre>
 
=={{header|Perl}}==
First 20 terms are very fast, and 30 terms in just the time it takes to make a cup of coffee! (caveat: IFF you allow for the travel time of a flight to Hawaii to pick up some nice Kona beans first). Seriously, don't try this at home; <tt>bigint</tt> is mandatory here, which makes it glacially slow...
Cookies help us deliver our services. By using our services, you agree to our use of cookies.