Skip to content

Instantly share code, notes, and snippets.

@hackintoshrao
Last active December 26, 2015 06:07
Show Gist options
  • Save hackintoshrao/0be1f3f8d09a235149fc to your computer and use it in GitHub Desktop.
Save hackintoshrao/0be1f3f8d09a235149fc to your computer and use it in GitHub Desktop.
New efficient version of fib.go
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