简体   繁体   English

从书中了解C示例?

[英]Understanding C example from a book?

I am learning from Programming in C by Stephen Kochan. 我正在从Stephen Kochan的C语言编程中学习。 Program 7.4 Revising the Program to Generate Prime Numbers, Version 2: 计划7.4修改程序以生成质数,版本2:

#include <stdio.h>
#include <stdbool.h>

// Modified program to generate prime numbers
int main (void)
{
    int p, i, primes[50], primeIndex = 2;
    bool isPrime;
    primes[0] = 2;
    primes[1] = 3;

    for ( p = 5; p <= 50; p = p + 2 ) {
        isPrime = true;

        for ( i = 1; isPrime && p / primes[i] >= primes[i]; ++i )
            if ( p % primes[i] == 0 )
            isPrime = false;
            if ( isPrime == true ) {
                primes[primeIndex] = p;
                ++primeIndex;
            }
        }
        for ( i = 0; i < primeIndex; ++i )
            printf ("%i ", primes[i]);

    printf ("\n");
    return 0;
}

The problem is I failed to understand how its works. 问题是我无法理解其工作原理。

The expression 表达方式

  p / primes[i] >= primes[i] 

Is used in the innermost for loop as a test to ensure that the value of p does not exceed the square root of primes[i] .This test comes directly from the discussions in the previous paragraph. 在最内层的for循环中用作检验,以确保p的值不超过素数[i]的平方根。该检验直接来自上一段的讨论。 (You might want to think about the math a bit.) (您可能需要考虑一下数学。)

After that book show me that another line: 那本书之后,告诉我另一行:

If it is, then isPrime is set false. 如果是,则将isPrime设置为false。 The for loop continues execution so long as the value of isPrime is true and the value of primes[i] does not exceed the square root of p . 只要isPrime值为true且primes[i]的值不超过p平方根,for循环就会继续执行。

please explain that line 请解释那条线

Prime number is only divided by 1 and itself. 质数仅除以1及其本身。
So, maximum number that can be divisible is square root of it. 因此,最大可平方数是其平方根。

The below are same equation if p is a positive integer. 如果p为正整数,则以下方程式相同。

1) primes[i] <= square root of (p)  
2) primes[i] * primes[i] <= p
3) p >= primes[i] * primes[i]
4) p / primes[i] >= primes[i]

suppose the number p = 99 then the loop will check sqrt(99) = 9 that is upto 9 prime if there is no isPrime check in the loop. 假设数字p = 99,则循环中将检查sqrt(99)= 9,如果循环中没有isPrime检查,则最多9个素数。 that is : 那是 :

for ( i = 1;  p / primes[i] >= primes[i]; ++i )
        if ( p % primes[i] == 0 )
          isPrime = false;

then it will go upto 9 prime check even though 99 is divisible by 3. But 99 is divisible by 3. So do we need to test it further more?? 那么即使99被3整除,它也会上升到9个素数检查。但是99被3整除,所以我们需要进一步测试吗? Nope so we can stop here and thats why we set a flag variable to test if it is already divisible by any prime in range that 9 prime. 不,所以我们可以在这里停止,这就是为什么我们设置一个标志变量来测试它是否已经可以被9个素数范围内的任何素数整除。

So if we add isPrime in loop that is: 因此,如果我们在循环中添加isPrime,则为:

for ( i = 1; isPrime && p / primes[i] >= primes[i]; ++i )
            if ( p % primes[i] == 0 )
            isPrime = false;

then after the second iteration it will stop as isPrime then set false and this is much more efficient. 然后在第二次迭代之后,它将按isPrime停止,然后设置为false,这将更加有效。

I hope You understand. 我希望你明白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM