.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/combinatorics/pascals_triangle/plot_01_triangle_and_binomials.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_combinatorics_pascals_triangle_plot_01_triangle_and_binomials.py: 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``. .. GENERATED FROM PYTHON SOURCE LINES 10-13 .. code-block:: Python from mathematicskit.combinatorics import combinations_count, pascals_triangle from mathematicskit.combinatorics.visualizers.plots import plot_pascals_triangle .. GENERATED FROM PYTHON SOURCE LINES 14-16 Build and cross-check ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 16-24 .. code-block:: Python 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") .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 25-27 Visualize ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-29 .. code-block:: Python plot_pascals_triangle(triangle) .. image-sg:: /api/gallery/combinatorics/pascals_triangle/images/sphx_glr_plot_01_triangle_and_binomials_001.png :alt: Pascal's triangle :srcset: /api/gallery/combinatorics/pascals_triangle/images/sphx_glr_plot_01_triangle_and_binomials_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.016 seconds) .. _sphx_glr_download_api_gallery_combinatorics_pascals_triangle_plot_01_triangle_and_binomials.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_triangle_and_binomials.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_triangle_and_binomials.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_triangle_and_binomials.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_