Tim Varley Logo
Tim Varley Systems Engineer
Problem #46 hard

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 × 1²
  • 15 = 7 + 2 × 2²
  • 21 = 3 + 2 × 3²
  • 25 = 7 + 2 × 3²
  • 27 = 19 + 2 × 2²
  • 33 = 31 + 2 × 1² 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?

Implementations

cpp
// https://projecteuler.net/problem=46
// 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×1²
// 15 = 7 + 2×2²
// 21 = 3 + 2×3²
// 25 = 7 + 2×3²
// 27 = 19 + 2×2²
// 33 = 31 + 2×1²
// 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?
// Answer: 5777
// Authored by: Tim Varley 💘
#include <iostream>
#include "sieve_eratos.h"
int goldbach_other() {
const int LIMIT = 10000;
CSieveOfEratosthenes primes(LIMIT);
for (int n = 9; ; n += 2) {
if (!primes.is_prime(n)) { // odd composite
bool found = false;
for (int p = 2; p < n; ++p) {
if (primes.is_prime(p)) {
int sq = (n - p) / 2;
int k = 1;
while (k * k < sq) ++k;
if (k * k == sq) {
found = true;
break;
}
}
}
if (!found) return n;
}
}
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[]) {
std::cout << goldbach_other() << std::endl;
}
#endif // UNITTEST_MODE

Solution Notes

Mathematical Background

Goldbach conjecture states that every even integer greater than 2 can be written as the sum of two primes. This problem deals with “Goldbach’s other conjecture”: every odd composite number can be written as the sum of a prime and twice a square.

Examples given:

  • 9 = 7 + 2×1²
  • 15 = 7 + 2×2²
  • 21 = 3 + 2×3²
  • 25 = 7 + 2×3²
  • 27 = 19 + 2×2²
  • 33 = 31 + 2×1²

The conjecture is false; the smallest counterexample is 5777.

Algorithm Analysis

The solution iterates through odd numbers starting from 9, checking if each odd composite can be expressed as p + 2×k² where p is prime and k is integer.

For each odd composite n, it tries all primes p < n, computes the remainder r = n - p, and checks if r/2 is a perfect square.

Performance Analysis

  • Time Complexity: O(n²) in the worst case, but finds the answer quickly
  • Space Complexity: O(1) or O(n) depending on prime storage
  • Execution Time: Fast for small n (<1 second)
  • Scalability: Quadratic, but practical for this problem size

Key Insights

  • The counterexample 5777 = 5777, cannot be written in the required form

  • All smaller odd composites satisfy the conjecture

  • The problem requires checking up to moderately large primes

  • Efficient primality testing is crucial

Educational Value

This problem teaches:

  • Number theory and conjectures

  • Prime number properties

  • Perfect square checking algorithms

  • Counterexamples in mathematics

  • The relationship between primes and quadratic forms