Quick actions

cmd+k|ctrl+k

Navigation

Languages

Sum Div

Snippet info

Language

Fsharp

Visibility

public

Author

orionblastar

Created

2015-07-26T21:42:18Z

Updated

2015-07-26T21:42:18Z

let sumOfDivisors n =
    let rec loop current max acc =
        if current > max then
            acc
        else
            if n % current = 0 then
                loop (current + 1) max (acc + current)
            else
                loop (current + 1) max acc
    let start = 2
    let max = n / 2     (* largest factor, apart from n, cannot be > n / 2 *)
    let minSum = 1 + n  (* 1 and n are already factors of n *)
    loop start max minSum

printfn "%d" (sumOfDivisors 10)
(* prints 18, because the sum of 10's divisors is 1 + 2 + 5 + 10 = 18 *)
INFO