我需要打印出前 1000 个元素的哥德巴赫猜想(在代码中,您会注意到,为了简单起见,我只使用 100 个元素,并且我将 1 作为素数包含在内)。我知道哥德巴赫猜想说每个偶数都可以表示为两个素数之和。我的程序可以运行,但它会跳过某些偶数,例如 8、12 等,我不确定如何解决这个问题。请帮我解决这个问题。
public class GoldbachClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i;
int num = 0;
int maxCheck = 100; // limit set to which you want to find prime numbers
boolean isPrime = true;
int[] primeNumbers = new int [maxCheck];
//Start loop 1 to maxCheck
for (i = 1; i <= maxCheck; i++)
{
isPrime = CheckPrime(i);
if (isPrime)
{
primeNumbers[i] = i;
}
}
System.out.println("Prime numbers from 1 to " + maxCheck + " are:");
// Print prime numbers from 1 to maxCheck
for (int j = 1; j < primeNumbers.length; j++)
{
if (primeNumbers[j] != 0){
System.out.printf("%d ", primeNumbers[j]);
}
}
System.out.println();
for (int j = 1; j < primeNumbers.length; j++)
{
if (primeNumbers[j] != 0)
{
num = primeNumbers[j] + primeNumbers[j];
System.out.printf("%d = %d + %d\n", num, primeNumbers[j], primeNumbers[j]);
}
}
}
public static boolean CheckPrime(int numberToCheck) {
int remainder;
for (int i = 2; i <= numberToCheck / 2; i++) {
remainder = numberToCheck % i;
//if remainder is 0 than numberToCheckber is not prime and break loop. Elese continue loop
if (remainder == 0) {
return false;
}
}
return true;
}
}
让我们解决代码中的一些具体问题:哥德巴赫猜想仅适用于偶数数,但您的代码也输出奇数数,因此我们将过滤偶数结果;你的素数测试检查到
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
。
我使用了一些优化。 我不确定你是否想要所有的素数对或者只是第一个命中。 如果您想要它们全部,只需删除我标记的 continue 和 break 语句即可。您可能想阅读 6k±1 和素数 > 4
public class GoldbachClass {
public static void main(String[] args) {
int maxCheck = 1000;
int k,num,index;
boolean[] primeNumbers = new boolean[maxCheck+1]; // Save space
primeNumbers[2] = true;
primeNumbers[3] = true;
for (index = 5; index <= maxCheck; index+=6) {
if (CheckPrime(index)) primeNumbers[index] = true;
if (CheckPrime(index+2)) primeNumbers[index+2] = true;
}
System.out.println("Prime numbers from 1 to " + maxCheck + " are:");
for (index = 2; index <= maxCheck; index++) {
if (primeNumbers[index]) {
System.out.printf("%d ", index);
}
}
System.out.println();
// Goldbach check using primes
for (num = 4; num <= maxCheck; num+=2) { // just evens to check
if (num%2==1) continue; // just evens
if (num == 4) {
System.out.printf("%d = 2 + 2\n", num);
continue; // remove for all pairs
}
if (primeNumbers[num-3] ) {
System.out.printf("%d = %d + 3\n", num,num-3);
continue; } // remove for all pairs
for (k=5; k<=(int)num/2+1; k+=6) { // just 6k until num/2
if (primeNumbers[num-k] && primeNumbers[k]) {
System.out.printf("%d = %d + %d\n", num, num-k, k);
break; } // remove for all pairs
if (primeNumbers[num-(k+2)] && primeNumbers[k+2]) {
System.out.printf("%d = %d + %d\n", num, num-(k+2), k+2);
break; } // remove for all pairs
}
}
}
public static boolean CheckPrime(int numberToCheck) {
int remainder = numberToCheck%6; // for 6k check
if (numberToCheck <=3) return (numberToCheck>1);
if (remainder!=1 && remainder!=5) return false; // not 6k±1
int limit = (int)Math.sqrt(numberToCheck)+1;
for (int i = 5; i <= limit; i+=6) { // just check 6k±1
if (numberToCheck%i==0 || numberToCheck%(i+2)==0) return false;
}
return true;
}
}