Last active
December 15, 2015 08:59
-
-
Save gylaz/5235308 to your computer and use it in GitHub Desktop.
Fibonacci in Go
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" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
current_fib_number := 0 | |
prev_fib_number := 0 | |
f := func() int { | |
result := prev_fib_number + current_fib_number | |
prev_fib_number = current_fib_number | |
if result == 0 { | |
current_fib_number += 1 | |
} else { | |
current_fib_number = result | |
} | |
return result | |
} | |
return f | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment