ECOO '07 R3 P1 - Sum of Primes

View as PDF

Submit solution

Points: 7
Time limit: 2.5s
Memory limit: 256M

Problem type

Any natural number N > 4 can be expressed as the sum of 1, 2 or 3 odd primes. If N itself is a prime, only one number is required.

However, because there are so many solutions for large composite N, it is your task to find the solution N = p + q where p \le q are prime, and where p is as large as possible, or, if N cannot be expressed as the sum of two primes, find the solution N = p + q + r where p \le q \le r are prime and where p is as large as possible, followed by where q is as large as possible.

Examples

10 49
10 = 3 + 7 49 = 13 + 13 + 23
10 = 5 + 5 49 = 13 + 17 + 19

5 is larger than 3 and so, the required solution is 10 = 5 + 5

In this case, 17 is larger than 13, and so the required solution is 49 = 13 + 17 + 19

The input contains 5 numbers on 5 separate lines. These numbers are less than 10\,000\,000 and larger than 4. Write a program that will find the sum of primes for each of these numbers as described, and display the result as is shown in the sample below.

Sample Input

49
152029
90118
4565973
705510

Sample Output

49 = 13 + 17 + 19
152029 = 152029
90118 = 44987 + 45131
4565973 = 1521991 + 1521991 + 1521991
705510 = 352753 + 352757

Comments


  • -6
    MstrPikachu  commented on July 22, 2019, 9:56 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.


    • 2
      Riolku  commented on July 30, 2019, 10:52 a.m.

      IIRC they proved that it holds for at least all N \le 10^7


  • 0
    Heimdall  commented on Feb. 17, 2019, 10:28 a.m.

    Shouldn't the output for 49 be: 49 = 2 + 47

    If you can't express 49 as the sum of two primes, then you go on to try and find three primes. But you can find two primes. So it shouldnt be 13+17 + 19


  • -5
    JustinXu  commented on Dec. 24, 2018, 12:54 p.m. edited

    This comment is hidden due to too much negative feedback. Click here to view it.


  • 1
    Johemian_Rhapsody  commented on March 30, 2018, 11:58 a.m.

    My code seems to work offline but when I submitted it the solution checker says that it's wrong for both the second and third test cases. What is the issue with my code?


  • 1
    KevinWan  commented on April 21, 2017, 10:50 a.m.

    for the first test case isn't it supposed to be 49 = 2 + 47


    • -1
      Ken_Shi  commented on April 21, 2017, 1:59 p.m.

      let the first number as large as possible


      • -1
        Paradox  commented on May 1, 2017, 1:43 p.m. edited

        But.. the problem statement implies that you should express N as a sum of three primes only if you can't express it as a sum of two primes.
        IDK tho


        • 3
          Phoenix1369  commented on May 1, 2017, 7:35 p.m.

          Any natural number N > 4 can be expressed as the sum of 1,2 or 3 odd primes.


          • 6
            Paradox  commented on May 2, 2017, 1:49 p.m.

            lmao rip
            fair enough


            • -9
              ScriptKitty  commented on Aug. 26, 2018, 4:42 p.m.

              This comment is hidden due to too much negative feedback. Click here to view it.


  • -11
    mse387  commented on April 9, 2017, 4:42 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.


    • 10
      Xyene  commented on April 9, 2017, 6:40 p.m.

      15 isn't a prime number, and all of p, q and r must be prime.