# 15.5. A bit of number theory with SymPy

https://ipython-books.github.io/155-a-bit-of-number-theory-with-sympy/

## Ref

* Undergraduate level: Elementary Number Theory, Gareth A. Jones, Josephine M. Jones, Springer, (1998)
* Graduate level: A Classical Introduction to Modern Number Theory, Kenneth Ireland, Michael Rosen, Springer, (1982)
* SymPy's number-theory module, available at http://docs.sympy.org/latest/modules/ntheory.html
* The Chinese Remainder Theorem on Wikipedia, at https://en.wikipedia.org/wiki/Chinese_remainder_theorem
* Applications of the Chinese Remainder Theorem, given at http://mathoverflow.net/questions/10014/applications-of-the-chinese-remainder-theorem
* Number theory lectures on Awesome Math, at https://github.com/rossant/awesome-math/#number-theory


```python
from sympy import *
import sympy.ntheory as nt
init_printing()
```


```python
nt.isprime(11), nt.isprime(121)
```




    (True, False)




```python
nt.isprime(2017)
```




    True




```python
nt.nextprime(2017)
```


```python
nt.prime(1000)
```


```python
nt.primepi(2017)
```


```python
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.arange(2, 10000)
fig, ax = plt.subplots(1, 1, figsize=(6, 4))
ax.plot(x, list(map(nt.primepi, x)), '-k',
        label='$\pi(x)$')
ax.plot(x, x / np.log(x), '--k',
        label='$x/\log(x)$')
ax.legend(loc=2)
```


```python
factors = nt.factorint(2020)
```


```python
type(factors), factors.keys(), factors.values()
```




    (dict, dict_keys([2, 5, 101]), dict_values([2, 1, 1]))




```python
n = 1
for k,v in factors.items():
    n *= k**v
print(n)
```

    2020



```python
nt.factorint(1998)
```


```python
2 * 3**3 * 37
```


```python
from sympy.ntheory.modular import solve_congruence
solve_congruence((1, 3), (2, 4), (3, 5))
```


```python

```

3fe657c46b827fda5a303790ea3b4832724d758a
ipynb
Jupyter Notebook
chapter15_symbolic/05_number_theory.ipynb
wgong/cookbook-2nd-code
8ca2e5b3c90fee6605f4155e6b9dfb783ce46807
MIT
chapter15_symbolic/05_number_theory.ipynb
wgong/cookbook-2nd-code
8ca2e5b3c90fee6605f4155e6b9dfb783ce46807
MIT
chapter15_symbolic/05_number_theory.ipynb
wgong/cookbook-2nd-code
8ca2e5b3c90fee6605f4155e6b9dfb783ce46807
MIT
Qwen/Qwen-72B
1. YES
2. YES