Search code examples
graphsagegoldbach-conjecture

Goldbach graph using sagemath


I'm learning SageMath (uses Python 3) and playing with the Goldbach conjecture.

I wrote this function (it works!):

def Goldbach(n):
    if n % 2 != 0 or n <= 2:
        show("No és parell")
    else:
        for i in srange(n):
            if is_prime(i):
                for j in srange(n):
                    if is_prime(j) and i + j == n:                        
                        a = [i, j]
                        show(a)
    return

Now I'm trying (no idea) to do the following plot:

Denoting by r(2k) the number of Goldbach partitions of 2k, the conjecture affirms that r(2k) > 0 if k > 1.

I have to do a graph of points (k, r(2k)), k > 2. How could I do it?


Solution

  • First of all, let us get some better implementation in Sage of the routine counting the number r(K) (for K > 2 some even integer) of the solutions for p + q = 2k, p, q prime numbers.

    We count both solutions (p, q) and (q, p) when they differ.

    def r(K):
        if K not in ZZ or K <= 2 or K % 2:
            return None
        if K == 4:
            return 1
        count = 0
        for p in primes(3, K):
            for q in primes(3, K + 1 - p):
                if p + q == K:
                    count += 1
        return count
    
    goldbach_points = [(K, r(K)) for K in range(4, 100,2)]
    show(points(goldbach_points))
    

    This gives:

    Plot of the Goldbach points.