Project Euler 3: Getting the largest prime factor of a number I'm looking for some general feedback on my solution to Project Euler challenge 3


  The prime factors of 13195 are 5, 7, 13 and 29. What is the largest
  prime factor of the number 600851475143 ?


let p3 () = 
    let rec primeFactors n i primes = 
        if i &gt; n/2L then n::primes else
            let quotient, remainder = Math.DivRem(n, i)
            if remainder = 0L then primeFactors quotient 2L (i::primes)
            else primeFactors n (i + 1L) primes
    primeFactors 600851475143L 2L []
There's nothing really wrong with your code but here's a slight rewrite using pattern matching which is arguably a more functional style than if ... then ... else. I also rearranged the args to eliminate the unnecessary n parameter on the outer function.

let primeFactors = 
    let rec recPrimeFactors primes i = function
        | n when 2L*i &gt; n -&gt; n::primes
        | n -&gt; match n % i with
               | 0L -&gt; recPrimeFactors (i::primes) 2L (n / i)
               | _ -&gt; recPrimeFactors primes (i + 1L) n
    recPrimeFactors [] 2L

600851475143L |&gt; primeFactors |&gt; List.head |&gt; printfn "%d"