Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
sol.c File Reference

Problem 7 solution More...

#include <stdio.h>
#include <stdlib.h>
Include dependency graph for sol.c:

Functions

int main (void)
 

Detailed Description

Problem 7 solution

Function Documentation

◆ main()

int main ( void  )

Main function

10 {
11  char *sieve;
12  size_t i;
13  unsigned count = 0;
14  size_t n = 1000000;
15  const unsigned target = 10001;
16 
17  sieve = (char *)calloc(n, sizeof(char));
18  for (i = 2; i < n; i++)
19  {
20  if (!sieve[i])
21  {
22  size_t j;
23  count++;
24  if (count == target)
25  {
26  printf("%lu\n", i);
27  break;
28  }
29  for (j = i * 2; j < n; j += i)
30  {
31  sieve[j] = 1;
32  }
33  }
34  }
35  free(sieve);
36  return 0;
37 }