Check the integers additive decomposition conjecture - MATLAB Cody - MATLAB Central

Problem 60949. Check the integers additive decomposition conjecture

Difficulty:Rate
Problem statement
A conjecture (I rediscovered ?) related to Goldbach's one states that every integer above 2 can be written as the sum of at maximum two prime numbers and the number 1. The goal of this problem is to check this decomposition. Given a positive integer n as an input, your algorithm will return a vector of two primes, [p1, p2], plus potentially the number 1, [1, p1, p2], such that either n = p1 + p2 (case where n is an even number) or n = 1 + p1 + p2 (case where n is an odd number). This p vector will be sorted in ascending order : 1 < p1 < p2 < n. For n = 1 or n = 2 your algorithm should simply return n.
Examples (check the tests below for more)
  • n = 3 => p = [1, 2] ;
  • n = 7 => p = [2, 5] ;
  • n = 17 => p = [1, 3, 13] ;
  • n = 20 => p = [1; 19] ; % p1 may not be prime in this case
  • n = 23 => p = [1, 3, 19] ;
  • n = 60 => p = [1, 59] ; % p1 may not be prime in this case
  • n = 1 => p = 1 ;
  • n = 2 => p = 2;
Tips
Even if maybe not unique, there is always a solution. If you find a case withouit, at least you will have proven the conjecture to be false ! A simple way to start is to begin with seeking p2, the greater prime before n (even when n is prime itself. Then if the difference between n and this number is a prime number, you just have found p1. Else, add 1 and it should complete the sum.
Forbidden functions
  • regexp
  • str2num
  • assignin
  • echo
See also

Solution Stats

56.0% Correct | 44.0% Incorrect
Last Solution submitted on Aug 11, 2025

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers11

Suggested Problems

More from this Author42

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!