Project Euler Solution 7
#
Problem StatementFind the 10001st prime.
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?
#
SolutionI have written a program for checking primality in problem 3 and I just used it to solve this problem.
I used a ‘count’ to keep track of the prime number. Also I have considered only the odd numbers for checking primality.
#
Implementation#include <stdio.h>#include <math.h>
int isPrime(long long int num){ int j, num_sqrt; num_sqrt = sqrt(num); for (j = 2; j <= num_sqrt; j++) { if ((num % j) == 0) return 0; } return 1;}
int main(){ int count; long long int number; count = 2; number = 5; while (number != 0) { if (isPrime(number)) count++; if (count == 10001) break; number += 2; } printf("%d", number); return 0;}