Given an integer D, the task is to find the sum of all the prime numbers whose maximum position of set bits (farthest set bit from the right) is less than or equal to D.
Note: 2 in binary is 10 and the maximum set bit position is 2. 7 in binary is 111, maximum set bit position is 3.
Input: D = 3
2, 3, 5 and 7 are the only primes
which satisfy the given condition.
Input: D = 8
Approach: The maximum number which satisfies the given condition is 2D – 1. So, generate all prime numbers using Sieve of Eratosthenes upto 2D – 1 then find the sum of all the prime numbers in the same range.
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.
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count all prime numbers in a given range whose sum of digits is also prime
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Prime numbers after prime P with sum S
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Sum of prime numbers without odd prime digits
- Sum of all prime divisors of all the numbers in range L-R
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Maximum set bit count from pairs of integers from 0 to N that yields a sum as N
- Sum of bit differences for numbers from 0 to N | Set 2
- Find the position of the given Prime Number
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.