Last active
September 19, 2020 09:54
-
-
Save aturgarg/8ff3b8e6cd29cfdd6e7b341a91de9575 to your computer and use it in GitHub Desktop.
fibonacci without condition or recursion (in golang)
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
package main | |
import "fmt" | |
func fibonacci() func() int { | |
sum := 0 | |
x := 0 | |
y := 1 | |
return func() int { | |
sum = sum + x | |
x = y | |
y = sum | |
return sum | |
} | |
} | |
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