Avatar of shmibs

shmibs's solution

to Nth Prime in the C Track

Published at Jul 13 2018 · 1 comment
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;
}

src/nth_prime.c

#include <stdbool.h>
#include <stdlib.h>

#include <assert.h>
#include <math.h>

#include "nth_prime.h"

static struct {
	bool is_init;
	uint32_t *stack;
	uint32_t count;
	uint32_t mem_width;
} pstack = { false };

static void init_sub(void)
{
	pstack.stack = malloc(256*sizeof(uint32_t));
	assert(pstack.stack != NULL);

	pstack.mem_width = 256;
	pstack.stack[0] = 2;
	pstack.count = 1;
	pstack.is_init = true;
}

static bool is_prime_sub(uint32_t n)
{
	uint32_t i;

	for(i = 0; pstack.stack[i] <= (unsigned)sqrt(n); i++) {
		if( !(n % pstack.stack[i]) )
			return false;
	}

	return true;
}

static void find_prime_sub(void)
{
	uint32_t i = pstack.stack[pstack.count-1] + 1;

	while(!is_prime_sub(i))
		i++;

	if(pstack.count == pstack.mem_width) {
		pstack.mem_width *= 2;
		pstack.stack = realloc(pstack.stack, pstack.mem_width*sizeof(uint32_t));
		assert(pstack.stack != NULL);
	}

	pstack.stack[pstack.count] = i;
	pstack.count++;
}

/* if n is > 203,280,221, the last value for which
 * the resulting prime is containable in a 32bit
 * integer, return 0 */
uint32_t nth(uint32_t n)
{
	if(n > 203280221 || n == 0)
		return 0;

	if(!pstack.is_init)
		init_sub();

	while(pstack.count < n)
		find_prime_sub();

	return pstack.stack[n-1];
}

src/nth_prime.h

#ifndef NTH_PRIME_H
#define NTH_PRIME_H

#include <stdint.h>

uint32_t nth(uint32_t n);

#endif

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of Gamecock
if(n &gt; 203280221 || n == 0)
    return 0;```

Awesome!!

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?