Created
March 21, 2017 07:24
-
-
Save alextanhongpin/3cdc794c79639a4f7c9c359ca449fd15 to your computer and use it in GitHub Desktop.
Project Euler #06 - Alternative solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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