Given a positive integer N, check if it can be expressed as a sum of two semi-primes or not.
Semi-primes A number is said to be a semi-prime if it can be expressed as product of two primes number ( not necessarily distinct ).
Semi-primes in the range 1 -100 are:
4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95.
Input : N = 30 Output: YES Explanation: 30 can be expressed as '15 + 15' 15 is semi-primes as 15 is a product of two primes 3 and 5. Input : N = 45 Output : YES Explanation: 45 can be expressed as '35 + 10' 35 and 10 are also semi-primes as it can a be expressed as product of two primes: 35 = 5 * 7 10 = 2 * 5
A Simple Solution is to traverse form i=1 to and check if i and N-i are semi-primes or not. If yes, print i and n-i.
An Efficient Solution is to pre-compute semi-primes in an array up to the given range and then traverse the semi-prime array and check if n-arr[i] is semi-prime or not. As, arr[i] is already a semi-prime if n-arr[i] is also a semi-prime, then n can be expressed as sum of two semi-primes.
Below is the implementation of above approach:
- Elements of Array which can be expressed as power of some integer to given exponent K
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Check if a number can be expressed as a^b | Set 2
- Check if a number can be expressed as 2^x + 2^y
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check if a number can be expressed as sum two abundant numbers
- Check if N can be expressed as product of 3 distinct numbers
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a number can be expressed as a sum of consecutive numbers
- Check if a number can be expressed as a product of exactly K prime divisors
- Square free semiprimes in a given range using C++ STL
- Check whether a number can be expressed as a product of single digit numbers
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if two Integer are anagrams of each other
- Check if the given array contains all the divisors of some integer
- Check for integer overflow on multiplication
- Minimum decrements to make integer A divisible by integer B
- N expressed as sum of 4 prime numbers
- Number expressed as sum of five consecutive integers
- Elements of Array which can be expressed as power of 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 firstname.lastname@example.org. 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.