Skip to content

Instantly share code, notes, and snippets.

@knsmr
Created March 14, 2015 05:04
Show Gist options
  • Select an option

  • Save knsmr/566b864cfcbea658d62f to your computer and use it in GitHub Desktop.

Select an option

Save knsmr/566b864cfcbea658d62f to your computer and use it in GitHub Desktop.
// 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