Created
June 27, 2017 04:29
-
-
Save Poorvak/fb5faab9595554c73ef1385bcf0dc330 to your computer and use it in GitHub Desktop.
Prime Number program in (N*log(pow 1/2)N) complexity
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 <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