Skip to content

Instantly share code, notes, and snippets.

@lnikon
Created August 9, 2018 07:05
Show Gist options
  • Save lnikon/cf61ff461ccd0fad4a4fb7d9ad3f6797 to your computer and use it in GitHub Desktop.
Save lnikon/cf61ff461ccd0fad4a4fb7d9ad3f6797 to your computer and use it in GitHub Desktop.
Prime numbers from 2 to N(O(N^2) worst case complexity)
#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