-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoldbach.java
More file actions
51 lines (45 loc) · 1012 Bytes
/
Goldbach.java
File metadata and controls
51 lines (45 loc) · 1012 Bytes
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
package devjava;
public class Goldbach {
static boolean isPerfectSquare(double x) {
double sq = Math.sqrt(x);
return ((sq - Math.floor(sq)) == 0);
}
static boolean isPrime(int factor) {
int counter = 0;
for (int i = 1; i <= Math.sqrt(factor); i++) {
if (factor % i == 0) {
counter++;
}
if (counter >= 2) {
return false;
}
}
return true;
}
static boolean isOddComposite(int digit) {
if (digit % 2 == 1 && !isPrime(digit)) {
return true;
}
return false;
}
public static void main(String[] args) {
for (int i = 1; i <= 10000; i++) {
int solutionsCount = 0;
if (isOddComposite(i)) {
for (int j = 0; j <= i; j++) {
int primeCandidate = i - j;
if (isPrime(primeCandidate)) {
int diff = i - primeCandidate;
if (diff % 2 == 0 && isPerfectSquare(diff / 2)) {
solutionsCount++;
}
}
}
if (solutionsCount == 0) {
System.out.println("No solutions for " + i + "!");
System.exit(0);
}
}
}
}
}