Created
February 24, 2019 03:25
-
-
Save fractalbach/7717c49d3ec042df1f342822091f5013 to your computer and use it in GitHub Desktop.
Staircase Problem
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" | |
"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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.golang.org/p/5OZjwgxyM7v