Find the prime numbers between 100 and 200

First, let’s talk about the definition of a prime number: a number that is only divisible by 1 and itself is called a prime number.

Look at the code:

#include <stdio.h>
main()
{
    int m, i, n= 0 ;        // m represents the number to be judged, i represents the number divided by m (that is, the number of judgments), and n records the number of times 
    for (m= 101 ; m<= 200 ; m=m+ 2 )
    {
        if (n% 10 == 0 )printf( " \n " ); //Indicates that every 10 numbers are output to a new line
     for (i= 2 ;i<m;i++)         // here i<m means that the dividend should be Less than the divisor, that is, m should judge with a number smaller than itself 
        if (m%i== 0 ) break ;      //When m%i==0, m is not a prime number at this time.
    if (i>= m) //Key understanding: i fails to break out of the function when it loops to m-1 through the previous if statement, indicating that m at this time is a prime number, that is, the value of m can be output when i=m
       {
        printf("%d\t",m);
        n=n+1;
       }
    }
    printf("\nprime number =%d\n",n);
}

Parsing code: Why is i>=m here:

    if(i>= m) //Key understanding: i fails to break out of the function when it loops to m-1 through the previous if statement, indicating that m at this time is a prime number, that is, the value of m can be output when i=m
       {
        printf("%d\t",m); n=n+1; }

  There is already a for loop statement before. When m%i==0, the current loop is exited, but if it can run to the if() statement, it means that there is no number that can make m divisible, that is, m is a prime number.

At that time, only if (i >= m) is required to set up the output m

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325473199&siteId=291194637