 # ezquire's solution

## to Nth Prime in the C++ Track

Published at Oct 12 2019 · 0 comments
Instructions
Test suite
Solution

Given a number n, determine what the nth prime is.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.

## Getting Started

Make sure you have read the Installing and Running the Tests pages for C++ on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

``````#if defined(EXERCISM_RUN_ALL_TESTS)
``````

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++14 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.

## Source

A variation on Problem 7 at Project Euler http://projecteuler.net/problem=7

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### nth_prime_test.cpp

``````#include "nth_prime.h"
#include "test/catch.hpp"
#include <stdexcept>

TEST_CASE("first")
{
REQUIRE(2 == nth_prime::nth(1));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("second")
{
REQUIRE(3 == nth_prime::nth(2));
}

TEST_CASE("sixth")
{
REQUIRE(13 == nth_prime::nth(6));
}

TEST_CASE("big_prime")
{
REQUIRE(104743 == nth_prime::nth(10001));
}

TEST_CASE("weird_case")
{
REQUIRE_THROWS_AS(nth_prime::nth(0), std::domain_error);
}
#endif``````

### nth_prime.cpp

``````#include "nth_prime.h"
#include <vector>
#include <iostream>
#include <cmath>
namespace nth_prime {
int nth(int n) {
if (n < 1)
throw std::domain_error("the given n is out of bounds");
if (n == 1)
return 2;
if (n == 2)
return 3;
int limit, root, count = 1;
limit = (int)(n*(log(n) + log(log(n)))) + 3;
root = (int)sqrt(limit) + 1;
limit = (limit-1)/2;
root = root/2 - 1;
std::vector<bool> sieve(limit, true);
for(int i = 0; i < root; ++i) {
if (sieve[i]) {
++count;
for(int j = 2*i*(i+3)+3, p = 2*i+3; j < limit; j += p) {
sieve[j] = false;
}
}
}
int p;
for(p = root; count < n; ++p) {
if (sieve[p]) {
++count;
}
}
return 2*p+1;
}
}  // namespace nth_prime``````

### nth_prime.h

``````#if !defined(NTH_PRIME_H)
#define NTH_PRIME_H

namespace nth_prime {
int nth(int);
}  // namespace nth_prime

#endif // NTH_PRIME_H``````