C 寻找双素数-逻辑已经完成,但它赢了';程序运行时,请不要打印任何内容
双素数是一个素数,它正好比比它小的最大素数大两倍。例如,7是一个孪生素数,因为它正好比5大两倍。但是17不是孪生素数,因为小于17的最大素数是13 我的程序逻辑如下:C 寻找双素数-逻辑已经完成,但它赢了';程序运行时,请不要打印任何内容,c,primes,C,Primes,双素数是一个素数,它正好比比它小的最大素数大两倍。例如,7是一个孪生素数,因为它正好比5大两倍。但是17不是孪生素数,因为小于17的最大素数是13 我的程序逻辑如下: *ask number of twin primes that want to be found *loop until desired number of twin primes are found *loop numbers 2 - 1million (declared as variable j) *chec
*ask number of twin primes that want to be found
*loop until desired number of twin primes are found
*loop numbers 2 - 1million (declared as variable j)
*check if that number 'j' is prime - if so flag it
*if 'j' is not flagged, subtract 2 from 'j' (call that new number 'TPcheck')
*Check if 'TPcheck' is a prime, if so, print 'TPcheck' and the first number 'j'
/* function isPrime returns True if argument is prime number. */
boolean isPrime(int aNumber)
{
int i;
if(aNumber < 2) { return False; }
else if (aNumber==2) {return True;}
for i=2 to aNumber-1
{
if((aNumber%i) == 0){
return False;
}
}
return True;
}
当我运行这个程序时,我输入要找到的双素数,但它只是继续运行,不会在屏幕上打印任何内容。我认为问题可能与循环和if语句的顺序有关(或者可能与它们的嵌套方式有关),但我尝试了大量不同的方法,但都没有奏效
这是我的密码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 2, count = 0, TPcheck, j, k, flag;
int numberofTwinPrimes;
printf("Enter how many twin primes you want to find");
scanf("%d", &numberofTwinPrimes);
while(count < numberofTwinPrimes)
{
for(j = 2; j <= 1000000; ++j)
{ for(i = 2; i < j; ++i)
{
if(j%i == 0)
{
flag = 1;
continue;
}
if(flag == 0)
{
TPcheck = j - 2;
for(k = 2; k < TPcheck; ++k)
{
if(TPcheck%k == 0)
{
flag = 1;
continue;
}
if(flag == 0)
{
printf("%d\t %d\t", TPcheck, j);
count++;
}
}
}
}
}
}
return 0;
}
#包括
#包括
int main()
{
int i=2,计数=0,TPcheck,j,k,标志;
整数个双素数;
printf(“输入要查找的孪生素数数量”);
scanf(“%d”和numberofTwinPrimes);
while(count 对于(j=2;j,检查数字是否为素数的代码不正确
您应该检查数字,切勿将任何小于数字的数字除以
检查数字是否为素数的函数代码如下:
*ask number of twin primes that want to be found
*loop until desired number of twin primes are found
*loop numbers 2 - 1million (declared as variable j)
*check if that number 'j' is prime - if so flag it
*if 'j' is not flagged, subtract 2 from 'j' (call that new number 'TPcheck')
*Check if 'TPcheck' is a prime, if so, print 'TPcheck' and the first number 'j'
/* function isPrime returns True if argument is prime number. */
boolean isPrime(int aNumber)
{
int i;
if(aNumber < 2) { return False; }
else if (aNumber==2) {return True;}
for i=2 to aNumber-1
{
if((aNumber%i) == 0){
return False;
}
}
return True;
}
/*如果参数是素数,则函数isPrime返回True*/
布尔isPrime(整数个数)
{
int i;
如果(数量<2){返回False;}
如果(aNumber==2){return True;}
对于i=2到a个数-1
{
如果((数量%i)==0){
返回False;
}
}
返回True;
}
我希望这能给你一些有用的想法。我认为你的代码可以简化很多
定义一个函数,该函数只返回一个数是否为素数
使用非常简单的逻辑在循环中使用它
这是一个有效的版本
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int isPrime(int n)
{
int stop = 0;
int i = 0;
// Special case for 3.
if ( n == 3 )
{
return 1;
}
// If n is not divisible by numbers up to sqrt(n),
// then, n is a prime number.
stop = (int)(sqrt(n));
// We can start at 3 and increment by 2
// There is no point dividing by even numbers.
for ( i = 3; i <= stop; i +=2 )
{
if ( n%i == 0 )
{
// It is not a prime number.
return 0;
}
}
// Checked divisibility by all numbers up to sqrt(n)
// This is a prime number.
return 1;
}
int main()
{
int i = 0;
int count = 0;
int numberofTwinPrimes;
printf("Enter how many twin primes you want to find: ");
scanf("%d", &numberofTwinPrimes);
// Start checking at 3 and increment by 2.
// There is no point checking even numbers.
// When we find the required number of twin primes, stop.
for(i = 3; i <= 1000000 && count < numberofTwinPrimes; i += 2 )
{
if ( isPrime(i) && isPrime(i+2) )
{
++count;
printf("%d\t %d\n", i, i+2);
}
}
return 0;
}
此isPrime()函数比Fumu的建议更快:
/* function isPrime returns True if argument is prime number. */
boolean isPrime(int aNumber)
{
int i;
int limit;
/* Numbers < 2 */
if(aNumber < 2) { return False; }
/* Even numbers. */
if (aNumber % 2 == 0) { return aNumber == 2; }
/* Odd numbers. */
/* Only need to check odd divisors as far as the square root. */
limit = (int)(sqrt(aNumber));
for (i = 3; i <= limit; i += 2)
{
if( aNumber % i == 0) { return False; }
}
/* Only prime numbers make it this far. */
return True;
}
/*如果参数是素数,则函数isPrime返回True*/
布尔isPrime(整数个数)
{
int i;
整数极限;
/*数量<2*/
如果(数量<2){返回False;}
/*偶数*/
如果(数量%2==0){返回数量==2;}
/*奇数*/
/*只需要检查奇数除数,直到平方根为止*/
限值=(整数)(sqrt(数量));
对于(i=3;i)您没有初始化标志,也从未将其设置为零(为了打印一个值,您的代码询问标志是否=0)