mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-28 08:13:08 +03:00
23b2a290fb
* Please check this solution to Q7 of Project Euler * rename file * fix code formatting * added doc * updating DIRECTORY.md * added see-also references Co-authored-by: adityasheth305 <43900942+adityasheth305@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
41 lines
767 B
C
41 lines
767 B
C
/**
|
|
* \file
|
|
* \brief [Problem 7](https://projecteuler.net/problem=7) solution.
|
|
* @see Another version: problem_7/sol2.c
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
/** Main function
|
|
* @return 0 on exit
|
|
*/
|
|
int main(void)
|
|
{
|
|
char *sieve;
|
|
size_t i;
|
|
unsigned count = 0;
|
|
size_t n = 1000000;
|
|
const unsigned target = 10001;
|
|
|
|
sieve = (char *)calloc(n, sizeof(char));
|
|
for (i = 2; i < n; i++)
|
|
{
|
|
if (!sieve[i])
|
|
{
|
|
size_t j;
|
|
count++;
|
|
if (count == target)
|
|
{
|
|
printf("%lu\n", i);
|
|
break;
|
|
}
|
|
for (j = i * 2; j < n; j += i)
|
|
{
|
|
sieve[j] = 1;
|
|
}
|
|
}
|
|
}
|
|
free(sieve);
|
|
return 0;
|
|
}
|