Factorial primes: Difference between revisions
Content added Content deleted
Sjharper79 (talk | contribs) |
Sjharper79 (talk | contribs) |
||
Line 192: | Line 192: | ||
9: 479001600! - 1 = 479001599 |
9: 479001600! - 1 = 479001599 |
||
10: 87178291200! - 1 = 87178291199 |
10: 87178291200! - 1 = 87178291199 |
||
</pre> |
|||
=== Another Way with BigIntegers === |
|||
<syntaxhighlight lang=java> |
|||
import java.math.BigInteger; |
|||
public class MainApp { |
|||
public static void main(String[] args) { |
|||
//Used to measure total runtime of program. |
|||
long starttime = System.nanoTime(); |
|||
//How many primes found, how many primes wanted, loop counter. |
|||
int countOfPrimes = 0; |
|||
final int targetCountOfPrimes = 30; |
|||
long f = 1; |
|||
//Starting BigInteger at 1. |
|||
BigInteger biFactorial = BigInteger.ONE; |
|||
while (countOfPrimes < targetCountOfPrimes) { |
|||
//Each loop, multiply the number by the loop |
|||
//counter (f) to increase factorial much more quickly. |
|||
biFactorial = biFactorial.multiply(BigInteger.valueOf(f)); |
|||
// one less than the factorial. |
|||
BigInteger biMinusOne = biFactorial.subtract(BigInteger.ONE); |
|||
// one more than the factorial. |
|||
BigInteger biPlusOne = biFactorial.add(BigInteger.ONE); |
|||
//Determine if the numbers are prime with a probability of 100 |
|||
boolean primeMinus = biMinusOne.isProbablePrime(100); |
|||
boolean primePlus = biPlusOne.isProbablePrime(100); |
|||
//Make the big number look like a pretty string for output. |
|||
String biMinusOneString = convert(biMinusOne); |
|||
String biPlusOneString = convert(biPlusOne); |
|||
//If the number was prime, output and increment the primt counter. |
|||
if (primeMinus) { |
|||
countOfPrimes++; |
|||
System.out.println( |
|||
countOfPrimes + ": " + f + "! - 1 = " + biMinusOneString); |
|||
} |
|||
if (primePlus && f > 1) { |
|||
countOfPrimes++; |
|||
System.out.println(countOfPrimes + ": " + f + "! + 1 = " + biPlusOneString); |
|||
} |
|||
//Increment loop counter. |
|||
f++; |
|||
} |
|||
//Calculate and display program runtime. |
|||
long stoptime = System.nanoTime(); |
|||
long runtime = stoptime - starttime; |
|||
System.out.println("Program runtime: " + runtime + " ns (~" + runtime/1_000_000_000 + " seconds)"); |
|||
} |
|||
//Method to make output pretty |
|||
private static String convert(BigInteger bi) { |
|||
String s = bi.toString(); |
|||
int l = s.length(); |
|||
if (l >= 40) { |
|||
s = s.substring(0,39); |
|||
s += "... : " + l + " digits"; |
|||
} |
|||
return s; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
===output=== |
|||
<pre> |
|||
1: 2! + 1 = 3 |
|||
2: 3! - 1 = 5 |
|||
3: 3! + 1 = 7 |
|||
4: 4! - 1 = 23 |
|||
5: 6! - 1 = 719 |
|||
6: 7! - 1 = 5039 |
|||
7: 11! + 1 = 39916801 |
|||
8: 12! - 1 = 479001599 |
|||
9: 14! - 1 = 87178291199 |
|||
10: 27! + 1 = 10888869450418352160768000001 |
|||
11: 30! - 1 = 265252859812191058636308479999999 |
|||
12: 32! - 1 = 263130836933693530167218012159999999 |
|||
13: 33! - 1 = 8683317618811886495518194401279999999 |
|||
14: 37! + 1 = 137637530912263450463159795815809024000... : 44 digits |
|||
15: 38! - 1 = 523022617466601111760007224100074291199... : 45 digits |
|||
16: 41! + 1 = 334525266131638071081700620534407516651... : 50 digits |
|||
17: 73! + 1 = 447011546151268434089125713812505111007... : 106 digits |
|||
18: 77! + 1 = 145183092028285869634070784086308284983... : 114 digits |
|||
19: 94! - 1 = 108736615665674308027365285256786601004... : 147 digits |
|||
20: 116! + 1 = 339310868445189820119825609358857320323... : 191 digits |
|||
21: 154! + 1 = 308976961384735088795856467036324046592... : 272 digits |
|||
22: 166! - 1 = 900369170577843736647426172359331746074... : 298 digits |
|||
23: 320! + 1 = 211610334721925248295571704107762986587... : 665 digits |
|||
24: 324! - 1 = 228899746017910232114933690529555743502... : 675 digits |
|||
25: 340! + 1 = 510086447210371108093019328392729336303... : 715 digits |
|||
26: 379! - 1 = 248403074609647070509353232041657729688... : 815 digits |
|||
27: 399! + 1 = 160086307116559738155869925798757514626... : 867 digits |
|||
28: 427! + 1 = 290634717696073484110291540071593168467... : 940 digits |
|||
29: 469! - 1 = 677180966681495109007809820592109263009... : 1051 digits |
|||
30: 546! - 1 = 141302009261418325453762621393526286698... : 1260 digits |
|||
Program runtime: 10451693200 ns (~10 seconds) |
|||
</pre> |
</pre> |
||