Find the largest prime factor
In general factoring problem is not easy, see:Theoretical Computer Science Q&A
There is a Pollard-Rho algorithm which you can find some information here: Wikipedia: Factoring integers
I solved that problem of Project Euler too! The output of MATLAB is:
71
839
1471
6857
Elapsed time is 0.002433 seconds.
>>
And the code in MATLAB:
clear;
clc;
tic;
p=600851475143 ;
m=3;
while(sqrt(m)<p)
j=rem(p,m);
if (j==0)
p=p/m;
disp(m);
m=m+2;
else
m=m+2;
end
end
toc;
By the way, there is factor
command in MATLAB that directly outputs the factors of a number. But it has a 2^32 of input limitation size. I removed the limitation of the original code and it responded in 0.027501 seconds!
For numbers of such small magnitude I find it most convenient to use factor, a GNU/Linux command-line utility.