{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "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", "\n", "First we write a Python function `goldbach` that takes `N` and returns all pairs of numbers $a, b$." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sympy as sym\n", "import pandas as pd\n", "\n", "def goldbach(N):\n", " \"\"\"Returns all pairs of primes that sum to give N\"\"\"\n", " primes = list(sym.primerange(1, N))\n", " sums = []\n", " for i, p1 in enumerate(primes):\n", " for p2 in primes[i:]:\n", " if p1 + p2 == N:\n", " sums.append((p1, p2))\n", " return sums" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us use the above function to create our data:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "maxN = 500\n", "data = [[N, *pair] for N in range(4, maxN + 1) \n", " for pair in goldbach(N) if N % 2 == 0 ]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us write our data to an excel file:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(data, columns=[\"N\",\"a\", \"b\"]) # Create a data frame\n", "df.to_excel(\"data/goldbach.xlsx\") # Write it to excel" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda env:mwp]", "language": "python", "name": "conda-env-mwp-py" }, "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.7.0" } }, "nbformat": 4, "nbformat_minor": 1 }