-5

bool isPrimeここで使用する理由と、この関数が何をするのか理解できません。

{
    bool    isPrime;
    int     startingPoint, candidate, last, i;

    startingPoint = 856;
    if ( startingPoint < 2 ) 
    {
    candidate = 2;
    }
    else 
    if ( startingPoint == 2 )
    {
     candidate = 3;
    } 
    else 
    {
    candidate = startingPoint;
    if ( candidate % 2 == 0 )           
    candidate--;
    do 
    {
            isPrime = true;                 
            candidate += 2;                 
            last = sqrt( candidate );       
            for ( i = 3; (i <= last) && isPrime; i += 2 ) 
            {
                if ( (candidate % i) == 0 )
                    isPrime = false;
            }
        } while ( ! isPrime );
    }

    printf( "The next prime after %d is %d. Happy?\n",
           startingPoint, candidate );
    return 0;
}
4

3 に答える 3

1

この関数は、エンドユーザーが入力した数字の次の素数を教えてくれます。このisPrime変数は、ループ内で検出した条件に関する情報を、そのループ外で実行されるコードに渡すことができるブール型の「フラグ」として機能します。

内側のforループは、連続する奇数を除数の候補として試行します。除数が見つかった場合、フラグを に設定して、除数が見つかった (したがって候補が素数ではない) という事実を外側のdo/whileループに知らせる必要があります。外側のループはそのフラグをチェックし、範囲内のすべての候補が使い果たされた後に残っている場合にのみループを終了します。isPrimefalsetrue3..sqrt(candidate)

于 2013-07-29T13:46:33.320 に答える
0
isPrime is being used to exit from the for loop & do while loop by conditionally.

1. in For Loop 

    Our goal is to find out the next greater prime number of candidate.

    If we have the 25 as candidate, we are going to execute the for loop upto 3 to 5(sqrt of 25).

    if we found that any number between that value(3 , 5)
         divides the candidate and gave the remaining as 0.
         then the candidate was not prime number.
         So exit the from the for loop by flag setting isPrime to false.
         next time the for loop will not be executed because , 
         for loop condition fails.

    Control moves to while loop and checks condition,
            still we didn't find out the prime number & do while condition
            is true by (!isPrime) where isPrime is false (!false == Yes)
            so while loop again executing from the starting of the loop.


2. in Do while loop

    To exit from the do while loop , 
    the condition should be failed, 
    that means isPrime should be always true for 3, 5 in for loop.

    So for loop will entirely run upto the maximum of last value.

    No chance of setting up the isPrime to false on the for loop.

    so it will exit from the do while loop.
    Otherwise it will never sleep until find out the next prime number.
    it will be infinity.

I hope it will help you to understand the
code sequence & why we are using the isPrime flag on the code sequence.

コード シーケンスの概要:

素数とは:

素数 (または素数) は、1 とそれ自体以外に正の約数を持たない 1 より大きい自然数です。素数でない1より大きい自然数を合成数といいます。

たとえば、5 は 1 と 5 だけが均等に分割されるため素数であり、6 は 1 と 6 に加えて 2 と 3 の約数があるため合成数です。

bool    isPrime;
int     startingPoint, candidate, last, i;

startingPoint = 24;

//If start point less than 2
if ( startingPoint < 2 ) {

    //take candidate as 2
    candidate = 2;
}
//If start point equals 2

else if ( startingPoint == 2 ) {
    //take candidate as 3
    candidate = 3;
}
else {


//if none of the above condition then have startingPoint as candidate

    candidate = startingPoint;
    //candiate 24

    if (candidate % 2 == 0)             /* Test only odd numbers */
        candidate--;//if the candidate is even number then make it to odd number by -1 which will be 23

    do {

        isPrime = true;                 /* Initially we are assuming that the Number 23 is prime number.*/

        candidate += 2;                 /* Bump to the next number(23+2 =25) to test */
        //candidate 25

        last = sqrt( candidate );      
        //last 5


        Everytime we will do process the 'for' loop upto sqrt of the candidate.
        //candidate 25
        //last 5

        So If we found the any number between the 3 to 5 not prime number, then we no need to run the for loop till the end.

        Because we need to find out the nearest next prime number. So no need to waste our CPU usage.

        // Both 3<=5 && isPrime should be true.

        //last 3<=5 && true(we assumed it will be a prime number initially)
        for ( i = 3; (i <= last) && isPrime; i += 2 ) {     
            if ( (candidate % i) == 0 )                       
                isPrime = false;
        //here increase the 3 to 5 by (i+2), So again it will be executed. But third time it will fail the first condition.
        //So go to start of do while loop, and so on it will work
        //Next time candidate will be increased by 2 which will be (25+2) = 27 and failed to find out the prime Number.
        //Next time candidate will be increased by 2 which will be (27+2) = 29 and success 29 is prime number.

        }

    } while ( ! isPrime );// if the condition true then go to "do" statement.
}

printf( "The next prime after %d is %d. Happy?\n",
       startingPoint, candidate );
return 0;
于 2013-07-29T15:19:14.830 に答える