Jupyter Snippet CB2nd 05_number_theory

Jupyter Snippet CB2nd 05_number_theory

15.5. A bit of number theory with SymPy

from sympy import *
import sympy.ntheory as nt
init_printing()
nt.isprime(2017)
True
nt.nextprime(2017)

png

nt.prime(1000)

png

nt.primepi(2017)

png

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)

png

nt.factorint(1998)

png

2 * 3**3 * 37

png

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

png