Prime generator with sieve Algo:
Tutorial (ENG)
shafaet tutorial (bangla)
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
bool a[11000000]; | |
void siv(){ | |
int n = 10000000; | |
memset(a,true,sizeof(a)); | |
a[1]=false; | |
for(int i=4;i<=n;i=i+2){ | |
a[i]=false; | |
} | |
int root = sqrt(n); | |
for(int i=3;i<=root;i=i+2){ | |
if(a[i]==true){ | |
for(int j=i*i;j<=n;j=j+i){ | |
a[j]=false; | |
} | |
} | |
} | |
} | |
int main(){ | |
siv(); | |
int n; | |
cout<<"Enter a number: "; | |
cin>>n; | |
if(a[n]) cout<<"prime\n"; | |
else cout<<"not prime\n"; | |
} |
No comments:
Post a Comment