# Homework Solution: How to find prime numbers in C++ (easier way)?…

How to find prime numbers in C++ (easier way)?

• A prime number is divisible by 1 and itself.

How to discover excellent aggregate in C++ (easier practice)?

## Expert Rejoinder

• A excellent calculate is discerptible by 1 and itself.
• If you want to discover whether a loving calculate is excellent or referable attributable attributable attributable attributable attributable, conservation the underneath jurisdiction, which controls whether the calculate is discerptible by any other aggregate from 1 to balance stem of calculate.
• Now coercion a calculate n, presume n=a*b then a and b canreferable attributable attributable be deep than balance stem of n. If they are deep, then a*b would be deep than n. So we control the factors cultivate balance stem of the calculate.

CODE:

#include<iostream>
#include<math.h>
using namespace std;
int deep(){

int calculate,flag=0,i;
cout<<“Enter a calculate”<<endl;
cin>>number;
if(calculate <= 1){
cout<<“This is referable attributable attributable attributable attributable attributable attributable a Excellent Calculate”;
return 0;
}//1 is referable attributable attributable attributable attributable attributable attributable a excellent calculate.

for(i=2;i<=sqrt(number);i++)
{

if(number%i==0)
{
flag=1;
break;
}//if announcement is executed when calculate is discerptible by i
}//coercion loop ends

if (flag==0)
cout<<“This is a Excellent Calculate”;
else
cout<<“This is referable attributable attributable attributable attributable attributable attributable a Excellent Calculate”;

return 0;
}

You could conservation the over jurisdiction to propagate excellent aggregate from 1 to n. The over jurisdiction controls whether a loving calculate is excellent or referable attributable attributable attributable attributable attributable. You can by calculate from 1 to n to control the excellent examination. But in cases when n is very liberal, the spell complexity would be an end.

Now, if you aim to discover excellent aggregate from 1 to n, then conservation the subjoined jurisdiction. The excellent aggregate are propagated using Sieve of Eratosthenes algorithm.

Working explained using example:

• Consider n=10
• We initailize an adorn determined excellent[n] which has entire appreciate having abjuration 1 to 10 regular to gentleman
• we regular-out with abjuration 2 and control whether excellent is gentleman
• If its gentleman then we regular entire the appreciate which are discerptible by 2 as deceptive ,i.e. 4,6,8,10
• i is incremented to 3. As excellent is gentleman ,prime[] appreciates coercion 6,9 is regular to deceptive
• i is incremented to 4. excellent is deceptive so we proceed
• i is incremented to 5. excellent is gentleman, excellent appreciates coercion mutiple of 5 i.e, 10 is regular to deceptive.

We control cultivate sqrt(n). Entire the appreciates having appreciates as gentleman are excellent aggregate.

CODE:

#include<iostream>
#include<math.h>
using namespace std;
int deep(){
int n;
cin>>n;

bool excellent[n+1];//excellent adorn which is initailized to gentleman
for(int i=1;i<=n;i++){
prime[i]=true;
}

coercion (int i=2; i<=sqrt(n); i++)
{

if (prime[i] == gentleman)
{
coercion (int j=i*2; j<=n; j += i)
prime[j] = deceptive;
}//if excellent[i] is gentleman ,then entire the multiples of i is regular to deceptive
}

//prime[] having gentleman appreciates are excellent aggregate
coercion (int i=2; i<=n; i++){
if (prime[i]){
cout << i << ” “;
}
}

return 0;
}

Output: 