I am a new c++ programmer currently working through project Euler, and I've gotten myself stuck on problem 3: find the greatest prime factor of 600851475143.
My friend and I came up with the idea to create this "for" loop, where n is the large integer:
#include <iostream>
usingnamespace std;
int main()
{
longlong n;
cout << "please input a number" << endl;
cin >> n;
int limit = (n/2)+1;
//no prime factor can exceed half the value
int biggest = n;
//initializing biggest to n
for (int i = 2; i < limit; i++)
{
while (n%i==0)
{
biggest = n;
n = n/i;
}
// for every integer that divides evenly into n, divide n by that integer until it does not divide evenly any more. Then, iterate i, and store the biggest value obtained in c. When n=1, c is the greatest prime factor.
}
cout << biggest << endl;
return 0;
//display c and end the program
}
This should ostensibly give the largest prime factor of n. However, when Project Euler's number is the input, it returns 151, which Euler says is wrong.
Is there any way, (without doing my work for me of course), that someone could point me in the direction of my error?