Given an integer N, the task is to find the sum of semi-prime numbers which are less than or equal to N. A Semi prime number is a number which is a multiple of two prime numbers.
Input: N = 6
4 and 6 are the semi primes ≤ 6
4 + 6 = 10
Input: N = 10000000
- First Calculate the primes less than or equal to N using Sieve and store them in a vector in sorted order.
- Iterate over the vector of primes. Fix one of the primes and starting checking the value of the product of all primes with this fixed prime.
- As the primes are arranged in sorted order, once we find a prime for which the product exceeds N, then it would exceed for all remaining primes. Hence, break the nested loop here.
- Add the product value to the answer variable for all valid pairs.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check whether a number is semiprime or not
- Highest and Smallest power of K less than and greater than equal to N respectively
- Largest number less than N with digit sum greater than the digit sum of N
- Largest subset having with sum less than equal to sum of respective indices
- Find all factorial numbers less than or equal to n
- Print all prime numbers less than or equal to N
- Print all Semi-Prime Numbers less than or equal to N
- Find maximum product of digits among numbers less than or equal to N
- Print all Strong numbers less than or equal to N
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Sum of largest prime factor of each number less than equal to n
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Numbers less than N which are product of exactly two distinct prime numbers
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Highest power of 2 less than or equal to given number
- Highest power of 2 less than or equal to given Integer
- Find unique pairs such that each element is less than or equal to N