这是一个用 C 语言编写的简单程序,它询问用户有关 int n
的信息。
查找从 2
到 n
范围内的 2 个连续素数之间的最大差异的最佳方法是什么?
int main() {
int n, i, j, c;
clrscr();
printf("Enter Range To Print Prime Numbers:");
scanf("%d", &n);
printf("Prime Numbers Are Following:\n");
for(i=1, c=0; i<=n; i++) {
for (j=1; j<=i; j++)
if (i % j == 0)
c++;
if (c == 2)
printf("%d ", i);
}
printf("\n\n The highest difference between numbers is:");
/* logic i cant get */
getch();
}
最佳答案
跟踪变量中两个连续素数之间的最大差异,如果新差异更大,则用新差异替换该变量。
int maxDifference = 0;
int currentDifference;
// begin looping
currentDifference = calculateNewDifference();
if(currentDifference > maxDifference) {
maxDifference = currentDifference;
}
关于c - 两个连续质数之间的最大差值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25750630/