# Starting in the top left corner of a 2×2 grid,
# and only being able to move to the right and down,
# there are exactly 6 routes to the bottom right corner.
# How many such routes are there through a 20×20 grid?
def lattice_paths
a = (0..19).to_a
puts a.repeated_combination(a.length).to_a.length * 2
end
lattice_paths
这解决了它,虽然我的电脑花了一个多小时。我手工做了一个 3x3 网格,以检查生产中的解决方案。
事后研究,我发现了这个二项式系数:
f(n)=(2n-1; n)
但即使经过一个小时的研究如何计算这些,我仍然不知道如何手动完成,更不用说通过 Ruby。