Your program is correct, but you are not returning the greatest prime factor of 600851475143
; you are dividing it by itself right before you return it! To “fix” your code, all I did was add the following commented line in the body of Prob_3
:
long a = 600851475143;
int e = 1;
for (long i = 0; i < 10; i++)
{
if (a % 2 == 0 && a != 1)
{
a = a / 2;
}
else if (a % 2 != 0 && a % (e + 2) == 0 && a != 1)
{
if (a / (e + 2) == 1) break; // !
a = a / (e + 2);
}
while (a % 2 != 0 && a % (e + 2) != 0 && a != 1)
{
e = e + 2;
}
}
return a;
What it does is simply act as a base case.
3
solved My version of a solution to the infamous Euler Number 3 C# [closed]