#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cout<<"enter the no till which prime nos to be found :"<<endl;
cin>>n;
vector<int> prime(n+1,0);// we want n index too
for(int i=2;i<=n;i++) {
if(prime[i]==0) {
for(int j=i*i;j<=n;j=j+i) {
prime[j]=1;
}
}
}
cout<<"prime numbers are: "<<endl;
for(int i=2;i<=n;i++) {
if(prime[i]==0) {
cout<<i<<endl;
}
}
return 0;
}
above code should be working for n>=100000 since n(log(log(n))) will equal to 400000. writing below part just so that the question may be posted /The main idea here is that every value given to p will be prime, because if it were composite it would be marked as a multiple of some other, smaller prime. Note that some of the numbers may be marked more than once (e.g., 15 will be marked both for 3 and 5)./
Aucun commentaire:
Enregistrer un commentaire