Created
August 9, 2018 07:05
-
-
Save lnikon/cf61ff461ccd0fad4a4fb7d9ad3f6797 to your computer and use it in GitHub Desktop.
Prime numbers from 2 to N(O(N^2) worst case 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 <cmath> | |
#include <vector> | |
using ull = unsigned long long; | |
bool isPrime(int n) | |
{ | |
for(int i = 2; i < n / 2; i++) | |
{ | |
if((n % i) == 0) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
auto getPrime(int n) | |
{ | |
std::vector<ull> primes; | |
primes.reserve(n); | |
for(int i = 2; i < n; i++) | |
{ | |
if(isPrime(i)) | |
{ | |
primes.push_back(i); | |
} | |
} | |
return primes; | |
} | |
int main() | |
{ | |
int primeNumber = 15331; | |
auto primes = getPrime(primeNumber); | |
for(const auto& item : primes) | |
{ | |
std::cout << item << ' '; | |
} | |
std::cout << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment