🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # vlzware's solution

## to Nth Prime in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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 C page on the Exercism site. 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.

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.

## 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.

### test_nth_prime.c

``````#include "vendor/unity.h"
#include "../src/nth_prime.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_first_prime(void)
{
TEST_ASSERT_EQUAL_UINT32(2, nth(1));
}

void test_second_prime(void)
{
TEST_IGNORE();               // delete this line to run test
TEST_ASSERT_EQUAL_UINT32(3, nth(2));
}

void test_sixth_prime(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT32(13, nth(6));
}

void test_large_prime(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT32(104743, nth(10001));
}

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);

UnityEnd();
return 0;
}``````

### makefile

``````CFLAGS  = -std=c99
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -pedantic
CFLAGS += -Werror

VFLAGS  = --quiet
VFLAGS += --tool=memcheck
VFLAGS += --leak-check=full
VFLAGS += --error-exitcode=1

test: tests.out
@./tests.out

memcheck: tests.out
@valgrind \$(VFLAGS) ./tests.out
@echo "Memory check passed"

clean:
rm -rf *.o *.out *.out.dSYM

tests.out: test/test_nth_prime.c src/nth_prime.c src/nth_prime.h
@echo Compiling \$@
@cc \$(CFLAGS) src/nth_prime.c test/vendor/unity.c test/test_nth_prime.c -o tests.out -lm``````

### src/nth_prime.c

``````#include "nth_prime.h"
#include <stdlib.h>
#include <stdint.h>
#include <math.h>

uint32_t nth(const uint32_t n)
{
if (!n)
return 0;

/* https://en.wikipedia.org/wiki/Prime_number_theorem\
#Approximations_for_the_nth_prime_number */
uint32_t limit =
(uint32_t) (n * log(n) + n * log(log(n)));
if (limit < 6)
limit = 6;

int *sieve = (int*) malloc(sizeof(int) * (limit + 1));
if (sieve == NULL)
return 0;

uint32_t i, j, upto;

for (i = 2; i <= limit; i++)
sieve[i] = 1;

upto = (uint32_t) sqrt(limit);

for (i = 2; i <= upto; i++)
if (sieve[i] == 1)
for (j = i*i; j <= limit; j += i)
sieve[j] = 0;

i = 0;
for (j = 2; j <= limit; j++)
if (sieve[j] == 1)
if (++i == n) {
free(sieve);
return j;
}

free(sieve);
return 0;
}``````

### src/nth_prime.h

``````#ifndef NTH_PRIME_H
#define NTH_PRIME_H
#include <stdint.h>

uint32_t nth(const uint32_t n);

#endif``````