c#孪生素数查找程序_C ++程序查找最大的质数列表

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#孪生素数查找程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值