Skip to content

Instantly share code, notes, and snippets.

@fractalbach
Created February 24, 2019 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fractalbach/7717c49d3ec042df1f342822091f5013 to your computer and use it in GitHub Desktop.
Save fractalbach/7717c49d3ec042df1f342822091f5013 to your computer and use it in GitHub Desktop.
Staircase Problem
package main
import (
"fmt"
"math/big"
)
var memo = map[int]*big.Int{
0: big.NewInt(0),
1: big.NewInt(1),
2: big.NewInt(2),
3: big.NewInt(4),
}
func nWays(n int) *big.Int {
if n <= 0 {
return big.NewInt(0)
}
v, ok := memo[n]
if ok {
return v
}
sum := big.NewInt(0)
sum.Add(sum, nWays(n-1))
sum.Add(sum, nWays(n-2))
sum.Add(sum, nWays(n-3))
memo[n] = sum
return sum
}
func displayMemo() {
for i := 0; i < len(memo); i++ {
fmt.Printf("f(%d) = %d\n", i, memo[i])
}
}
func main() {
n := 1000
nWays(n)
displayMemo()
}
@fractalbach
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment