A1-Appendix-creating-Goldbach-data

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

First we write a Python function goldbach that takes N and returns all pairs of numbers $a, b$.

In [1]:
import sympy as sym
import pandas as pd

def goldbach(N):
    """Returns all pairs of primes that sum to give N"""
    primes = list(sym.primerange(1, N))
    sums = []
    for i, p1 in enumerate(primes):
        for p2 in primes[i:]:
            if p1 + p2 == N:
                sums.append((p1, p2))
    return sums

Let us use the above function to create our data:

In [3]:
maxN = 500
data = [[N, *pair] for N in range(4, maxN + 1) 
        for pair in goldbach(N) if N % 2 == 0 ]

Let us write our data to an excel file:

In [4]:
df = pd.DataFrame(data, columns=["N","a", "b"])  # Create a data frame
df.to_excel("data/goldbach.xlsx")  # Write it to excel