c#孪生素数查找程序
Problem statement:
问题陈述:
You are given a number N, you have to find the largest list of prime numbers that will give N after summation of the list.
给您一个数字N ,您必须找到最大的质数列表,该列表加总后将得出N。
Example:
例:
Sample Input:
7
2
Sample Output:
2 2 3
2
Explanation:
说明:
7 can be expressed as summation of 2+2+3
2 can be expressed as summation of 2
Basic Idea:
基本思路:
A number can be expressed as summation of longest list of prime numbers only when it is expressed only with 2 and 3.
仅当数字仅用2和3表示时,才可以将其表示为最长质数列表的总和。
Algorithm:
算法:
prime_List(N)
1. if(N%2)==0
2. Express it as summation of (N/2) number of 2
3. Else if(N%2)!=0
//So that N can be even, for example N=19, so now N=19-3=16
4. Set N=N-3
5. Print (N/2) numbers of 2
6. Print 3
7. END of Program.
C++ program
C ++程序
#include<bits/stdc++.h>
using namespace std;
int list_prime(int n)
{
int i;
if(n<2)
{
cout<<"Summation not possible\n";
}
//if n can be divided by 2, then we can express
//it as summation of 2 only
else if(n%2==0)
{
for(i=1;i<=n/2;i++)
{
cout<<2<<" ";
}
}
else
{
//making the number even
n=n-3;
//n can be divided by 2 now
for(i=1;i<=n/2;i++)
cout<<2<<" ";
//add 3, and the summation will be now n
cout<<3<<" ";
}
}
int main()
{
int i,n;
cin>>n;
list_prime(n);
return 0;
}
Output
输出量
7
2 2 3
翻译自: https://www.includehelp.com/cpp-programs/find-largest-list-of-prime-numbers.aspx
c#孪生素数查找程序