Last active
March 8, 2020 12:04
-
-
Save rxrav/3b44795a1627fcb94e6163a5eeb44b14 to your computer and use it in GitHub Desktop.
Fibonacci series implementation using dynamic programming technique 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" | |
var cache = make(map[int]int) | |
var dCounter int | |
func fibDyn(n int) int { | |
dCounter++ | |
if _, ok := cache[n]; !ok { | |
result := fibDyn(n-1) + fibDyn(n-2) | |
cache[n] = result | |
} | |
return cache[n] | |
} | |
func main() { | |
cache[1] = 0 | |
cache[2] = 1 | |
fmt.Println(fibDyn(16)) | |
fmt.Println("Called function", dCounter, "times") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment