我的程序大部分都可以使用,但是每当我尝试检查一个数字时,
最奇怪的是,如果早点开始,它实际上已超过该点,我只是不能让它开始超过这一点。
关于问题可能存在的任何提示,以及我可以纠正的方法?
#include <stdio.h>
#include <stdbool.h>
#define TRUE 1
#define FALSE 0
void goldbach(int);
int main(void) {
int n;
printf("Enter a number to start testing the goldbach conjecture: ");
scanf("%i", &n);
goldbach(n);
return 0;
}
void goldbach(int n) {
_Bool goldbachCheck = TRUE;
//keep running as long as n can be expressed as the sum of two primes
while(goldbachCheck == TRUE) {
_Bool isPrime[n];
for(int i = 2; i < n; i++) {
isPrime[i] = TRUE;
}
//Sieve of Erastosthenes method for calculating all primes < n
for (int i = 2; i*i < n; i++) {
if (isPrime[i]) {
for (int j = i; i*j < n; j++) {
isPrime[i*j] = FALSE;
}
}
}
//counts number of primes found
int primes = 0;
for (int i = 2; i < n; i++) {
if (isPrime[i]) {
primes++;
}
}
//store primes in an array
int storePrimes[primes];
int count = 0;
for (int i = 3; i < n; i++) {
if (isPrime[i]) {
storePrimes[count++] = i;
}
}
//Checks if n can be expressed as the sum of two primes
int start = 0;
int end = count -1;
while (start <= end){
if (storePrimes[start] + storePrimes[end] == n) {
break;
}
else if (storePrimes[start] + storePrimes[end] < n){
start++;
}
else {
end--;
}
}
if (storePrimes[start] + storePrimes[end] == n) {
printf("%i = %i + %i\n", n, storePrimes[start], storePrimes[end]);
}
else {
printf("%i can not be expressed as the sum of two odd primes.\n", n);
goldbachCheck = FALSE;
}
//Moves on to next even integer
n+=2;
}
}
答案 0 :(得分:0)
您很可能遇到堆栈限制,通常设置为8192k。你可以通过运行
找到它# ulimit -s
8192
您可以通过指定
来设置此项# ulimit -s unlimited
如果你在Mac上运行它,可以通过运行
将其设置为Mac上允许的最大值(64 Mb)# ulimit -a hard