Home >>Cpp Programs >C++ program to find largest list of prime numbers
In this example, we will see a C++ program to find the largest list of prime numbers.
Any number can be expressed as summation of longest list of prime numbers only when it is expressed only with 2 and 3.
Program:
#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;
}