Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,38 @@
public class MainApp {
public static void main(String[] args) {
int countOfPrimes = 0;
final int targetCountOfPrimes = 10;
long f = 1;
while (countOfPrimes < targetCountOfPrimes) {
long factorialNum = getFactorial(f);
boolean primePlus = isPrime(factorialNum + 1);
boolean primeMinus = isPrime(factorialNum - 1);
if (primeMinus) {
countOfPrimes++;
System.out.println(countOfPrimes + ": " + factorialNum + "! - 1 = " + (factorialNum - 1));
}
if (primePlus && f > 1) {
countOfPrimes++;
System.out.println(countOfPrimes + ": " + factorialNum + "! + 1 = " + (factorialNum + 1));
}
f++;
}
}
private static long getFactorial(long f) {
long factorial = 1;
for (long i = 1; i < f; i++) {
factorial *= i;
}
return factorial;
}
private static boolean isPrime(long num) {
if (num < 2) {return false;}
for (long i = 2; i < num; i++) {
if (num % i == 0) {return false;}
}
return true;
}
}

View file

@ -0,0 +1,61 @@
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) {
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();
String s2 = "";
if (l >= 40) {
s2 = s.substring(0, 19);
s2 += "..." + s.substring(s.length() - 20, s.length());
s2 += " : " + l + " digits";
} else {s2 = s;}
return s2;
}
}