Skip to content

Instantly share code, notes, and snippets.

@rxrav
Last active March 8, 2020 12:04
Show Gist options
  • Save rxrav/3b44795a1627fcb94e6163a5eeb44b14 to your computer and use it in GitHub Desktop.
Save rxrav/3b44795a1627fcb94e6163a5eeb44b14 to your computer and use it in GitHub Desktop.
Fibonacci series implementation using dynamic programming technique in Golang
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