Skip to content

Instantly share code, notes, and snippets.

@chrismilson
Created November 29, 2020 07:23
Show Gist options
  • Save chrismilson/d9f40992008510e6bcf99afa1d183ac0 to your computer and use it in GitHub Desktop.
Save chrismilson/d9f40992008510e6bcf99afa1d183ac0 to your computer and use it in GitHub Desktop.
MPMP19 - The 19 Challenge

This is the ninteenth puzzle in Matt Parker's Matt Parker's Maths Puzzles puzzle series

The 19 Challenge

If you sum of the squares of the first 19 primes you get a multiple of 19.

Find another number n that has the property that the sum of the squares of the first n primes is a multiple of n.

Solution

We can write a simple python script to calculate arbitrarily large numbers with this property (assuming there are arbitrarily many of them). We can take advantage of laziness in python to make this nice and easy.

def primes():
    captured = [2]
    candidate = 3
    yield 2

    while True:
        for p in captured:
            if p * p > candidate:
                captured.append(candidate)
                yield candidate
                break
            if candidate % p == 0:
                break
        candidate += 1

This method will return a generator over the primes. We can then enumerate this generator and keep a sum of the squares so far and check each sum for divisibility.

def specialPrimeSquares():
    sumOfSquares = 0
    
    for i, p in enumerate(primes(), 1):
        sumOfSquares += p ** 2
        if sumOfSquares % i == 0:
            yield i

This is just another generator that will iterate these special numbers!

Here are some numbers with this property from our generator:

1, 19, 37, 455, 509, 575, 20597, 202717, 1864637

I put this list into the OEIS and I found sequence A111441. Only the first 12 terms have been found so far, and the 12th term is 51283502951.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment