2020-06-05 19:20:25 +03:00
|
|
|
/**
|
|
|
|
* \file
|
2020-09-03 15:51:59 +03:00
|
|
|
* \brief [Problem 7](https://projecteuler.net/problem=7) solution.
|
|
|
|
* @see Another version: problem_7/sol2.c
|
2020-06-05 19:20:25 +03:00
|
|
|
*/
|
2019-10-01 17:21:55 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-09-03 15:51:59 +03:00
|
|
|
/** Main function
|
|
|
|
* @return 0 on exit
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
int main(void)
|
|
|
|
{
|
2019-10-01 17:21:55 +03:00
|
|
|
char *sieve;
|
|
|
|
size_t i;
|
|
|
|
unsigned count = 0;
|
|
|
|
size_t n = 1000000;
|
|
|
|
const unsigned target = 10001;
|
|
|
|
|
2020-06-05 19:20:25 +03:00
|
|
|
sieve = (char *)calloc(n, sizeof(char));
|
2020-05-29 23:23:24 +03:00
|
|
|
for (i = 2; i < n; i++)
|
|
|
|
{
|
|
|
|
if (!sieve[i])
|
|
|
|
{
|
|
|
|
size_t j;
|
2019-10-01 17:21:55 +03:00
|
|
|
count++;
|
2020-05-29 23:23:24 +03:00
|
|
|
if (count == target)
|
|
|
|
{
|
2019-10-01 17:21:55 +03:00
|
|
|
printf("%lu\n", i);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
for (j = i * 2; j < n; j += i)
|
|
|
|
{
|
2019-10-01 17:21:55 +03:00
|
|
|
sieve[j] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(sieve);
|
|
|
|
return 0;
|
|
|
|
}
|