DEV Community

Discussion on: Project Euler #6 - Sum Square Difference

Collapse
 
comaldave profile image
David Skinner

Go Gopher

goplay.space/#rQ0XekiCknZ

// func SumSquareDifference receives the max range
// and returns the difference of the sumSquare and the squareSum.
func SumSquareDifference(x int) int {
    sum := 0
    sumSquare := 0

    for i := 1; i <= x; i++ {
        sum += i
        sumSquare += i * i
    }

    squareSum := sum * sum

    return squareSum - sumSquare
}

You can see the complete program on the playground link.

  • I have included a block that confirms that my function provides the expected response.
  • I then start a timer so I can print the elapsed time to run the function.
  • After printing the results, I include a comment defining the Output, If the output changes it will generate an error on my computer.
  • The language is Go but it is more a C++ style, I am not the best Go programmer.
  • Code needs to be readable, compilers can do the optimization.