In [ ]:

```
from sympy import *
init_printing()
```

In [ ]:

```
import sympy.ntheory as nt
```

Test whether a number is prime.

In [ ]:

```
nt.isprime(2011)
```

Find the next prime after a given number.

In [ ]:

```
nt.nextprime(2011)
```

What is the 1000th prime number?

In [ ]:

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

How many primes less than 2011 are there?

In [ ]:

```
nt.primepi(2011)
```

*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.

In [ ]:

```
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
```

In [ ]:

```
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);
```

Let's compute the integer factorization of some number.

In [ ]:

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

In [ ]:

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

In [ ]:

```
from sympy.ntheory.modular import solve_congruence
```

In [ ]:

```
solve_congruence((1, 3), (2, 4), (3, 5))
```

You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).

IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).