Problem 6 — Sum square difference
“The sum of the squares of the first ten natural numbers is,
‘‘‘1^2 + 2^2 + 3^2 … + 10^2 = 385’’’
The square of the sum of the first ten natural numbers is,
(1+2+3...+10)^2 = 55^2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is .
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.”
package main
import "fmt"
const maxNumbers = 100
func main() {
fmt.Println(SoS())
res := SquareOfSum() - SoS()
fmt.Println("=== res ", res)
fmt.Println("======== v2")
// better solution
sumSquares := 0
squaresOfTheSum := 0
for i := 1; i <= 100; i++ {
sumSquares += i * i
squaresOfTheSum += i
}
diffSums := (squaresOfTheSum * squaresOfTheSum) - sumSquares
fmt.Println(diffSums)
}
func SquareOfSum() int {
r := 0
for x := 1; x <= maxNumbers; x++ {
r += x
}
return r * r
}
func SoS() int {
ans := 0
for x := 1; x <= maxNumbers; x++ {
ans += x * x
}
return ans
}