Sunday 25 January 2015

Goldbach's Other Conjecture

QUESTION : 
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?
Reference : Project Euler

SOLUTION :
All we could come up with was a brute force logic which stores an ArrayList of prime numbers and caculates all the composite numbers based on this list (since every non-prime number is composite, ignoring all numbers less than or equal to 1). We thus have the LHS and the first part of the RHS. We subtract them and divide the result by 2. Finally, we check if the result is now a perfect square using Math.sqrt(double). Here is a well-commented Java code for the same.
PS: The required answer is 5777!