[PTA程序设计题目讲解][英文版][6-5 使用函数验证哥德巴赫猜想]

PS: This is my first time writing blogs with English, if there exist any mistakes of grammar or programme, please do not hesitate telling me, many thanks!

Exercise 6-5

        This exersicre require us to write a easy function used for judging a number is a prime number, we will also use this function to prove the Goldbach guess:

        Goldbach guess: Any even number (not less than 6) can be presented by the sum of two odd  and prime number

How to write this function:

int prime (int p);
void Goldbach (int n );

NOTE:

About prime function: If the number p is a prime number, return 1, else return 0.

About Goldbach function: We should do the output form as '' n = p + q ", and p should not greater than q, and both p and q should be prime numbers. But this might lead to the situation that there exist alteernative choice of p and q, (such as 24 can be divided into 5 + 19, but it can be also divided into 7+17), so we should give the output with the smallest p.

Test program example:

#include <stdio.h>
#include <math.h>

int prime( int p );
void Goldbach( int n );
    
int main()
{
    int m, n, i, cnt;

    scanf("%d %d", &m, &n);
    if ( prime(m) != 0 ) printf("%d is a prime number\n", m);
    if ( m < 6 ) m = 6;
    if ( m%2 ) m++;
    cnt = 0;
    for( i=m; i<=n; i+=2 ) {
        Goldbach(i);
        cnt++;
        if ( cnt%5 ) printf(", ");
        else printf("\n");
    }

    return 0;
}


/* your code will be placed here */

Input examples:

89 100

Output examples:

89 is a prime number
90=7+83, 92=3+89, 94=5+89, 96=7+89, 98=19+79
100=3+97, 

Some limitations

Code limitation: 16KB

Time limitation:  400ms

Memory limitation: 64MB

Think how to complete the exercise:

From the exercise we can know that we should complete two functions: 

a prime function to check a number is prime or not;

and a Goldbach function to check the Goldbach guess on the range;

We will use prime fuction to help us complete the Goldbach function,  so we first complete prime function.

Then we can see the main function, it give user two input, m and n, as the start and end of the function, m will also checked by the prime function. the int character i will be used in the for loop, and the int character cnt is a counter to count the output and beautify it.

About prime function:

Actually, I have already completed this function in the blogs I write on the 16 Dec. 2021, it is about to get gthe sum of a series of prime numbers. I use the code as

int prime( int p){
  int result = 1;
  for( int i = 2;i < p; i++){
    if ( p % i == 0) 
    	{result = 0;}
    }
  }
  return result;
}

Basically, I will use the number p to divide every number smaller than it.  It is a way to do, but maybe we can have a better way? You can imagine, if a number is greater than 2/p and smaller than p, then it is impossible for p to fully divide it. So we can make the for loop better. We can use the sieve methodn (The latosthenes sieve), the basically idea is check every prime number from 2 to squart(p), This method do can make the for loop easier! So the code of prime looks like:

int prime( int p){
  int result = 1;
  if (p <= 1) {
    return -1;
  }
  else if (p == 2 || p == 3) {
    return 1;
  }
  else{
    for (int i = 2; i < sqrt(p)+1; i++) {
      if (p % i == 0) {
        result = 0;
        break;
      }
    }
  }
  return result;
}

And then we will use prime function to complete the Goldbach function, and note we should only output the result with the smallest p. So we can first check the number in  Goldbach is a prime number, if so, we divide it with "p + q", if not, we do the next loop.

then we do the loop with p from 3, and if n - p is a prime number, we output the result, if not, we go on the loop, untill the p is greater than the p/2.

So the Goldbach function looks like

void Goldbach( int n ){
  // The number in Goldbach cannot be an odd number
  // Note that we do not need to check the number is greater than 6 cause
  // line the main function has already checked
  if (n % 2 == 1) {
    return;
  }
  // Only even number can be sent in it
  else{
    for (int i = 3; i < n; i+=2) {
      if (prime(i) == 0) {
        continue;
      }
      else if ( prime(n - i) == 1) {
        printf("%d = %d + %d",n,i,n-i );
        break;
      }
    }
  }
}

The double slash are the comments, I always write comments when I have no ideas about the code, and this really helps a lot!

then we can see the result, we run the whole code, and get the result:

 89 100
89 is a prime number
90 = 7 + 83, 92 = 3 + 89, 94 = 5 + 89, 96 = 7 + 89, 98 = 19 + 79
100 = 3 + 97,
请按任意键继续. . .

looks like the same with the example!

We did it!

Some thinking...

I made a little mistake at the prime function at first... That means I write the prime function like:

int prime( int p){
  int result = 1;
  if (p <= 1) {
    return -1;
  }
  else if (p == 2 || p == 3) {
    return 1;
  }
  else{
    for (int i = 2; i < sqrt(p); i++) {
      if (p % i == 0) {
        result = 0;
        break;
      }
    }
  }
  return result;
}

It looks like the same with the answer, but I forget to plus one in the for loop, this will cause the result changed a lot:

89 100
89 is a prime number
90 = 7 + 83, 92 = 3 + 89, 94 = 5 + 89, 96 = 7 + 89, 98 = 9 + 89
100 = 3 + 97,
请按任意键继续. . .

You can see, 9 is alse a prime number! So if we do not plus one, this will cause the whole programme wrong!

In the end, the whole programme looks like:

#include <stdio.h>
#include <math.h>

int prime( int p );
void Goldbach( int n );

int main()
{
    int m, n, i, cnt;

    scanf("%d %d", &m, &n);
    if ( prime(m) != 0 ) printf("%d is a prime number\n", m);
    if ( m < 6 ) m = 6;
    if ( m%2 ) m++;
    cnt = 0;
    for( i=m; i<=n; i+=2 ) {
        Goldbach(i);
        cnt++;
        if ( cnt%5 ) printf(", ");
        else printf("\n");
    }

    return 0;
}


int prime( int p){
  int result = 1;
  if (p <= 1) {
    return -1;
  }
  else if (p == 2 || p == 3) {
    return 1;
  }
  else{
    for (int i = 2; i < sqrt(p); i++) {
      if (p % i == 0) {
        result = 0;
        break;
      }
    }
  }
  return result;
}


void Goldbach( int n ){
  // The number in Goldbach cannot be an odd number
  // Note that we do not need to check the number is greater than 6 cause
  // line the main function has already checked
  if (n % 2 == 1) {
    return;
  }
  // Only even number can be sent in it
  else{
    for (int i = 3; i < n; i+=2) {
      if (prime(i) == 0) {
        continue;
      }
      else if ( prime(n - i) == 1) {
        printf("%d = %d + %d",n,i,n-i );
        break;
      }
    }
  }
}

That's all, hope this can help you!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值