Created
March 14, 2015 05:04
-
-
Save knsmr/566b864cfcbea658d62f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Euler Project | |
| // Problem 45: https://projecteuler.net/problem=45 | |
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| func main() { | |
| var p, h int | |
| pen := seq(func(i int) int { return i * (3 * i - 1) / 2}, 165) | |
| hex := seq(func(i int) int { return i * (2 * i - 1)}, 144) | |
| func() { | |
| h = <-hex | |
| for ;; p = <-pen { | |
| if h == p { | |
| return | |
| } | |
| if p > h { | |
| h = <-hex | |
| } | |
| } | |
| }() | |
| fmt.Println(h) | |
| } | |
| func seq(fn func(int) int, beg int) <-chan int { | |
| c := make(chan int) | |
| go func() { | |
| for i := beg; ; i++ { | |
| c <- fn(i) | |
| } | |
| }() | |
| return c | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment