What is the largest prime factor of the number 600851475143 ?

I tried the most naive possible solution, and that solved the problem in less than 1ms, so I didn’t bother optimizing this at all. I can’t help but think that this solution could be written more elegantly using pattern matching, though. Any suggestions?

module Problem3 =
let rec factors n m ms =
if n % m = 0L then
factors (n/m) m (m::ms)
elif n > m then
factors n (m+1L) ms
else
ms
let ans = factors 600851475143L 2L [] |> List.head

### Like this:

Like Loading...

*Related*

[…] trivial, given the solution to a previous problem. I ended up re-writing the factors function from problem 3, because the old function needed to handle 64 bit values, and this doesn’t. There’s a […]