Давайте рассмотрим некоторые специфические проблемы с вашим кодом: гипотеза Гольдбаха применима только к четным числам, но ваш код также выводит нечетные числа, поэтому мы будем фильтровать четные результаты;ваш простой тест проверяет до numberToCheck/2
вместо квадратного корня из numberToCheck
;Ваш конечный производственный цикл действительно должен быть парой вложенных циклов:
public class GoldbachClass {
public static void main(String[] args) {
int maxCheck = 100;
int[] primeNumbers = new int[maxCheck];
for (int number = 1, index = 0; number <= maxCheck; number++, index++) {
if (isPrime(number)) {
primeNumbers[index] = number;
}
}
System.out.println("Prime numbers from 1 to " + maxCheck + " are:");
for (int index = 0; index < primeNumbers.length; index++) {
if (primeNumbers[index] != 0) {
System.out.printf("%d ", primeNumbers[index]);
}
}
System.out.println();
for (int i = 0; i < primeNumbers.length; i++) {
if (primeNumbers[i] == 0) {
continue;
}
for (int j = i; j < primeNumbers.length; j++) {
if (primeNumbers[j] == 0) {
continue;
}
int number = primeNumbers[i] + primeNumbers[j];
if (number % 2 == 0) { // conjecture only applies to even numbers
System.out.printf("%d = %d + %d\n", number, primeNumbers[i], primeNumbers[j]);
}
}
}
}
public static boolean isPrime(int number) {
if (number < 2 || number % 2 == 0) {
return (number == 2);
}
for (int odd = 3; odd * odd <= number; odd += 2) {
if (number % odd == 0) {
return false;
}
}
return true;
}
}
Вывод, отфильтрованный через сортировку Unix:
% java GoldbachClass | sort -n
Prime numbers from 1 to 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
4 = 2 + 2
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7
10 = 5 + 5
12 = 5 + 7
14 = 3 + 11
14 = 7 + 7
16 = 3 + 13
16 = 5 + 11
18 = 5 + 13
18 = 7 + 11
20 = 3 + 17
20 = 7 + 13
22 = 11 + 11
22 = 3 + 19
22 = 5 + 17
24 = 11 + 13
24 = 5 + 19
24 = 7 + 17
26 = 13 + 13
26 = 3 + 23
26 = 7 + 19
28 = 11 + 17
28 = 5 + 23
30 = 11 + 19
30 = 13 + 17
30 = 7 + 23
32 = 13 + 19
32 = 3 + 29
34 = 11 + 23
34 = 17 + 17
34 = 3 + 31
34 = 5 + 29
36 = 13 + 23
36 = 17 + 19
36 = 5 + 31
36 = 7 + 29
38 = 19 + 19
38 = 7 + 31
40 = 11 + 29
40 = 17 + 23
40 = 3 + 37
42 = 11 + 31
42 = 13 + 29
42 = 19 + 23
42 = 5 + 37
44 = 13 + 31
44 = 3 + 41
44 = 7 + 37
46 = 17 + 29
46 = 23 + 23
46 = 3 + 43
46 = 5 + 41
48 = 11 + 37
48 = 17 + 31
48 = 19 + 29
48 = 5 + 43
48 = 7 + 41
50 = 13 + 37
50 = 19 + 31
50 = 3 + 47
50 = 7 + 43
52 = 11 + 41
52 = 23 + 29
52 = 5 + 47
54 = 11 + 43
54 = 13 + 41
54 = 17 + 37
54 = 23 + 31
54 = 7 + 47
56 = 13 + 43
56 = 19 + 37
56 = 3 + 53
58 = 11 + 47
58 = 17 + 41
58 = 29 + 29
58 = 5 + 53
60 = 13 + 47
60 = 17 + 43
60 = 19 + 41
60 = 23 + 37
60 = 29 + 31
60 = 7 + 53
62 = 19 + 43
62 = 3 + 59
62 = 31 + 31
64 = 11 + 53
64 = 17 + 47
64 = 23 + 41
64 = 3 + 61
64 = 5 + 59
66 = 13 + 53
66 = 19 + 47
66 = 23 + 43
66 = 29 + 37
66 = 5 + 61
66 = 7 + 59
68 = 31 + 37
68 = 7 + 61
70 = 11 + 59
70 = 17 + 53
70 = 23 + 47
70 = 29 + 41
70 = 3 + 67
72 = 11 + 61
72 = 13 + 59
72 = 19 + 53
72 = 29 + 43
72 = 31 + 41
72 = 5 + 67
74 = 13 + 61
74 = 3 + 71
74 = 31 + 43
74 = 37 + 37
74 = 7 + 67
76 = 17 + 59
76 = 23 + 53
76 = 29 + 47
76 = 3 + 73
76 = 5 + 71
78 = 11 + 67
78 = 17 + 61
78 = 19 + 59
78 = 31 + 47
78 = 37 + 41
78 = 5 + 73
78 = 7 + 71
80 = 13 + 67
80 = 19 + 61
80 = 37 + 43
80 = 7 + 73
82 = 11 + 71
82 = 23 + 59
82 = 29 + 53
82 = 3 + 79
82 = 41 + 41
84 = 11 + 73
84 = 13 + 71
84 = 17 + 67
84 = 23 + 61
84 = 31 + 53
84 = 37 + 47
84 = 41 + 43
84 = 5 + 79
86 = 13 + 73
86 = 19 + 67
86 = 3 + 83
86 = 43 + 43
86 = 7 + 79
88 = 17 + 71
88 = 29 + 59
88 = 41 + 47
88 = 5 + 83
90 = 11 + 79
90 = 17 + 73
90 = 19 + 71
90 = 23 + 67
90 = 29 + 61
90 = 31 + 59
90 = 37 + 53
90 = 43 + 47
90 = 7 + 83
92 = 13 + 79
92 = 19 + 73
92 = 3 + 89
92 = 31 + 61
94 = 11 + 83
94 = 23 + 71
94 = 41 + 53
94 = 47 + 47
94 = 5 + 89
96 = 13 + 83
96 = 17 + 79
96 = 23 + 73
96 = 29 + 67
96 = 37 + 59
96 = 43 + 53
96 = 7 + 89
98 = 19 + 79
98 = 31 + 67
98 = 37 + 61
100 = 11 + 89
100 = 17 + 83
100 = 29 + 71
100 = 3 + 97
100 = 41 + 59
100 = 47 + 53
102 = 13 + 89
102 = 19 + 83
102 = 23 + 79
102 = 29 + 73
102 = 31 + 71
102 = 41 + 61
102 = 43 + 59
102 = 5 + 97
104 = 31 + 73
104 = 37 + 67
104 = 43 + 61
104 = 7 + 97
106 = 17 + 89
106 = 23 + 83
106 = 47 + 59
106 = 53 + 53
108 = 11 + 97
108 = 19 + 89
108 = 29 + 79
108 = 37 + 71
108 = 41 + 67
108 = 47 + 61
110 = 13 + 97
110 = 31 + 79
110 = 37 + 73
110 = 43 + 67
112 = 23 + 89
112 = 29 + 83
112 = 41 + 71
112 = 53 + 59
114 = 17 + 97
114 = 31 + 83
114 = 41 + 73
114 = 43 + 71
114 = 47 + 67
114 = 53 + 61
116 = 19 + 97
116 = 37 + 79
116 = 43 + 73
118 = 29 + 89
118 = 47 + 71
118 = 59 + 59
120 = 23 + 97
120 = 31 + 89
120 = 37 + 83
120 = 41 + 79
120 = 47 + 73
120 = 53 + 67
120 = 59 + 61
122 = 43 + 79
122 = 61 + 61
124 = 41 + 83
124 = 53 + 71
126 = 29 + 97
126 = 37 + 89
126 = 43 + 83
126 = 47 + 79
126 = 53 + 73
126 = 59 + 67
128 = 31 + 97
128 = 61 + 67
130 = 41 + 89
130 = 47 + 83
130 = 59 + 71
132 = 43 + 89
132 = 53 + 79
132 = 59 + 73
132 = 61 + 71
134 = 37 + 97
134 = 61 + 73
134 = 67 + 67
136 = 47 + 89
136 = 53 + 83
138 = 41 + 97
138 = 59 + 79
138 = 67 + 71
140 = 43 + 97
140 = 61 + 79
140 = 67 + 73
142 = 53 + 89
142 = 59 + 83
142 = 71 + 71
144 = 47 + 97
144 = 61 + 83
144 = 71 + 73
146 = 67 + 79
146 = 73 + 73
148 = 59 + 89
150 = 53 + 97
150 = 61 + 89
150 = 67 + 83
150 = 71 + 79
152 = 73 + 79
154 = 71 + 83
156 = 59 + 97
156 = 67 + 89
156 = 73 + 83
158 = 61 + 97
158 = 79 + 79
160 = 71 + 89
162 = 73 + 89
162 = 79 + 83
164 = 67 + 97
166 = 83 + 83
168 = 71 + 97
168 = 79 + 89
170 = 73 + 97
172 = 83 + 89
176 = 79 + 97
178 = 89 + 89
180 = 83 + 97
186 = 89 + 97
194 = 97 + 97
%
В конце результаты становятся неровными из-за нашего произвольного простого числаотсечка maxCheck
.