#!/usr/bin/env python3 cache = [[{0 : 1} for b in range(10)] for a in range(10)] def f(i, a=0, b=0, fst=1): if i in cache[a][b]: return cache[a][b][i] r = sum((f(i-1, b, n, 0) for n in range(fst, 10-a-b))) cache[a][b][i] = r return r print(f(20))