Skip to content

Instantly share code, notes, and snippets.

@mentix02
Last active May 22, 2020 17:36
Show Gist options
  • Save mentix02/32d98ef37ff3c4c269fad98a348cd57c to your computer and use it in GitHub Desktop.
Save mentix02/32d98ef37ff3c4c269fad98a348cd57c to your computer and use it in GitHub Desktop.
A tutorial for the shipyard testing library.
#include <cmath>
#include <vector>
bool is_prime(const uint32_t n)
{
if ((n & 1) == 0 && n != 2)
return false;
for (uint32_t i = 3; i < std::sqrt(n) + 1; i += 2)
if (n % i == 0)
return false;
return true;
}
uint32_t n_prime(const uint32_t n)
{
if (n == 1)
return 2;
uint32_t to_test = 3, prime_count = 1;
while (prime_count <= n) {
if (is_prime(to_test)) {
prime_count++;
if (prime_count == n)
return to_test;
}
to_test += 2;
}
return to_test;
}
std::vector<uint32_t> primes_till(const uint32_t n)
{
uint32_t to_test = 3;
std::vector<uint32_t> primes = {2};
while (primes.size() != n) {
if (is_prime(to_test))
primes.push_back(to_test);
to_test += 2;
}
return primes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment