Skip to content

Instantly share code, notes, and snippets.

@alextanhongpin
Created March 21, 2017 07:24
Show Gist options
  • Save alextanhongpin/3cdc794c79639a4f7c9c359ca449fd15 to your computer and use it in GitHub Desktop.
Save alextanhongpin/3cdc794c79639a4f7c9c359ca449fd15 to your computer and use it in GitHub Desktop.
Project Euler #06 - Alternative solution
package main
// Answer: 25164150
// Find the difference between the sum of the squares
// of the first one hundred natural numbers and the square of the sum.
import (
"fmt"
"math"
)
func main() {
a := make(chan int)
b := make(chan int)
go func() {
a <- sumOfSquare(100)
close(a)
}()
go func() {
b <- squareOfSum(100)
close(b)
}()
fmt.Println(<-b - <-a)
}
func sumOfSquare(n int) int {
return ((n * (n + 1)) * (2 * n + 1)) / 6
}
func squareOfSum(n int) int {
count := (n * (n + 1)) / 2
return int(math.Pow(float64(count), 2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment