C Implementation of Problem 7

View source code here on GitHub!

Includes

Solution

uint32_t p0007()
int main(int argc, char const *argv[])

Note

This function is only present in the Python test runner, or when compiling as a standalone program. It is not present when compiling for the Unity test runner.

 1/*
 2Project Euler Problem 7
 3
 4The prime number infrastructure paid off here
 5
 6Problem:
 7
 8By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
 9
10What is the 10 001st prime number?
11*/
12#ifndef EULER_P0007
13#define EULER_P0007
14#include <stdint.h>
15#include <inttypes.h>
16#include <stdio.h>
17#include "include/macros.h"
18#include "include/primes.h"
19
20uint32_t EMSCRIPTEN_KEEPALIVE p0007() {
21    uint32_t answer, count = 0;
22    prime_sieve ps = prime_sieve0();
23    while (true) {
24        answer = next(ps);
25        if (++count == 10001)
26            break;
27    }
28    free_prime_sieve(ps);
29    return answer;
30}
31
32PROGRAM_TAIL("%" PRIu32, p0007)
33#endif

Tags: c-iterator, prime-number