Nov-23-2020, 08:42 PM
(This post was last modified: Nov-23-2020, 08:42 PM by deanhystad.)
You can also make recursion a lot faster by using less of it.
def pascal_re(n): def nextrow(r): '''gives next row of pascal's triangle from previous row''' prerow = [1].append(prerow) # Makes a new list. Fixes shared list problem. for i in range(1, len(prerow) - 1): prerow[i] += prerow[i+1] return prerow if n == 1: return [[1]] tmp = pascal_re(n - 1) # Compute once and use twice return tmp + [nextrow(tmp[-1])] print(pascal_re(25))