|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "This notebook is used to generate data regarding the Goldbach conjecture: rows are of the form $N, a, b$ where $N$ is an even number, $a< b$ are prime and $N=a + b$.\n", |
| 8 | + "\n", |
| 9 | + "First we write a Python function `goldbach` that takes `N` and returns all pairs of numbers $a, b$." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "metadata": { |
| 16 | + "collapsed": false |
| 17 | + }, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "import sympy as sym\n", |
| 21 | + "import pandas as pd\n", |
| 22 | + "\n", |
| 23 | + "def goldbach(N):\n", |
| 24 | + " \"\"\"Returns all pairs of primes that sum to give N\"\"\"\n", |
| 25 | + " primes = list(sym.primerange(1, N))\n", |
| 26 | + " sums = []\n", |
| 27 | + " for i, p1 in enumerate(primes):\n", |
| 28 | + " for p2 in primes[i:]:\n", |
| 29 | + " if p1 + p2 == N:\n", |
| 30 | + " sums.append((p1, p2))\n", |
| 31 | + " return sums" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "markdown", |
| 36 | + "metadata": {}, |
| 37 | + "source": [ |
| 38 | + "Let us use the above function to create our data:" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": 3, |
| 44 | + "metadata": { |
| 45 | + "collapsed": false |
| 46 | + }, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "maxN = 500\n", |
| 50 | + "data = [[N, *pair] for N in range(4, maxN + 1) \n", |
| 51 | + " for pair in goldbach(N) if N % 2 == 0 ]" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "Let us write our data to an excel file:" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": 4, |
| 64 | + "metadata": { |
| 65 | + "collapsed": false |
| 66 | + }, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "df = pd.DataFrame(data, columns=[\"N\",\"a\", \"b\"]) # Create a data frame\n", |
| 70 | + "df.to_excel(\"data/goldbach.xlsx\") # Write it to excel" |
| 71 | + ] |
| 72 | + } |
| 73 | + ], |
| 74 | + "metadata": { |
| 75 | + "anaconda-cloud": {}, |
| 76 | + "kernelspec": { |
| 77 | + "display_name": "Python [default]", |
| 78 | + "language": "python", |
| 79 | + "name": "python3" |
| 80 | + }, |
| 81 | + "language_info": { |
| 82 | + "codemirror_mode": { |
| 83 | + "name": "ipython", |
| 84 | + "version": 3 |
| 85 | + }, |
| 86 | + "file_extension": ".py", |
| 87 | + "mimetype": "text/x-python", |
| 88 | + "name": "python", |
| 89 | + "nbconvert_exporter": "python", |
| 90 | + "pygments_lexer": "ipython3", |
| 91 | + "version": "3.5.2" |
| 92 | + } |
| 93 | + }, |
| 94 | + "nbformat": 4, |
| 95 | + "nbformat_minor": 1 |
| 96 | +} |
0 commit comments