Avatar of godphage

godphage's solution

to Nth Prime in the C++ Track

Published at Oct 14 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

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.


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++11 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.


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.


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

    REQUIRE(2 == prime::nth(1));

    REQUIRE(3 == prime::nth(2));

    REQUIRE(13 == prime::nth(6));

    REQUIRE(104743 == prime::nth(10001));

    REQUIRE_THROWS_AS(prime::nth(0), std::domain_error);


#if !defined(NTH_PRIME_H)
#define NTH_PRIME_H

#include <stdexcept>
#include <vector>
#include <cmath>

namespace prime {

	int nth(long unsigned int);

} // namespace prime



#include "nth_prime.h"

namespace prime {

	void if_prime_push_back(int candidate);

	static std::vector<int> primes = {2, 3};
	static int k = 1;

	void if_prime_push_back(int candidate) {
		bool candidate_is_prime = true;
		int root = std::sqrt(candidate);
		for (int known_prime : primes) {
			// don't look past roots (DO check roots)
			if (known_prime > root)
			if (candidate % known_prime == 0) {
				candidate_is_prime = false;
		if (candidate_is_prime)
	int nth(long unsigned int n) {
		if (n == 0)
			throw std::domain_error("Argument <= 0 out of range for prime::nth(n)");

		while(primes.size() < n) {
			if_prime_push_back(6 * k - 1);
			if_prime_push_back(6 * k + 1);


		return primes.at(n - 1);


What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?