I am doing Project Euler Problem 3, but I seem to get stuck. I have written some code that makes sense (at least to me) but keeps printing the answer to 137.
This is the problem:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
#include <iostream>
#include <cmath>
using namespace std;
bool isprime(int);
int calcprime(int);
int main()
{
int number = sqrt(600851475143);
int biggestprime = calcprime(number);
cout << "The biggest prime is: " << biggestprime << endl;
}
bool isprime(int number)
{
for (int i = 2; i <= sqrt(number); i++)
{
if (number % i == 0)
return false;
}
return true;
}
int calcprime(int number)
{
for (int i = number; i > 2; i--)
{
if ((number % i == 0) && (isprime(i)))
{
return i;
}
}
}