Docs


Problem 7: 10001st prime

Problem

By listing the first six prime numbers: 2, 3, 5, 7, 11 and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

Solution

/*
 * 10001st Prime
 * Problem 7 <https://projecteuler.net/problem=7>
 *
 * Project Euler
 *
 * By Ankit R Gadiya
 */

#include <stdio.h>

long int largest_factor (long int num, int fact);

int main (void)
{
	int prime_counter = 0;
	long int num = 2, largest_fact;

	while (prime_counter != 10001) {
		largest_fact = largest_factor(num, 2);

		if (largest_fact == num)
			prime_counter++;

		num++;
	}

	printf("%ld\n", --num);

	return 0;
}

long int largest_factor (long int num, int fact)
{
	if (num == 1) {
		return fact;
	} else {
		while (fact * fact <= num) {
			if (num % fact == 0)
				return largest_factor (num / fact, fact);
			else
				fact++;
		}
		return num;
	}
}

prob7.c

Result

104743