Chapter 7 Experimental Functions

Chapter 7 Experimental Functions

 

  • Topic 1:

Trial quotient method is the simplest method to judge prime numbers. Use an integer between i=2 ~ m-1 to try the quotient. If there is a certain m that can be divisible by an integer i other than 1 and m itself (that is, the remainder is 0), then m is not a prime number. If all of the above ranges are If an integer cannot divide m, then m is a prime number. Using the trial quotient method, write the prime number judgment function IsPrime() with three methods: goto statement, break statement and set flag variables and strengthen the loop test. Input an integer m from the keyboard to determine whether m is a prime number, if m is a prime number , Then print the number as a prime number in the format "%d is a prime number\n", otherwise print the number as a prime number in the format "%d is not a prime number\n". Then analyze which method is more readable.

 

1. Problem solving ideas:

Define a flag, which is prime when flag=1. The prime number judges IsPrime(). If the number entered in the outer layer is 1 or a prime number, define prime=0 to return the main function, which is not a prime number. Trial quotient method, use for loop, i=2 ~ m-1 to try quotation, if the input number divided by the modulus of the i variable is 0, prime=0 returns the main function, this number is not a prime number, break.

 

2. Source code:

#include <stdio.h>

 

int m,prime;

int IsPrime(int m);

 

int main ()

{

    int flag;

    printf("please enter a number:",m);

    scanf("%d",&m);

    flag=IsPrime(m);

    if(flag==1)

      printf("%d is a prime number");

    else

      printf("%d is not a prime number",m);

    return 0;

}

int IsPrime(int m)

{

    if(m==1)

       {

           prime=0;

       }

    else

        {

        int i;

          for(i = 2;i <= (m - 1);i++)

           {

            if(m%i==0)

           {

            prime=0;

            break;

           }

           else

           prime=1;

           }

        }

    return prime;

}

 

3. Screenshot of program running effect:

 

 

 

 

 

 

 

 

  • Topic 2:

Mathematical methods can be used to prove that a number that cannot be divisible by a number between 2 and √m (rounded) must not be divisible by 1 and any integer other than itself. According to this property of prime numbers, by modifying the specific realization of IsPrime(), programming to complete task 1.

 

  1. Problem-solving ideas:

On the basis of task 1, according to the nature of prime numbers, the range of variable i is changed to 2~√m.

 

2. Source code:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

int m,prime;

int IsPrime(int m);

 

int main ()

{

    int flag;

    printf("please enter a number:",m);

    scanf("%d",&m);

    flag=IsPrime(m);

    if(flag==1)

      printf("%d is a prime number");

    else

      printf("%d is not a prime number",m);

    return 0;

}

int IsPrime(int m)

{

    if(m==1)

       {

           prime=0;

       }

    else

        {

        int i;

          for(i = 2;i <= sqrt(m);i++)

           {

            if(m%i==0)

           {

            prime=0;

            break;

           }

           else

           prime=1;

           }

        }

    return prime;

}

 

3. Screenshot of program running effect:

 

 

 

 

 

 

 

 

 

 

 

  • Question 3:

Input an integer n from the keyboard arbitrarily, program to calculate and output the sum of all prime numbers between 1 and n.

 

  1. Problem-solving ideas:

Define n, j, and 2 as prime numbers, and the sum of sum starting value is defined as 2. J starts from 3, if the return value of IsPrime(int j) is prime=1, it is a prime number, use a for loop, j=3~m, and sum accumulates j which is a prime number of 3~m and outputs it. For the prime number j, a loop is judged, whether the remainder of the variable i is 0, if it is not 0, return prime=1, and perform the accumulation calculation.

 

2. Source code:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

int m,prime,j;

int IsPrime(int j);

 

int main ()

{

    int flag,sum=2;

    printf("please enter a number:");

    scanf("%d",&m);

    for(j=3;j<=m;j++)

    {

        if(IsPrime(j))

        {

            sum += j;

        }

    }

    printf("Sum is %d",sum);

    return 0;

}

int IsPrime(int j)

{

    int prime=1;

    int i;

          for(i=2;i<=j-1;i++)

           {

            if(j%i==0)

           {

            prime=0;

            break;

           }

           else

           prime=1;

           }

       return prime;

}

3. Screenshot of program running effect:

 

 

 

 

 

 

 

 

 

 

  • Question 4:

Input an integer m from the health disk, if m is not a prime number, calculate and output all its factors (excluding 1), for example, for 16, output 2, 4, 8; otherwise, input "No divisor! It is a prime number" ".

 

1. Problem solving ideas:

Printf is placed in the for loop, and outputs the factors that are not prime numbers each time. For the input number is not a prime number, the outer program prime=0 returns to the main function, this number is the remainder of the variable j, and j loops once and adds 1 to it. If the remainder is 0, then j is its factor and output.

 

  1. Source code:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

int m,prime,j;

int IsPrime(int m);

 

int main ()

{

    int flag,j;

    printf("please enter a number:");

    scanf("%d",&m);

    flag=IsPrime(m);

       if(flag==1)

        {

            printf("No divisor,It is a prime number");

        }

        else

        {

            for(j=2;j<m;j++)

            {

            if(m %j==0)

            printf("%2d",j);

            }

 

        }

 

    return 0;

}

int IsPrime(int m)

{

    int prime=1;

    int i;

          for(i=2;i<=m-1;i++)

           {

            if(m%i==0)

           {

            prime=0;

            break;

           }

           else

           prime=1;

           }

       return prime;

}

3. Screenshot of program running effect:

 

 

 

 

 

 

 

 

  • Question 5:

If all the different factors of a positive integer m that are less than m (including 1) add up exactly to m itself, then it is called a perfect number (Pefet Numberl) for a long time. For example, 6 is a perfect number, because 6=1+2+3, please write a function IsPerfect() that judges a perfect number, and then judge whether the integer input from the keyboard is a perfect number.

 

1. Problem solving ideas:

Change the function of IsPrime() to determine prime numbers to IsPerfect() to determine whether it is a perfect number. Sum defines the initial value of 0. For non-prime numbers, prime=0 returns to the main function in the outer layer. Variable j starts from 1. If the input number is 0 after the remainder of j is 0, it is a factor, and the factor j is accumulated. If all of m Different factors less than m (including 1) add up exactly to m, indicating that the input number is a perfect number.

 

  1. Source code:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

int m,prime,j;

int IsPerfect(int j);

 

int main ()

{

    int flag,sum=0;

    printf("please enter a number:");

    scanf("%d",&m);

    for(j=1;j<m;j++)

    {

       if(flag==1)

       printf("");

      else

      {

          if(m%j==0)

          sum += j;

      }

    }

    printf("Sum is %d",sum);

    return 0;

}

int IsPerfect(int j)

{

    int prime=1;

    int i;

          for(i=2;i<=j-1;i++)

           {

            if(j%i==0)

           {

            prime=0;

            break;

           }

           else

           prime=1;

           }

       return prime;

}

 

3. Screenshot of program running effect:

 

 

 

 

 

 

 

5. Test results of MOOC online courses

1. Test 1:

 

2. Test 2:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_48450741/article/details/112464708