Project Euler - problem 010

August 1st, 2007 by Daniel Høyer Iversen

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below one million.

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
 
 
#include <iostream>
#include <vector>
using namespace std;
 
#define sizeOfPrime 1000000
 
//declare methods
bool isPrimeNumber(int num);
 
int main ()
{
 
long long int res(1);
 
for(int currentPrime = 1; currentPrime< sizeOfPrime; currentPrime += 2){
if (isPrimeNumber(currentPrime) ){
res += currentPrime;
}
}
 
cout << "resultatet er ";
cout << res << "\n";
 
return 0;
}vector<int> numbers;
 
 
 
// methods
bool isPrimeNumber(int num){
// static vector<int> primeNumbers;
 
for(int i = 3; (i-1)*(i-1) < num ; i+=2){
if( num % i == 0){
return false;
}
}
 
//primeNumbers.push_back(num);
 
return true;
}
Do you want to use this code?

0 Responses to “Project Euler - problem 010”

  1. No Comments

Leave a Response