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.

Make sure you have read the "Guides" section of the C track on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test`

task.

```
make test
```

Create just the functions you need 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, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

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

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

```
#include "vendor/unity.h"
#include "../src/nth_prime.h"
void setUp(void)
{
}
void tearDown(void)
{
}
static void test_first_prime(void)
{
TEST_ASSERT_EQUAL_UINT32(2, nth(1));
}
static void test_second_prime(void)
{
TEST_IGNORE(); // delete this line to run test
TEST_ASSERT_EQUAL_UINT32(3, nth(2));
}
static void test_sixth_prime(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT32(13, nth(6));
}
static void test_large_prime(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT32(104743, nth(10001));
}
static void test_weird_case(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT32(0, nth(0));
}
int main(void)
{
UnityBegin("test/test_nth_prime.c");
RUN_TEST(test_first_prime);
RUN_TEST(test_second_prime);
RUN_TEST(test_sixth_prime);
RUN_TEST(test_large_prime);
RUN_TEST(test_weird_case);
return UnityEnd();
}
```

```
#include "nth_prime.h"
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#define bool int
#define true 1
#define false 0
uint32_t nth(uint32_t n) {
if (n == 0) return 0;
int current = 3;
int last_prime = 2;
uint32_t count = 1;
while (count < n) {
if (is_prime(current)) {
count++;
last_prime = current;
}
current += 2;
}
return last_prime;
}
bool is_prime(uint32_t n) {
if (n == 2) return true;
else if (n % 2 == 0 || n < 2) return false;
int count = 0;
for (uint32_t i = 3; i < n; i+=2) if (n % i == 0) count++;
return count == 0;
}
```

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?

Level up your programming skills with 3,089 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments