Created
March 21, 2017 07:00
-
-
Save alextanhongpin/befe93ba156186f381af1384f73dc210 to your computer and use it in GitHub Desktop.
Project Euler #6: Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
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(101) | |
}() | |
go func() { | |
b <- squareOfSum(101) | |
}() | |
fmt.Println(<-b - <-a) | |
} | |
func sumOfSquare(n int) int { | |
count := 0 | |
for i := 1; i < n; i++ { | |
fmt.Println("at a") | |
count += int(math.Pow(float64(i), 2)) | |
} | |
return count | |
} | |
func squareOfSum(n int) int { | |
count := 0 | |
for i := 1; i < n; i++ { | |
fmt.Println("at b") | |
count += i | |
} | |
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