我需要编写一个输入数字的代码,然后检查给定数字是否为跟随它的素数的孪生素数。以下是我到目前为止的情况:
int isPrime (int Value){
int Number;
for (Number=2; Number <= Value-1; Number++){
if (Value % Number ==0)
return 0;
else
return 1;
}
}
int nextPrime(int current){
while (1){
if (isPrime(current))
return current;
current++;
}
}
int main(){
int a;
int First = nextPrime (a);
int Second = nextPrime(First + 1);
scanf("%d", &a);
if (Second - First == 2) {
printf("%d and %d are twin primes.", First, Second);
} else {
printf("%d and %d are not twin primes.", First, Second);
}
return 0;
}
答案 0 :(得分:0)
第一次调用a
时,nextPrime(a)
未初始化。要获取除未定义行为之外的任何内容,您需要在调用scanf
之前读取输入值,以便将其值初始化为用户的输入:
int a;
int First;
int Second;
scanf("%d", &a); /* move this line before the nextPrime calls */
First = nextPrime (a);
Second = nextPrime(First + 1);