Created
May 26, 2020 11:24
-
-
Save decadef20/57cb82827d869875a7014a4ab4b97810 to your computer and use it in GitHub Desktop.
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 | |
import ( | |
"fmt" | |
) | |
func digits(number int, dchnl chan int) { | |
for number != 0 { | |
digit := number % 10 | |
dchnl <- digit | |
number /= 10 | |
} | |
close(dchnl) | |
} | |
func calcSquares(number int, squareop chan int) { | |
sum := 0 | |
dch := make(chan int) | |
go digits(number, dch) | |
for digit := range dch { | |
sum += digit * digit | |
} | |
squareop <- sum | |
} | |
func calcCubes(number int, cubeop chan int) { | |
sum := 0 | |
dch := make(chan int) | |
go digits(number, dch) | |
for digit := range dch { | |
sum += digit * digit * digit | |
} | |
cubeop <- sum | |
} | |
func main() { | |
number := 589 | |
sqrch := make(chan int) | |
cubech := make(chan int) | |
go calcSquares(number, sqrch) | |
go calcCubes(number, cubech) | |
squares, cubes := <-sqrch, <-cubech | |
fmt.Println("Final output", squares+cubes) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment