-
Notifications
You must be signed in to change notification settings - Fork 0
/
primesV2.java
65 lines (58 loc) · 2.01 KB
/
primesV2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.*;
import java.math.BigInteger;
public class Main {
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static boolean isSophieGermain(int p) {
int q = 2 * p + 1;
return isPrime(q);
}
public static boolean isSafePrime(int p) {
int q = (p - 1) / 2;
return isPrime(p) && isPrime(q);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of digits: ");
int digits = input.nextInt();
int max = (int) Math.pow(10, digits) - 1;
int min = (int) Math.pow(10, digits - 1);
System.out.println("Finding highest prime...");
int highestPrime = 0;
for (int i = max; i >= min; i--) {
if (isPrime(i)) {
highestPrime = i;
break;
}
}
System.out.println("Highest prime: " + highestPrime);
System.out.println("Finding lower primes...");
List<String> lowerPrimes = new ArrayList<String>();
for (int i = 2; i < highestPrime; i++) {
if (isPrime(i)) {
lowerPrimes.add(String.format("%0" + digits + "d", i));
}
}
System.out.println("Lower primes: " + lowerPrimes);
System.out.println("Checking for Sophie Germain and safe primes...");
for (String prime : lowerPrimes) {
int p = Integer.parseInt(prime);
if (isSophieGermain(p) && isSafePrime(p)) {
System.out.println(p + " is a Sophie Germain and safe prime");
} else if (isSophieGermain(p)) {
System.out.println(p + " is a Sophie Germain prime");
} else if (isSafePrime(p)) {
System.out.println(p + " is a safe prime");
}
}
}
}