elm/exercises/difference-of-squares/DifferenceOfSquares.example.elm

21 lines
336 B
Elm
Raw Normal View History

module DifferenceOfSquares exposing (..)
squareOfSum : Int -> Int
squareOfSum n =
let
sum =
n * (n + 1) // 2
in
sum * sum
sumOfSquares : Int -> Int
sumOfSquares n =
2016-12-17 22:15:34 +00:00
List.sum (List.map (\m -> m * m) (List.range 0 n))
difference : Int -> Int
difference n =
squareOfSum n - sumOfSquares n