Alright, I'm working on Project Euler Problem 234. I read it over, thought - well that's easy, and set to it.
My code works for the two test cases (n <= 15 and n <= 1000) however upon trying to do the full, large number, its seemingly just sitting there. Now, is my code just incredibly inefficient, or am I running into an issue with data sizes somewhere along the way that I hadn't thought of? It was running for atleast 10 minutes on an 800mhz machine without spitting out an answer.
Thanks for any help in advance.
http://pastebin.com/m7f92f352
My code works for the two test cases (n <= 15 and n <= 1000) however upon trying to do the full, large number, its seemingly just sitting there. Now, is my code just incredibly inefficient, or am I running into an issue with data sizes somewhere along the way that I hadn't thought of? It was running for atleast 10 minutes on an 800mhz machine without spitting out an answer.
Thanks for any help in advance.
http://pastebin.com/m7f92f352
Code:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<long> vPrimes;
void sieve(long max);
long lps(long long n);
long ups(long long n);
int main()
{
sieve(2000000);
long long sum = 0;
for (long long n = 4; n <= 999966663333LL; n++)
if (n % lps(n) == 0 && n % ups(n) != 0 || n % lps(n) != 0 && n % ups(n) == 0)
sum += n;
cout << sum << endl;
system("pause");
}
long lps(long long n)
{
double nDbl = sqrt(double(n));
for (long i = 0; i < vPrimes.size(); i++)
if (vPrimes[i] == nDbl)
return vPrimes[i];
else if (i > 0 && nDbl > vPrimes[i - 1] && nDbl < vPrimes[i])
return vPrimes[i - 1];
else if (i > 0 && nDbl > vPrimes[i] && nDbl < vPrimes[i + 1])
return vPrimes[i];
}
long ups(long long n)
{
double nDbl = sqrt(double(n));
for (long i = 0; i < vPrimes.size(); i++)
if (vPrimes[i] == nDbl)
return vPrimes[i];
else if (i > 0 && nDbl > vPrimes[i - 1] && nDbl < vPrimes[i])
return vPrimes[i];
else if (i > 0 && nDbl > vPrimes[i] && nDbl < vPrimes[i + 1])
return vPrimes[i + 1];
}
void sieve(long max)
{
vector<bool> vNotPrime;
vNotPrime.resize(max);
for (long i = 2; i < max; i++)
if (!vNotPrime[i])
{
vPrimes.push_back(i);
for (long a = i + i; a < max; a += i)
vNotPrime[a] = true;
}
}