-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem010.c
49 lines (45 loc) · 1.27 KB
/
Problem010.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
/**
* Author: Austin Derrow-Pinion
* Purpose: Solve Problem 10 on Project Euler
* Language: C
*
* Sums up all prime numbers below a given number.
* Project Euler problem was to sum up all primes under
* 2 million. This was very simple using my Sieve of
* Eratosthenes code. Just a small addition to the end
* in order to get the overall sum.
* Can be used to find the sum of primes below any given
* number in range of a long long. To prove this I added in
* commented code at the bottom that sums all primes below
* 1 billion. The answer is 24739512092254535.
*/
long long sieveOfEratosthenes(long long max) {
bool *primes = calloc(max, sizeof(bool));
long long i, j, output = 0;
for(i = 2; i < sqrt(max); i++) {
if(!primes[i]) {
for(j = i*i; j <= max; j+=i) {
primes[j] = true;
}
}
}
for(i = 2; i <= max; i++) {
if(!primes[i]) {
output += i;
}
}
return output;
}
int main() {
long long maxPrime = 2000000;
long long result = sieveOfEratosthenes(maxPrime);
printf("The sum of all primes below %lld is: %lld\n", maxPrime, result);
// maxPrime = 1000000000;
// result = sieveOfEratosthenes(maxPrime);
// printf("The sum of all primes below %lld is: %lld\n", maxPrime, result);
return 0;
}