Browse Source

Iteration 1

master
Jim Infield 9 years ago
parent
commit
f82bf108d3
  1. 31
      difference_of_squares.go

31
difference_of_squares.go

@ -0,0 +1,31 @@
// Package diffsquares provides a utility for the determination of the
// difference between the square of sums and sum of squares on a given
// number range.
package diffsquares
// SquareOfSums returns the square of the sum of the range of natural
// numbers from 1 to the given value.
func SquareOfSums(n int) int {
var s int
for i := 1; i <= n; i++ {
s += i
}
return s * s
}
// SumOfSquares returns the sum of the squares of the range of natural
// numbers from 1 to the given value.
func SumOfSquares(n int) int {
var s int
for i := 1; i <= n; i++ {
s += i * i
}
return s
}
// Difference returns the difference between the sum of squares and the
// square of sums for the range of natural number up to the given value.
func Difference(n int) int {
return SquareOfSums(n) - SumOfSquares(n)
}
Loading…
Cancel
Save