c - for 循环 C 工作奇怪吗?

标签 c for-loop

我在 C 语言中遇到 for 循环问题。 该程序的目的是找到已知数量 k 的素数。 这是我的程序:

 unsigned int i, k, j;
    unsigned long int prime;

    int _tmain(int argc, _TCHAR* argv[]) {

    printf("How many prime numbers do you want to print out in order? "); scanf_s("%u", &k);
        printf("%u fist prime numbers are: ", k);
        i = 1;
        prime = 2;
        while (i <= k)
        {
                for (j = 2; j <= prime; j++)
                {
                    if (prime % j == 0)
                    {
                        if (j == prime && prime != 2)
                        {
                            printf("%lu, ", prime);
                            i++;
                        }
                        prime++;
                        j = 2;
                    }
                }
        }
        _getch();
    return 0;
    }

当我运行该程序时,它返回无限的数字序列。但如果我添加“else j++;”像这样:

for (j = 2; j <= prime; j++)
                {
                    if (prime % j == 0)
                    {
                        if (j == prime && prime != 2)
                        {
                            printf("%lu, ", prime);
                            i++;
                        }
                        prime++;
                        j = 2;
                    }
                               else j++;
                }

然后程序就可以正常运行了。我觉得这有点奇怪,无法解释为什么?

提前致谢(并对我的英语不好表示歉意)。

最佳答案

正如我在评论中提到的,问题是由于您在 for 循环中没有“中断”导致它在检测到有效除数时返回到外部 while 循环。

由于您对变量名称的不当使用而使您感到困惑 - 您的原始代码似乎对“j”和“prime”是什么感到困惑。

在这里,我重构了您的代码,使其更加简洁,并显着提高了大型素数搜索的性能:

#include <stdio.h>

#define FALSE (0)
#define TRUE (!(FALSE))

int main()
{
    int numPrimes = 0;
    int candidate, divisor;
    int isPrime;
    printf("How many prime numbers do you want to print out in order? ");
    scanf("%u", &numPrimes);
    printf("\n%u first prime numbers are:\n", numPrimes);

    if (numPrimes > 0) {
        printf("2, ");
        --numPrimes;
    }

    // we only need to test odd numbers.
    for (candidate = 3; numPrimes > 0; candidate += 2) {
        // N / (N/2) + 1 is always < 1, so we only have to test half the values.
        // we also only need to test odd divisors
        isPrime = TRUE;
        for (divisor = 3; divisor <= candidate / 2; ++divisor) {
            // it's not prime if testNumber divides in
            if (candidate % divisor == 0) {
                isPrime = FALSE;
                break;
            }
        }
        if (isPrime) {
            printf("%lu, ", candidate);
            --numPrimes;
        }
    }

    printf("\n");

    return 0;
}

现场演示:http://ideone.com/yrUPBy

这很高兴地告诉我

How many prime numbers do you want to print out in order? 
1000 first prime numbers are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 
...
7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 
7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 

关于c - for 循环 C 工作奇怪吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19557009/

相关文章:

mysql - c 上的 const char 具有相同的变量但不同的值

c - K&R 的 atof 实现有什么问题?

ios - 如何在循环内增加索引并停止枚举?

java - for循环中scanner.nextLine()的问题

Javascript 获取表单数组值

c++ - 如何在 Eclipse 中将 C 项目与 C++ 静态库(使用 opencv)链接

c - 解压 16 位 BCD 的最有效公式? (例如 0x1234 到 0x01020304)

c - SciPy中16384.0*floor(v/16384.0)的使用说明

c - "for(;;)"是什么意思

perl - 为什么这不会永远运行?