Problem 10 solution
More...
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
|
int | main (int argc, char *argv[]) |
| Main function. More...
|
|
◆ main()
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Main function.
12{
13 long n = 100;
14 long long sum = 0;
15 char *sieve = NULL;
16
17 if (argc == 2)
18 n = atol(argv[1]);
19
20
21 sieve =
calloc(n,
sizeof(*sieve));
22 if (!sieve)
23 {
24 perror("Unable to allocate memory!");
25 return -1;
26 }
27
28
29
30
31
32
33 for (long i = 2; i < sqrtl(n); i++)
34 {
35
36
37 if (!sieve[i])
38 {
39 for (long j = i * i; j < n + 1; j += i)
40 {
41 sieve[j] = 1;
42 }
43 sum += i;
44 }
45 }
46
47 for (long i = sqrtl(n) + 1; i < n; i++)
48 if (!sieve[i])
49 sum += i;
50
52
53 printf("%ld: %lld\n", n, sum);
54
55 return 0;
56}
#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