Last active
February 13, 2017 14:11
-
-
Save HendrixString/b032016224849993a13e59c2878b69b8 to your computer and use it in GitHub Desktop.
Golang Fibonnaci
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 { | |
var n, f_n_1, f_n_2 = -1, 1, 1 | |
return func() int { | |
n++ | |
if n == 0 { | |
return f_n_2 | |
} else if n == 1 { | |
return f_n_1 | |
} | |
f_n_2, f_n_1 = f_n_1, f_n_2 + f_n_1 | |
return f_n_1 | |
} | |
} | |
func main() { | |
next := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(next()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment