MohamedHassan499's blog

By MohamedHassan499, history, 4 years ago, In English

You're given a number $$$n$$$ It may be prime or not, It's required from you to get 4 positive prime numbers at which their summition equals to $$$n$$$, and if this isn't possible, then state that (There's more that one test cast BTW).

My approach was to get all prime numbers form 2 to 10000000 using sieve theory only once, and to try assume 3 of these numbers and get every possible combination of 2's and 3's (Assuming that all prime numbers can be deduce from summition of these two) let these numbers are $$$x$$$, $$$y$$$ and $$$z$$$ and if there exist a fourth number at which this 4th number = n - x + y + z and If it's positive and prime, then we found the answer otherwise this won't be possible.

If there's something wrong with my approach? If not there's the mistake in my code Link to the problem is here

| Write comment?
»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Hi! I can help you. Sum of two even numbers is even (one number we can write like 2 * k1 and seconde one as 2 * k2, so and sum is even). Sum of two odd numbers is also even (one number we can write like 2 * k1 + 1 and seconde one as 2 * k2 + 1, so sum is 2 * (k1 + k2) + 2, which is equal to 2 * (k1 + k2 + 1). Sum of even and odd is odd. Keep in mind that 2 is only even prime number. N can be odd, if N — 2 * 3 is prime. (Use equations from before.). If is N even, we can divide it into two equal parts, for which we can find solution. I hope I have helped you. Edit: My bad.

»
4 years ago, # |
  Vote: I like it +12 Vote: I do not like it
Step 1:
Step 2:
Step 3:
Step 4:
All the steps in code
  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Nice solution.

    P.S: memset can't be used to fill an array of integers with values other than 0, -1.You can use fill.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    I don't know, my idea is similar to you as I mentioned above, anyway thank you, I really appreciate your help =D