共用方式為


Project Euler Problem #3

Largest prime factor of 600851475143

 

let primeFactors n =

    let isFactor n d = n % d = 0L

    let nextFactor n d = seq {d..n} |> Seq.find (isFactor n)

    (n, 2L, []) |> Seq.unfold (fun (n, d, a) ->

        if isFactor n d then Some(d, ((n / d), d, (d :: a)))

        elif n > d then Some(d, (n, nextFactor n d, a))

        else None)

600851475143L |> primeFactors |> Seq.max