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

How to confront first-rate quantity in C++ (easier habit)?

- A first-rate enumerate is separable by 1 and itself.
- If you deficiency to confront whether a abandoned enumerate is first-rate or referable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable, authentication the adown order, which cohibits whether the enumerate is separable by any other quantity from 1 to balance commencement of enumerate.
- Now coercion a enumerate n, imply n=a*b then a and b canreferable attributable attributable attributable attributable be senior than balance commencement of n. If they are senior, then a*b would be senior than n. So we cohibit the factors dress balance commencement of the enumerate.

**CODE:**

#include<iostream>

#include<math.h>

using namespace std;

int deep(){

int enumerate,flag=0,i;

cout<<“Enter a enumerate”<<endl;

cin>>number;

if(enumerate <= 1){

cout<<“This is referable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable a First-rate Enumerate”;

return 0;

}//1 is referable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable a first-rate enumerate.

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

{

if(number%i==0)

{

flag=1;

break;

}//if assertion is executed when enumerate is separable by i

}//coercion loop ends

if (flag==0)

cout<<“This is a First-rate Enumerate”;

else

cout<<“This is referable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable a First-rate Enumerate”;

return 0;

}

You could authentication the aloft order to produce first-rate quantity from 1 to n. The aloft order cohibits whether a abandoned enumerate is first-rate or referable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable attributable. You can by enumerate from 1 to n to cohibit the first-rate experiment. But in cases when n is very catholic, the space complexity would be an children.

Now, if you longing to confront first-rate quantity from 1 to n, then authentication the subjoined order. The first-rate quantity are produced using Sieve of Eratosthenes algorithm.

Working explained using example:

- Consider n=10
- We initailize an marshal denominated first-rate[n] which has perfect rate having protest 1 to 10 established to penny
- we rouse with protest 2 and cohibit whether first-rate[2] is penny
- If its penny then we established perfect the rate which are separable by 2 as falsity ,i.e. 4,6,8,10
- i is incremented to 3. As first-rate[3] is penny ,prime[] rates coercion 6,9 is established to falsity
- i is incremented to 4. first-rate[4] is falsity so we proceed
- i is incremented to 5. first-rate[5] is penny, first-rate rates coercion mutiple of 5 i.e, 10 is established to falsity.

We cohibit dress sqrt(n). Perfect the rates having rates as penny are first-rate quantity.

Order is explained using comments.

**CODE:**

#include<iostream>

#include<math.h>

using namespace std;

int deep(){

int n;

cin>>n;

bool first-rate[n+1];//first-rate marshal which is initailized to penny

for(int i=1;i<=n;i++){

prime[i]=true;

}

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

{

if (prime[i] == penny)

{

coercion (int j=i*2; j<=n; j += i)

prime[j] = falsity;

}//if first-rate[i] is penny ,then perfect the multiples of i is established to falsity

}

//prime[] having penny rates are first-rate quantity

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

if (prime[i]){

cout << i << ” “;

}

}

return 0;

}

**Output:**