2019-2020 The Fifth Session of the Joint Training Competition of University, Middle and Elementary School Students [DFS] [Goldbach Conjecture]

Title Description
Prime numbers, also known as prime numbers, refer to positive integers with no divisors except 1 and itself. For example, 2, 3, 5, and 13 are all composite numbers, but 4, 9, 12, and 18 are not.
Although prime numbers cannot be decomposed into the product of integers other than 1 and itself, they can be decomposed into the sum of more prime numbers.
You need to program to find out how many different prime numbers can be decomposed into a positive integer.
For example, 21 = 2 + 19 is a legal decomposition method for 21. 21 = 2 + 3 + 5 + 11 is the way to decompose into the most prime numbers.
Enter
an integer n (10≤n≤200).
The output
n can be decomposed into the sum of how many different prime numbers at most.
Sample input Copy
21
Sample output Copy
4

Topic analysis

The first is the DFS solution. Because the data range is relatively small, we can store the prime numbers in an array first, and
we can choose to add or not add to the current prime numbers.

#include<bits/stdc++.h>
using namespace std;
int count,su[250],n,Max;
int prime(int a) {
    
    
	for(int i=2; i*i<=a; i++)
		if(a%i==0)
			return 0;
	return 1;
}
void dfs(int t,int count,int num) {
    
    
	if(t==n) {
    
    
		Max=max(Max,count);
		return ;
	}
	if(  su[num]>n ||  t>n)  return ;       //避免死循环 
	dfs(t+su[num],count+1,num+1);
	dfs(t,count,num+1);
}
int main() {
    
    
	int  k=0;
	for(int  i=2; i<=250; i++)
		if(prime(i))     //存素数 
			su[k++]=i; 
	cin >> n ;
	
	dfs(0,0,0);
	printf("%d",Max);
	return 0;
}

two

Using Goldbach's conjecture

For composite numbers greater than 6, it can be decomposed into the sum of two prime numbers.
We can increase sequentially from small to large prime numbers in the array, and with the sum recording, recording with the number of J, was added until the sum of the N range
terminate the program
number at this time is out of range-n-sum;
that is we want to Remove sum-n from the previous number;
n=sum-(sum-n),
to satisfy the condition that the sum is n.
Because in the process of adding, it is added from small to large,
1. If sum-n is a composite number, two prime numbers from the first J numbers must be combined to form sum-n; output J-2;
2 if sum-n is a prime number. Then this prime number must have appeared in the first J numbers, just remove it, and output J-1
3 If sum==n during the incrementing process, the output J is a positive solution

#include<bits/stdc++.h>
using namespace std;
int prime(int a)
{
    
    
    for(int i=2;i*i<=a;i++)
    if(a%i==0)
    return 0;
    return 1;
}
int main() {
    
    
int  su[202],k=0,sum=0,j;
for(int  i=2;i<=250;i++)
if(prime(i))
su[k++]=i;
int n;  cin >> n ;
for( j=0;sum<n;j++)
sum=sum+su[j];
if(sum==n)
{
    
    
    printf("%d",j);
    return 0;
}
 
sum=sum-n;
if(prime(sum))
j=j-1;
else
j=j-2;
printf("%d",j); 
 
 
 
    return 0;
}

Guess you like

Origin blog.csdn.net/wmy0536/article/details/103322651