-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.cpp
42 lines (38 loc) · 814 Bytes
/
10.cpp
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
#include <stdio.h>
#include <string.h>
int is_prime_fast(long n) {
if (n == 2 || n == 3) {
return 1;
} else if (n % 2 == 0 || n % 3 == 0) {
return 0;
} else {
for (long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return 0;
}
}
return 1;
}
}
int is_prime(long n) {
long max = n * 0.5 + 1;
for(long i = 2; i < max; i++) {
if(n % i == 0) return 0;
}
return 1;
}
void sum_primes(long n) {
long sum = 0;
for(long i = 2; i < n; i++) {
if(is_prime_fast(i)) {
//printf("%li\n", i);
sum += i;
}
}
printf("sum of primes below %li: %li\n", n, sum);
}
int main() {
sum_primes(10);
sum_primes(2e6);
return 1;
}