<jupyter_start><jupyter_text>> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.  15.5. A bit of number theory with SymPy<jupyter_code>from sympy import *
init_printing()
import sympy.ntheory as nt<jupyter_output><empty_output><jupyter_text>Prime numbers Test whether a number is prime.<jupyter_code>nt.isprime(2011)<jupyter_output><empty_output><jupyter_text>Find the next prime after a given number.<jupyter_code>nt.nextprime(2011)<jupyter_output><empty_output><jupyter_text>What is the 1000th prime number?<jupyter_code>nt.prime(1000)<jupyter_output><empty_output><jupyter_text>How many primes less than 2011 are there?<jupyter_code>nt.primepi(2011)<jupyter_output><empty_output><jupyter_text>We can plot $\pi(x)$, the prime-counting function (the number of prime numbers less than or equal to some number x). The famous *prime number theorem* states that this function is asymptotically equivalent to $x/\log(x)$. This expression approximately quantifies the distribution of the prime numbers among all integers.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.arange(2, 10000)
plt.plot(x, list(map(nt.primepi, x)), '-k', label='$\pi(x)$');
plt.plot(x, x / np.log(x), '--k', label='$x/\log(x)$');
plt.legend(loc=2);<jupyter_output><empty_output><jupyter_text>Let's compute the integer factorization of some number.<jupyter_code>nt.factorint(1998)
2 * 3**3 * 37<jupyter_output><empty_output><jupyter_text>Chinese Remainder Theorem A lazy mathematician is counting his marbles. When they are arranged in three rows, the last column contains one marble. When they form four rows, there are two marbles in the last column, and there are three with five rows. The Chinese Remainer Theorem can give him the answer directly.<jupyter_code>from sympy.ntheory.modular import solve_congruence
solve_congruence((1, 3), (2, 4), (3, 5))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1054706752094628, 'fraction_numerical': 0.025628388368654508, 'mean_word_length': 6.898832684824903, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '550592', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 709, 'n_tokens_neox': 648, 'n_words': 205, 'path': 'notebooks/chapter15_symbolic/05_number_theory.ipynb', 'repo_name': 'khanparwaz/PythonProjects', 'size': '2029'}