Home Project Euler Teaching Projects Gallery

Problem 46: Goldbach's other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

                    9 = 7 + 2×12
                    15 = 7 + 2×22
                    21 = 3 + 2×32
                    25 = 7 + 2×32
                    27 = 19 + 2×22
                    33 = 31 + 2×12
                

It turns out that the conjecture was false. What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?


Algorthim:

This is rather simple question. For every composite, we can either loop through all possible primes upt to the odd composite number. For example, if we want to find 51, then we would loop all the primes up to 51 which is 2, 3, 5, 7 ... 47 and check for each prime if there exists a perfect square after subtracting this prime.

OR we can loop through all possible squares up to this odd composite. So for 51, it would up to 72 and check if after subtracting this square, it leaves a prime. I chose this option!

Code:

Result:

Answer: 5777
Runtime: 3.6 seconds