Monday, January 13, 2025
Google search engine
HomeData Modelling & AISum of all mersenne numbers present in an array

Sum of all mersenne numbers present in an array

Given an array of integers arr[], the task is to find the sum of all the Mersenne numbers from the array. A number is a Mersenne number if it is greater than 0 and is one less than some power of 2. First few Mersenne numbers are 1, 3, 7, 15, 31, 63, 127, …

Examples: 

Input: arr[] = {17, 6, 7, 63, 3} 
Output: 73 
Only 7, 63 and 3 are Mersenne numbers i.e. 7 + 63 + 3 = 73

Input: arr[] = {1, 3, 11, 45} 
Output:
 

Approach: Initialise sum = 0 and start traversing all the elements of the array, if current element is one less than some power of 2 and is greater than 0 then update sum = sum + arr[i]. Print the sum in the end.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Function that returns true
// if n is a Mersenne number
int isMersenne(int n)
{
    while (n != 0)
    {
        int r = n % 2;
        if (r == 0)
            return false;
        n /= 2;
    }
    return true;
}
 
// Function to return the sum of all the
// Mersenne numbers from the given array
int sumOfMersenne(int arr[], int n)
{
 
    // To store the required sum
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
 
        // If current element is a Mersenne number
        if (arr[i] > 0 && isMersenne(arr[i]))
        {
            sum += arr[i];
        }
    }
    return sum;
}
 
// Driver code
int main()
{
    int arr[] = { 17, 6, 7, 63, 3 };
    int n = sizeof(arr) / sizeof(int);
    cout << (sumOfMersenne(arr, n));
    return 0;
}
 
// This code is contributed by jit_t


Java




// Java implementation of the approach
class GFG {
 
    // Function that returns true
    // if n is a Mersenne number
    static boolean isMersenne(int n)
    {
        while (n != 0) {
            int r = n % 2;
            if (r == 0)
                return false;
            n /= 2;
        }
        return true;
    }
 
    // Function to return the sum of all the
    // Mersenne numbers from the given array
    static int sumOfMersenne(int[] arr, int n)
    {
 
        // To store the required sum
        int sum = 0;
        for (int i = 0; i < n; i++) {
 
            // If current element is a Mersenne number
            if (arr[i] > 0 && isMersenne(arr[i])) {
                sum += arr[i];
            }
        }
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 17, 6, 7, 63, 3 };
        int n = arr.length;
        System.out.print(sumOfMersenne(arr, n));
    }
}


Python3




# Python3 implementation of the approach
 
# Function that returns true
# if n is a Mersenne number
def isMersenne(n) :
    while (n != 0) :
        r = n % 2;
        if (r == 0) :
            return False;
        n //= 2;
         
    return True;
 
# Function to return the sum of all the
# Mersenne numbers from the given array
def sumOfMersenne(arr, n) :
    # To store the required sum
    sum = 0;
    for i in range(n) :
 
        # If current element is a Mersenne number
        if (arr[i] > 0 and isMersenne(arr[i])) :
            sum += arr[i];
     
    return sum;
 
 
# Driver code
if __name__ == "__main__" :
 
    arr = [17, 6, 7, 63, 3 ];
    n = len(arr);
    print(sumOfMersenne(arr, n));
     
# This code is contributed by AnkitRai01


C#




//C# implementation of the approach
using System;
 
class GFG
{
    // Function that returns true
    // if n is a Mersenne number
    static bool isMersenne(int n)
    {
        while (n != 0)
        {
            int r = n % 2;
            if (r == 0)
                return false;
            n /= 2;
        }
        return true;
    }
 
    // Function to return the sum of all the
    // Mersenne numbers from the given array
    static int sumOfMersenne(int[] arr, int n)
    {
 
        // To store the required sum
        int sum = 0;
        for (int i = 0; i < n; i++)
        {
 
            // If current element is a Mersenne number
            if (arr[i] > 0 && isMersenne(arr[i]))
            {
                sum += arr[i];
            }
        }
        return sum;
    }
 
    // Driver code
    static public void Main ()
    {
        int[] arr = { 17, 6, 7, 63, 3 };
        int n = arr.Length;
        Console.WriteLine(sumOfMersenne(arr, n));
    }
}
 
// This code is contributed by jit_t


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function that returns true
// if n is a Mersenne number
function isMersenne( n)
{
    while (n != 0)
    {
        let r = n % 2;
        if (r == 0)
            return false;
             
        n = Math.floor(n / 2);
    }
    return true;
}
 
// Function to return the sum of all the
// Mersenne numbers from the given array
function sumOfMersenne(arr, n)
{
     
    // To store the required sum
    let sum = 0;
    for(let i = 0; i < n; i++)
    {
         
        // If current element is a Mersenne number
        if (arr[i] > 0 && isMersenne(arr[i]))
        {
            sum += arr[i];
        }
    }
    return sum;
}
 
// Driver Code
let arr = [ 17, 6, 7, 63, 3 ];
let n = arr.length;
 
document.write(sumOfMersenne(arr, n));
 
// This code is contributed by jana_sayantan
 
</script>


Output: 

73

 

Time Complexity : O(nlogn)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments