What if you stored the result of the first time you computed it? Then when you need again, you just look it up instead of recalculating. This simple idea (remembering results to avoid duplicate work) is the core of dynamic programming.
I still remember the first time I read about it. DP (dynamic programming) was like a magic word, but seriously, that's it! You trade space (storing results) for time (avoiding recalculation). Instead of time, you compute each value once, giving you time. Three lines of code. That's all it takes.