Factorial primes: Difference between revisions

(Added XPL0 example.)
Line 135:
 
(i.28x here would have given us an eleventh prime, but the task asked for the first 10, and the stretch goal requires considerable patience.)
 
=={{header|Java}}==
<syntaxhighlight lang="java">
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;
}
}
</syntaxhighlight>
Results
<pre>
1: 1! + 1 = 2
2: 2! + 1 = 3
3: 6! - 1 = 5
4: 6! + 1 = 7
5: 24! - 1 = 23
6: 720! - 1 = 719
7: 5040! - 1 = 5039
8: 39916800! + 1 = 39916801
9: 479001600! - 1 = 479001599
10: 87178291200! - 1 = 87178291199
</pre>
 
=={{header|jq}}==
25

edits