c - 两个连续质数之间的最大差值

标签 c algorithm max primes

这是一个用 C 语言编写的简单程序,它询问用户有关 int n 的信息。
查找从 2n 范围内的 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/

相关文章:

c - ISO C90/99 检查编译器特定函数是否存在

javascript - 无法使用 GTK WebKit Webview 将按钮单击连接到 C 函数

java - 二叉搜索树混淆(最佳情况)

最远互质算法

c - 打开文件 - C 编程

c - Unix 环境高级编程第 3 版,§7.8,未声明函数的默认返回值是 int?

algorithm - 为什么在快速排序中选择随机枢轴

sql - 每个 worker 的最大日期

numpy - 沿 Numpy ndarray 轴的最大值?

mysql - 当有两个 max (XXX) 时,SQL 只选择一行