What is the difference between the sum of the squares and the square of the sums?
I first defined a helper function:
let square i = i*i
Then the sum of the squares can be expressed as:
let sum_of_squares = {1..100} |> Seq.map square |> Seq.sum
And the square of the sum is:
let square_of_sum = {1..100} |> Seq.sum |> square
So that the desired difference is:
square_of_sum - sum_of_squares |> printfn "diff = %A"
And there's really nothing much more to say.
No comments:
Post a Comment