.. 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_01_euler_totient.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_01_euler_totient.py: Euler's totient function and Euler's theorem =================================================== Euler's :math:`\varphi(n)` counts the integers in :math:`1, \dots, n` coprime to :math:`n`. His 1763 theorem, :math:`a^{\varphi(n)} \equiv 1 \pmod n` for :math:`\gcd(a, n) = 1`, generalizes Fermat's little theorem (:math:`\varphi(p) = p - 1` for a prime :math:`p`). This script checks the definition and the theorem and plots :math:`\varphi(n)`, whose top edge :math:`n - 1` is traced by the primes. .. GENERATED FROM PYTHON SOURCE LINES 15-22 .. code-block:: Python import math import matplotlib.pyplot as plt import numpy as np from mathematicskit.number_theory import euler_totient, fast_mod_pow, is_prime_trial_division .. GENERATED FROM PYTHON SOURCE LINES 23-25 phi(n) counts the coprime residues ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-30 .. code-block:: Python for n in (9, 10, 12, 36, 97): coprime = [k for k in range(1, n + 1) if math.gcd(k, n) == 1] print(f"phi({n}) = {euler_totient(n):>2} (count of coprime k <= n: {len(coprime)})") .. rst-class:: sphx-glr-script-out .. code-block:: none phi(9) = 6 (count of coprime k <= n: 6) phi(10) = 4 (count of coprime k <= n: 4) phi(12) = 4 (count of coprime k <= n: 4) phi(36) = 12 (count of coprime k <= n: 12) phi(97) = 96 (count of coprime k <= n: 96) .. GENERATED FROM PYTHON SOURCE LINES 31-33 Euler's theorem: a^phi(n) = 1 (mod n) ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: Python holds = all(fast_mod_pow(a, euler_totient(n), n) == 1 for n in range(2, 300) for a in range(1, n) if math.gcd(a, n) == 1) print("Euler's theorem holds for every n < 300 and every coprime a:", holds) print(f"e.g. 7^phi(40) = 7^{euler_totient(40)} = {fast_mod_pow(7, euler_totient(40), 40)} (mod 40)") .. rst-class:: sphx-glr-script-out .. code-block:: none Euler's theorem holds for every n < 300 and every coprime a: True e.g. 7^phi(40) = 7^16 = 1 (mod 40) .. GENERATED FROM PYTHON SOURCE LINES 39-41 The totient function up to 1000 ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-54 .. code-block:: Python ns = np.arange(1, 1001) phis = np.array([euler_totient(int(n)) for n in ns]) is_p = np.array([is_prime_trial_division(int(n)) for n in ns]) fig, ax = plt.subplots() ax.plot(ns[~is_p], phis[~is_p], ".", ms=2, color="tab:blue", label="composite n") ax.plot(ns[is_p], phis[is_p], ".", ms=2, color="tab:red", label="prime n: phi(n) = n - 1") ax.set_xlabel("n") ax.set_ylabel("phi(n)") ax.set_title("Euler's totient function") ax.legend() plt.show() .. image-sg:: /api/gallery/number_theory/totient/images/sphx_glr_plot_01_euler_totient_001.png :alt: Euler's totient function :srcset: /api/gallery/number_theory/totient/images/sphx_glr_plot_01_euler_totient_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.047 seconds) .. _sphx_glr_download_api_gallery_number_theory_totient_plot_01_euler_totient.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_euler_totient.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_euler_totient.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_euler_totient.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_