Sha256: c586ec62664d0f6ef728ed63f08a73852e1e327dce946fc0a13b77050be24914
Contents?: true
Size: 526 Bytes
Versions: 396
Compression:
Stored size: 526 Bytes
Contents
#include "nth_prime.h" #include <cmath> #include <stdexcept> namespace { bool is_prime(int n) { for (int probe = static_cast<int>(std::sqrt(n)); probe > 1; --probe) { if (n % probe == 0) { return false; } } return true; } } int prime::nth(int n) { if (n < 1) { throw std::domain_error("Out of range"); } int candidate = 1; int count = 0; while (count < n) { if (is_prime(++candidate)) { ++count; } } return candidate; }
Version data entries
396 entries across 396 versions & 1 rubygems