Given an integer N and the task is to find a sequence of N prime numbers whose sum is a composite number.
Input: N = 5
Output: 2 3 5 7 11
2 + 3 + 5 + 7 + 11 = 28 which is composite.
Input: N = 6
Output: 3 5 7 11 13 17
Approach: The sum of two prime numbers is always even which is composite as they are odd numbers except 2. There are two cases now,
- When N is even then we can print any N prime numbers except 2 and their sum will always be even i.e. odd numbers when added even number of times will give an even sum.
- When N is odd then we can print 2 and any other N – 1 primes to make sure that the sum is even. Since, N – 1 is even so the sum will be even for any primes except 2 then we add 2 as the Nth number to make sure that the sum remains even.
Below is the implementation of the above approach:
3 5 7 11 13 17
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- XOR of K largest prime and composite numbers from the given array
- Queries for the difference between the count of composite and prime numbers in a given range
- Find all the prime numbers of given number of digits
- Find two Composite Numbers such that there difference is N
- Find a range of composite numbers of given length
- Represent the given number as the sum of two composite numbers
- Find the total number of composite factor for a given number
- Find the maximum number of composite summands of a number
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Find the largest composite number that divides N but is strictly lesser than N
- Find coordinates of a prime number in a Prime Spiral
- Find the K-th Permutation Sequence of first N natural numbers
- Find three prime numbers with given sum
- Find two prime numbers with given sum
- Find the XOR of first N Prime Numbers
- Program to find sum of prime numbers between 1 to n
- Find the Product of first N Prime Numbers
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.