I wrote some code to solve this question and I was wondering if it was efficient or not.
I just started learning code last week and I would like to get some input so I can get in the habit of writing the most efficient code possible.
#include <iostream>
using namespace std;
// What is the largest prime factor of the number 600851475143?
int main()
{
long long greaterFactor = 600851475143;
bool factorIsPrime = true;
for (long long i = 3; i <= greaterFactor; i += 2)
{
if (greaterFactor % i == 0) // checks if i is a factor
{
if (greaterFactor / i >= i) // if the other factor (not i) of greaterFactor is greater or equal (for squared numbers)
{
greaterFactor /= i; // greaterFactor becomes the other factor
i -= 2; // in case prime factor shows up more than 1 time (eg - > 11 * 11 * 17 -> 11 & 11 * 17 -> 11 & 11 & 17
} // then continues to check next integers to completely factor smaller factors until greaterFactor has no more factors
}
}
cout << "The largest prime factor of the number 600851475143 is:\n" << greaterFactor << endl;
return 0;
} // end main()