Last active
December 26, 2015 06:07
-
-
Save hackintoshrao/0be1f3f8d09a235149fc to your computer and use it in GitHub Desktop.
New efficient version of fib.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" | |
func main() { | |
var fibMap map[int]int | |
fibMap = make(map[int]int) | |
fibMap[0] = 0 | |
fibMap[1] = 1 | |
fmt.Println(Fib(20, fibMap)) | |
} | |
func Fib(n int, fibMap map[int]int) int { | |
if val, ok := fibMap[n]; ok { | |
return val | |
} | |
fibMap[n] = Fib(n-1, fibMap) + Fib(n-2, fibMap) | |
return fibMap[n] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment