Experiment 4-2-3 Verification of "Goldbach Conjecture" (20 points)

The famous "Goldbach conjecture" in the field of mathematics roughly means: any even number greater than 2 can always be expressed as the sum of two prime numbers. For example:, 24=5+19where 5and 19are prime numbers. The task of this experiment is to design a program to verify 20亿that even numbers within can be decomposed into the sum of two prime numbers.

Input format:

Enter (2, 2 000 000 000]an even number in a range given on a line N.

Output format:

Decompose the prime numbers N = p + qoutput in the format " " in one line N, all of which p ≤ qare prime numbers. And because such a decomposition is not unique (for example, 24 can also be decomposed into 7+17), it is required that the psmallest solution among all the solutions must be output .

Input sample:

24

Sample output:

24 = 5 + 19

Code:

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

typedef long long int long_int;
int judge_is_prime(int x){
    
    
	int isPrime = 1,i = 3;
	if(x == 1 || x % 2 == 0 && x != 2) isPrime = 0;
	else {
    
    
		for(;i<=sqrt(x);i+=2){
    
    
			if(x % i == 0){
    
    
				isPrime = 0;
				break;
			}
		}
	}
	return isPrime;
}

int main() {
    
    
	long_int N,p,q;
	scanf("%lld",&N);
	// 现在需要编写一个函数判断数是否是素数
	for (p=2;p<=N/2;p++) {
    
    
		// p是较小的一方 
		q = N - p;
		// 成功从n**2级别降低到n层面 
		if (judge_is_prime(p) && judge_is_prime(q)) {
    
    
			printf("%lld = %lld + %lld\n",N,p,q);
			break;
		}
	}
	return 0;
}

Submit screenshot:

Insert picture description here

Problem-solving ideas:

This question made me clearly realize the importance of algorithms! The following are some of the problems encountered when solving this problem:
① The following is the code written at the beginning, the time complexity is about O (n 2) O(n^{2})O ( n2)

# include <stdio.h>
# include <stdlib.h>

typedef long long int long_int;
int judge_is_prime(int a) {
    
    
	int i,value = 1;
	if (a == 2) {
    
    
		value = 1;
	}else {
    
    
		for (i=2;i<a;i++) {
    
    
			if (a % i == 0) {
    
    
			// 能被[2,i)因子整除则不是素数 
				value = 0;
			}
		}
	}
	return value;
} 

int main() {
    
    
	long_int N,p,q;
	scanf("%lld",&N);
	// 现在需要编写一个函数判断数是否是素数
	for (p=2;p<N;p++) {
    
    
		for (q=N-1;q>=2;q--) {
    
    
			if (judge_is_prime(p) && judge_is_prime(q) && p + q == N) {
    
    
				goto sum;
			}
		}
	}
	sum:printf("%d = %d + %d\n",N,p,q);
	return 0;
}

Report an error directly: insert a picture description here

Insert picture description here
② Later, the algorithm was improved, and it was found that the second-level loop was not needed, because p +q == N, so the algorithm became a O(n)hierarchy!Insert picture description here

③ I found that there is still a problem, removing the gotosentence is still not working, so I realized that it was a problem with function writing, so I changed the loop range of judging prime numbers from i < nto i < sqrt(n), and found that there was a problem in the fourth case, so I modified the judgment condition of the function again , Changed to start from 3, add 2 successively...Finally passed! Hard to top

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114643785