[Golang] Sum Square Difference - Problem 6 - Project Euler


Go solution to sum square difference - Problem 6 - Project Euler. [1]

Problem:

The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Solution:

25502500 - 338350 = 25164150

Run Code on Go Playground

package main

import (
      "fmt"
)

func main() {
      var sumsquare = uint64(1)
      var squaresum = uint64(1)
      for i := uint64(2); i <= 100; i++ {
              squaresum += i
              sumsquare += i * i
      }
      squaresum = squaresum * squaresum
      fmt.Print(squaresum)
      fmt.Print(" - ")
      fmt.Print(sumsquare)
      fmt.Print(" = ")
      fmt.Print(squaresum - sumsquare)
}

Tested on: Go Playground


References:

[1]Sum square difference - Problem 6 - Project Euler