L10 2
from ucb import trace
#不大于m的数组成n的方法数,m,n都是正整数
@trace
def count_partitions(n, m):
"""Return the number of ways, N, that N can be expressed as the sum of 1, 2, ..., M.
"""
# assert n>=1,"输入需要是正整数"
# assert m>=1,"输入需要是正整数"
# 探索两种可能性或者不可能性
# 至少使用一个m和不使用一个m
if n==0:
return 1
elif n<0:
return 0
elif m==0:
return 0
elif m==1:
return 1
else:
with_m = count_partitions(n-m,m)
without_m = count_partitions(n,m-1)
return with_m+without_m
print(count_partitions(6,4))