So I am trying to find the largest prime factor of a fairly large number. This is a question on Project Euler (http://projecteuler.net/problem=3). My problem is that when I do compile and run my program, the command prompt comes up empty, its just a black with no text on it.
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
vector <int> factors;
bool isPrime(int number)
{
bool prime = true;
int maxIndex=number/2;
for (int index=2; index <= maxIndex; index++)
{
if (number % index == 0)
{
prime = false;
break;
}
}
return prime;
}
int Facts()
{
float num = 600851475143;
for (double i=1000000; i<=num; i++)
{
if (fmod(num,i)==0)
factors.push_back(i);
}
}
int main()
{
Facts();
for (int i=0; i<factors.size(); i++)
{
if (isPrime(factors[i]))
cout << i << ", " ;
}
return 0;
}
It has occurred to me that maybe the program is taking a long time because the operation I'm asking it to do is rather time consuming. If that is the case, is there a faster way I can find the answer to this question? I don't see a problem with my code, but if anyone else does catch a problem, feel free to change it.