.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/number_theory/primality/plot_03_prime_counting.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_primality_plot_03_prime_counting.py: The prime-counting function and the prime number theorem ================================================================ Sieves all primes up to 100,000 and compares the prime-counting function :math:`\pi(n)` against the prime number theorem's approximation :math:`n/\ln n`. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import math from mathematicskit.number_theory import is_prime_miller_rabin, sieve_of_eratosthenes from mathematicskit.number_theory.visualizers.plots import plot_prime_counting .. GENERATED FROM PYTHON SOURCE LINES 17-19 Sieve and count ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-23 .. code-block:: Python primes = sieve_of_eratosthenes(100000) print(f"pi(100000) = {len(primes)} (prime number theorem estimate: {100000 / math.log(100000):.1f})") .. rst-class:: sphx-glr-script-out .. code-block:: none pi(100000) = 9592 (prime number theorem estimate: 8685.9) .. GENERATED FROM PYTHON SOURCE LINES 24-26 Cross-check the largest sieved prime with Miller-Rabin ----------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: Python largest = int(primes[-1]) print(f"largest prime found: {largest}, Miller-Rabin agrees: {is_prime_miller_rabin(largest)}") .. rst-class:: sphx-glr-script-out .. code-block:: none largest prime found: 99991, Miller-Rabin agrees: True .. GENERATED FROM PYTHON SOURCE LINES 31-33 Plot pi(n) vs. the prime number theorem approximation ----------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-35 .. code-block:: Python plot_prime_counting(2000) .. image-sg:: /api/gallery/number_theory/primality/images/sphx_glr_plot_03_prime_counting_001.png :alt: Prime-counting function :srcset: /api/gallery/number_theory/primality/images/sphx_glr_plot_03_prime_counting_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.029 seconds) .. _sphx_glr_download_api_gallery_number_theory_primality_plot_03_prime_counting.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_prime_counting.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_prime_counting.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_prime_counting.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_