Pascal’s triangle vs. scipy-computed binomial coefficients#

Builds Pascal’s triangle by hand via the addition recurrence and confirms every entry matches scipy.special.comb.

from mathematicskit.combinatorics import combinations_count, pascals_triangle
from mathematicskit.combinatorics.visualizers.plots import plot_pascals_triangle

Build and cross-check#

triangle = pascals_triangle(10)
for n, row in enumerate(triangle):
    print(f"n={n}: {row}")
    for k, value in enumerate(row):
        assert value == combinations_count(n, k)
print("\nevery entry matches scipy.special.comb")
n=0: [1]
n=1: [1, 1]
n=2: [1, 2, 1]
n=3: [1, 3, 3, 1]
n=4: [1, 4, 6, 4, 1]
n=5: [1, 5, 10, 10, 5, 1]
n=6: [1, 6, 15, 20, 15, 6, 1]
n=7: [1, 7, 21, 35, 35, 21, 7, 1]
n=8: [1, 8, 28, 56, 70, 56, 28, 8, 1]
n=9: [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

every entry matches scipy.special.comb

Visualize#

plot_pascals_triangle(triangle)
Pascal's triangle
<Axes: title={'center': "Pascal's triangle"}, xlabel='k', ylabel='n'>

Total running time of the script: (0 minutes 0.016 seconds)

Gallery generated by Sphinx-Gallery