Skip to content
Browse files

Solving problem2 w/go using a closure

  • Loading branch information...
1 parent 3c43f84 commit c25dfdf42cac9079e924adc90f02144e4bed008b @coleifer committed Apr 20, 2012
Showing with 22 additions and 0 deletions.
  1. +22 −0 go/problem02.go
View
22 go/problem02.go
@@ -0,0 +1,22 @@
+package main
+
+import "fmt"
+
+func fib() func() int64 {
+ var a, b int64 = 1, 0
+ return func() int64 {
+ a, b = a+b, a
+ return a
+ }
+}
+
+func main() {
+ fib_gen := fib()
+ var i, sum, max int64 = 0, 0, 4 * 1e6
+ for i = 0; i < max; i = fib_gen() {
+ if i % 2 == 0 {
+ sum += i
+ }
+ }
+ fmt.Printf("%d\n", sum)
+}

0 comments on commit c25dfdf

Please sign in to comment.
Something went wrong with that request. Please try again.