Hi all !!! *
Finding Prime Numbers In A Different Way... *
include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int isprime(ll n)
{
if(n<=3) return 1; if(n%2==0||n%3==0) return 0; for(ll i=5;i*i<=n;i+=6) { if(n%i==0||n%(i+2)==0) return 0; } return 1;
}
int main()
{
//ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
ll N,i;
cout<<"\nEnter the Number N :";cin>>N;
cout<<"\nPrime Numbers <="<<N<<" are : \n";
for(i=2;i<=N;i++)
{
if(isprime(i))
{
cout<<i<<",";
}
}
return 0;
}
If found any corrections please comments... *
Help Appreciated... *
You only need to change the constant
1e5
to the variableN
whose value is input fromcin
orstdin
.is it efficient to answer all the queries where query<=1e6??????
It is classic fast implementation of simple check ,you can check . It is a fast ,but not very fast . If you want very very fast ,you must write sieve of eratosthenes.
My implementation of classic simple check .
If it is a simple ,it will return 1 ,else return 0 .