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

20 lines
304 B
Text

module DifferenceOfSquares exposing (..)
squareOfSum : Int -> Int
squareOfSum n =
let
sum =
n * (n + 1) // 2
in
sum * sum
sumOfSquares : Int -> Int
sumOfSquares n =
List.sum (List.map (\m -> m * m) [0..n])
difference : Int -> Int
difference n =
squareOfSum n - sumOfSquares n