.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/number_theory/totient/plot_02_perfect_numbers.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_number_theory_totient_plot_02_perfect_numbers.py: Perfect numbers and the divisor-sum function =================================================== A perfect number equals the sum of its own proper divisors (:math:`\sigma(n) = 2n`). This script finds the perfect numbers below 10,000 with :func:`~mathematicskit.number_theory.systems.totient.divisor_sum` and checks Euclid's form :math:`2^{p-1}(2^p - 1)` for each. .. GENERATED FROM PYTHON SOURCE LINES 13-15 .. code-block:: Python from mathematicskit.number_theory import divisor_sum .. GENERATED FROM PYTHON SOURCE LINES 16-18 Find perfect numbers up to 10,000 ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 18-22 .. code-block:: Python perfect_numbers = [n for n in range(2, 10000) if divisor_sum(n) == 2 * n] print("perfect numbers below 10000:", perfect_numbers) .. rst-class:: sphx-glr-script-out .. code-block:: none perfect numbers below 10000: [6, 28, 496, 8128] .. GENERATED FROM PYTHON SOURCE LINES 23-25 Each has Euclid's form 2^(p-1) (2^p - 1) ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: Python for n in perfect_numbers: p = (n & -n).bit_length() # 2^(p-1) is the largest power of 2 dividing n print(f" {n} = 2^{p - 1} * (2^{p} - 1) -> {2 ** (p - 1) * (2**p - 1) == n}") .. rst-class:: sphx-glr-script-out .. code-block:: none 6 = 2^1 * (2^2 - 1) -> True 28 = 2^2 * (2^3 - 1) -> True 496 = 2^4 * (2^5 - 1) -> True 8128 = 2^6 * (2^7 - 1) -> True .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.014 seconds) .. _sphx_glr_download_api_gallery_number_theory_totient_plot_02_perfect_numbers.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_perfect_numbers.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_perfect_numbers.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_perfect_numbers.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_