Skip to content

Instantly share code, notes, and snippets.

@pauladams8
Created May 13, 2019 18:46
Show Gist options
  • Save pauladams8/786d86d199d0c6879b6b576edabc02eb to your computer and use it in GitHub Desktop.
Save pauladams8/786d86d199d0c6879b6b576edabc02eb to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func squareOfSum(n float64) float64 {
/*
* n (n + 1)
* ( --------- ) ** 2
* 2
*/
n = ((n * (n + 1)) / 2)
n = math.Pow(math.Floor(n), 2)
return n
}
func sumOfSquare(n float64) float64 {
/*
* n (n + 1) (2n + 1)
* ( ----------------- )
* 6
*/
n = (n * (n + 1) * (2 * n + 1)) / 6
return n
}
func diff (n float64) float64 {
/*
* Square of sum - sum of square of sum
*/
diff := squareOfSum(n) - sumOfSquare(n)
return diff
}
func floatToString (n float64) string {
n = math.Round(n)
str := fmt.Sprintf("%v", n)
return str
}
func main() {
fmt.Println("Differentiate V1.0")
fmt.Println("A tool to obtain the difference between the square of the sum and the sum of the squares of a given natural number")
fmt.Println("Built by Paul Adams, 4DAS\n")
for {
/*
* Collect input
*/
var n float64
fmt.Println("What number should I compare?")
fmt.Scan(&n)
diff := floatToString(math.Floor(diff(n)))
fmt.Println("\nThe difference is " + diff + "\n")
}
}
@pauladams8
Copy link
Author

Differentiate: A tool to obtain the difference between the square of the sum and the sum of the squares of a given natural number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment