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:
# Python3 Code to check if an integer can
# be expressed as sum of two semi-primes
MAX = 10000
arr = 
sprime = [False] * (MAX)
# Utility function to compute
# semi-primes in a range
for i in range(2, MAX):
cnt, num, j = 0, i, 2
while cnt < 2 and j * j <= num: while num % j == 0: num /= j # Increment count of prime numbers cnt += 1 j += 1 # If number is greater than 1, add it # to the count variable as it indicates # the number remain is prime number if num > 1:
cnt += 1
# if count is equal to ‘2’ then
# number is semi-prime
if cnt == 2:
sprime[i] = True
# Utility function to check
# if a number sum of two
i = 0
while arr[i] <= n // 2: # arr[i] is already a semi-prime # if n-arr[i] is also a semi-prime # then we a number can be expressed as # sum of two semi-primes if sprime[n - arr[i]] == True: return True i += 1 return False # Driver code if __name__ == "__main__": computeSemiPrime() n = 30 if checkSemiPrime(n) == True: print("YES") else: print("NO") # This code is contributed by # Rituraj Jain [tabby title="C#"]
- 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 a sum of consecutive numbers
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a number can be expressed as sum two abundant numbers
- 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 for integer overflow on multiplication
- Check if the given array contains all the divisors of some integer
- N expressed as sum of 4 prime numbers
- Number expressed as sum of five consecutive integers
- Count primes that can be expressed as sum of two consecutive primes and 1
- Blum Integer
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.