Last active
April 27, 2019 13:09
-
-
Save kylethedeveloper/05fc1d3549a65aefb9930cbf9c42ccc3 to your computer and use it in GitHub Desktop.
Project Euler - Problem 10 - Summation of primes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Project_10.cpp : Summation of primes | |
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. | |
Find the sum of all the primes below two million. | |
*/ | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
long long sum = 0; // long long data type as sum will be too big, starts from 2 | |
bool prime = true; | |
for(int i=3; i<2000000; i+=2){ // until 2 million | |
prime = true; // set prime true, if not it will change | |
for(int j=2; j<=(i/2); j++){ // until half of the number | |
if (i % j == 0) { // check if not prime | |
prime = false; // set prime false so that it won't add to sum | |
} | |
} | |
if(prime){ // if i is prime | |
sum = sum + i; // add it to sum | |
// cout << "Sum: " << sum << endl; // if you want to print sum everytime it finds prime | |
} | |
} | |
cout << "Sum: " << sum << '\n'; // print the final sum | |
return 0; | |
} | |
// Answer is: 142913828922 | |
// it's really long to find the answer... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment