Skip to content

Instantly share code, notes, and snippets.

@Poorvak
Created June 27, 2017 04:29
Show Gist options
  • Save Poorvak/fb5faab9595554c73ef1385bcf0dc330 to your computer and use it in GitHub Desktop.
Save Poorvak/fb5faab9595554c73ef1385bcf0dc330 to your computer and use it in GitHub Desktop.
Prime Number program in (N*log(pow 1/2)N) complexity
#include <iostream>
#include <vector>
using namespace std;
void generatePrime(int N) {
vector <int> arrayList;
vector <int> :: iterator i;
arrayList.push_back(2);
for( int i=3; i<=N; i+=2 ) {
bool isPrime = true;
for( int j=0; arrayList[j]*arrayList[j] <= i; j++ ) {
if( i % arrayList[j] == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
arrayList.push_back(i);
}
}
// Loop for printing the array numbers
for(i=arrayList.begin(); i != arrayList.end(); ++i) {
cout << *i << endl;
}
}
int main(void) {
int N;
cin >> N;
generatePrime(N);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment