Skip to content

Instantly share code, notes, and snippets.

@quintessence
Last active January 2, 2016 01:19
Show Gist options
  • Save quintessence/8229681 to your computer and use it in GitHub Desktop.
Save quintessence/8229681 to your computer and use it in GitHub Desktop.
Project Euler #10
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Find the sum of the primes less than 2000000
vector<int> primes;
primes.push_back(2);
int maxprime = 2000000;
int primesize = primes.size();
int newprime;
int j;
for (int i = 2; i < maxprime; i++){
j = 0;
newprime = 1;
while (j < primesize) {
if (i % primes[j] == 0) {
// i % j is the remainder of i/j
newprime = 0;
}
j++;
}
if (newprime == 1) {
primes.push_back(i);
cout << i << endl;
}
primesize = primes.size();
}
primesize = primes.size();
long long sum = 0;
for (int i = 0; i < primesize; i++){
//cout << primes[i] << endl;
sum += primes[i];
}
cout << "The sum of the primes less than " << primes[primesize-1] << " is " << sum << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment