.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/combinatorics/sequences/plot_02_bernoulli_power_sums.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_sequences_plot_02_bernoulli_power_sums.py: Jacob Bernoulli's numbers and sums of powers ================================================== Computes the Bernoulli numbers exactly, uses them to evaluate sums of p-th powers in closed form, and repeats Bernoulli's own boast: the sum of the tenth powers of the first 1000 integers. .. GENERATED FROM PYTHON SOURCE LINES 11-13 .. code-block:: Python from mathematicskit.combinatorics import bernoulli_numbers, sum_of_powers .. GENERATED FROM PYTHON SOURCE LINES 14-16 The first Bernoulli numbers ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 16-20 .. code-block:: Python for m, b in enumerate(bernoulli_numbers(12)): print(f"B_{m:<2d} = {b}") .. rst-class:: sphx-glr-script-out .. code-block:: none B_0 = 1 B_1 = 1/2 B_2 = 1/6 B_3 = 0 B_4 = -1/30 B_5 = 0 B_6 = 1/42 B_7 = 0 B_8 = -1/30 B_9 = 0 B_10 = 5/66 B_11 = 0 B_12 = -691/2730 .. GENERATED FROM PYTHON SOURCE LINES 21-23 Closed-form power sums ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-30 .. code-block:: Python for p in range(5): formula = sum_of_powers(100, p) direct = sum(k**p for k in range(1, 101)) print(f"sum of k^{p} for k = 1..100: formula {formula}, direct {direct}") print(f"\n1^10 + ... + 1000^10 = {sum_of_powers(1000, 10):,}") .. rst-class:: sphx-glr-script-out .. code-block:: none sum of k^0 for k = 1..100: formula 100, direct 100 sum of k^1 for k = 1..100: formula 5050, direct 5050 sum of k^2 for k = 1..100: formula 338350, direct 338350 sum of k^3 for k = 1..100: formula 25502500, direct 25502500 sum of k^4 for k = 1..100: formula 2050333330, direct 2050333330 1^10 + ... + 1000^10 = 91,409,924,241,424,243,424,241,924,242,500 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.001 seconds) .. _sphx_glr_download_api_gallery_combinatorics_sequences_plot_02_bernoulli_power_sums.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_bernoulli_power_sums.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_bernoulli_power_sums.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_bernoulli_power_sums.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_