-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler10.cpp
More file actions
38 lines (33 loc) · 738 Bytes
/
Euler10.cpp
File metadata and controls
38 lines (33 loc) · 738 Bytes
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
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <math.h>
#include <unistd.h>
using namespace std;
//How can we further optimize this?
bool isprime(unsigned long int num){
if(num == 2 || num == 3){
return true;
}
if(num < 2){
return false;
}
if(num%2 == 0 || num%3 == 0){
return false;
}
for(unsigned long int i = 4; i <= pow(num, .5); i++ ){
if(num%i == 0){
return false;
}
}
return true;
}
int main(){
unsigned long int sum = 2;
for(unsigned int i = 3; i < 2000000; i+=2){
if(isprime(i)){
sum += i;
}
}
cout<<"Sum of all primes < 2M: "<<sum<<endl;
}