Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sol.c File Reference

Problem 7 solution. More...

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

Functions

int main (void)
 Main function.
 

Detailed Description

Problem 7 solution.

See also
Another version: problem_7/sol2.c

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
13{
14 char *sieve;
15 size_t i;
16 unsigned count = 0;
17 size_t n = 1000000;
18 const unsigned target = 10001;
19
20 sieve = (char *)calloc(n, sizeof(char));
21 for (i = 2; i < n; i++)
22 {
23 if (!sieve[i])
24 {
25 size_t j;
26 count++;
27 if (count == target)
28 {
29 printf("%lu\n", i);
30 break;
31 }
32 for (j = i * 2; j < n; j += i)
33 {
34 sieve[j] = 1;
35 }
36 }
37 }
38 free(sieve);
39 return 0;
40}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition malloc_dbg.h:22