Express a given number as a summation of 4 positive primes. If it is not possible to express then print “-1”.
Input: 24 Output: 3 11 3 7 Explanation : 3+11+3+7 = 24 and 3, 11, 7 are all prime. Input: 46 Output: 11 11 17 7 explanation : 11+11+17+7 = 46 and 11, 7, 17 are all prime.
Approach : Every even integer greater than 2 can be expressed as the sum of two numbers by Goldbach’s conjecture.
Below are some facts for expressing a number as sum of 4 primes.
- Number must be greater than or equal to 8 as 2 is the smallest prime
- If given number is even, we can break it as (2 + 2) + x so that x remains even and can broken into two primes.
- If given number is odd, we can break it as (2 + 3) + x so that x remains even and can broken into two primes.
Now we can easily express n as sum of two primes using link
2 2 5 19
Time complexity: O(n sqrt(n))
Auxiliary space: O(1)
This article is contributed by Raja Vikramaditya If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Check if a prime number can be expressed as sum of two Prime Numbers
- Numbers within a range that can be expressed as power of two numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Check if a number can be expressed as sum two abundant numbers
- Check if a number can be expressed as a sum of consecutive numbers
- Check whether a number can be expressed as a product of single digit numbers
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Print the nearest prime number formed by adding prime numbers to N
- Print prime numbers with prime sum of digits in an array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Numbers less than N which are product of exactly two distinct prime numbers
- Prime numbers after prime P with sum S
- Prime Numbers
- Almost Prime Numbers