exercism/go/difference-of-squares/difference_of_squares.go

33 lines
667 B
Go
Raw Normal View History

2016-08-13 23:20:14 +00:00
package diffsquares
import "math"
// SquareOfSums calculates the Square of the sums
// of the first 'num' natural numbers
func SquareOfSums(num int) int {
ret := 0
for num > 0 {
ret += num
num--
}
return int(math.Pow(float64(ret), 2))
}
// SumOfSquares calculates the Sum of the Squares
// of the first 'num' natural numbers
func SumOfSquares(num int) int {
ret := 0
for num > 0 {
ret += int(math.Pow(float64(num), 2))
num--
}
return ret
}
// Difference calculates the difference between
// The SquareOfSums and the SumOfSquares for the
// first 'num' natural numbers
func Difference(num int) int {
return SquareOfSums(num) - SumOfSquares(num)
}