-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumprimes.cpp
More file actions
50 lines (39 loc) · 1018 Bytes
/
sumprimes.cpp
File metadata and controls
50 lines (39 loc) · 1018 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
39
40
41
42
43
44
45
46
47
48
49
50
/**
(c) Sergio Morales 2013
CodeEval Challenge: Sum of Primes
Date Solved: 12/04/13
**/
#include <iostream>
using namespace std;
/**
function: is_prime
description: Determines if the input is prime or not.
return: True if the input is prime
False otherwise
**/
bool is_prime(int num);
int main() {
// Declare variables
int sum = 0; // Summation for primes
int num = 0; // Number of primes currently found
int numprimes = 1000; // Number of primes to be summed
int i = 2; // Count & iterator
while(num < numprimes) {
// If the number is prime, add up to sum and iterate.
if( is_prime(i) ) {
sum += i;
++num;
}
++i;
}
// Final summation of primes
cout << endl << sum << endl;
return 0;
}
bool is_prime(int num) {
for(int i = 2; i < num ; ++i) {
if(num % i == 0)
return false;
}
return true;
}