{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> 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.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 15.5. A bit of number theory with SymPy" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sympy import *\n", "init_printing()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import sympy.ntheory as nt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prime numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test whether a number is prime." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nt.isprime(2011)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the next prime after a given number." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nt.nextprime(2011)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is the 1000th prime number?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nt.prime(1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How many primes less than 2011 are there?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nt.primepi(2011)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(2, 10000)\n", "plt.plot(x, list(map(nt.primepi, x)), '-k', label='$\\pi(x)$');\n", "plt.plot(x, x / np.log(x), '--k', label='$x/\\log(x)$');\n", "plt.legend(loc=2);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's compute the integer factorization of some number." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nt.factorint(1998)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "2 * 3**3 * 37" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Chinese Remainder Theorem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sympy.ntheory.modular import solve_congruence" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "solve_congruence((1, 3), (2, 4), (3, 5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are infinitely many solutions: 58, and 58 plus any multiple of 60. Since 118 seems visually too high, 58 is the right answer." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }