Sum of all the prime numbers with the maximum position of set bit ≤ D

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.

Examples:

Input: D = 3
Output: 17
2, 3, 5 and 7 are the only primes
which satisfy the given condition.

Input: D = 8
Output: 6081

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function for Sieve of Eratosthenes
void sieve(bool prime[], int n)
{
    prime[0] = false;
    prime[1] = false;
    for (int p = 2; p * p <= n; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to return the sum of
// the required prime numbers
int sumPrime(int d)
{
  
    // Maximum number of the required range
    int maxVal = pow(2, d) - 1;
  
    // Sieve of Eratosthenes
    bool prime[maxVal + 1];
    memset(prime, true, sizeof(prime));
    sieve(prime, maxVal);
  
    // To store the required sum
    int sum = 0;
  
    for (int i = 2; i <= maxVal; i++) {
  
        // If current element is prime
        if (prime[i]) {
            sum += i;
        }
    }
  
    return sum;
}
  
// Driver code
int main()
{
    int d = 8;
  
    cout << sumPrime(d);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function for Sieve of Eratosthenes
static void sieve(boolean prime[], int n)
{
    prime[0] = false;
    prime[1] = false;
    for (int p = 2; p * p <= n; p++)
    {
        if (prime[p] == true)
        {
            for (int i = p * p;
                     i <= n; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to return the sum of
// the required prime numbers
static int sumPrime(int d)
{
  
    // Maximum number of the required range
    int maxVal = (int) (Math.pow(2, d) - 1);
  
    // Sieve of Eratosthenes
    boolean []prime = new boolean[maxVal + 1];
    Arrays.fill(prime, true);
    sieve(prime, maxVal);
  
    // To store the required sum
    int sum = 0;
  
    for (int i = 2; i <= maxVal; i++)
    {
  
        // If current element is prime
        if (prime[i]) 
        {
            sum += i;
        }
    }
    return sum;
}
  
// Driver code
public static void main(String[] args) 
{
    int d = 8;
  
    System.out.println(sumPrime(d));
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the approach
from math import sqrt, pow
  
# Function for Sieve of Eratosthenes
def sieve(prime, n):
    prime[0] = False
    prime[1] = False
    for p in range(2, int(sqrt(n)) + 1, 1):
        if (prime[p] == True):
            for i in range(p * p, n + 1, p):
                prime[i] = False
  
# Function to return the sum of
# the required prime numbers
def sumPrime(d):
      
    # Maximum number of the required range
    maxVal = int(pow(2, d)) - 1;
  
    # Sieve of Eratosthenes
    prime = [True for i in range(maxVal + 1)]
      
    sieve(prime, maxVal)
  
    # To store the required sum
    sum = 0
  
    for i in range(2, maxVal + 1, 1):
          
        # If current element is prime
        if (prime[i]):
            sum += i
  
    return sum
  
# Driver code
if __name__ == '__main__':
    d = 8
  
    print(sumPrime(d))
  
# This code is contributed by Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
using System.Linq;
  
class GFG 
{
  
// Function for Sieve of Eratosthenes
static void sieve(Boolean []prime, int n)
{
    prime[0] = false;
    prime[1] = false;
    for (int p = 2; p * p <= n; p++)
    {
        if (prime[p] == true)
        {
            for (int i = p * p;
                    i <= n; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to return the sum of
// the required prime numbers
static int sumPrime(int d)
{
  
    // Maximum number of the required range
    int maxVal = (int) (Math.Pow(2, d) - 1);
  
    // Sieve of Eratosthenes
    Boolean []prime = new Boolean[maxVal + 1];
      
    for (int i = 0; i <= maxVal; i++)
        prime.SetValue(true,i);
    sieve(prime, maxVal);
  
    // To store the required sum
    int sum = 0;
  
    for (int i = 2; i <= maxVal; i++)
    {
  
        // If current element is prime
        if (prime[i]) 
        {
            sum += i;
        }
    }
    return sum;
}
  
// Driver code
public static void Main(String[] args) 
{
    int d = 8;
  
    Console.WriteLine(sumPrime(d));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

6081

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.