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 = 73Input: arr[] = {1, 3, 11, 45}
Output: 4
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 |
73
Recommended Posts:
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Sum of all perfect numbers present in an array
- Sum of all palindrome numbers present in an Array
- Maximum consecutive numbers present in an array
- Mersenne Prime
- First string from the given array whose reverse is also present in the same array
- Probability of a key K present in array
- Find elements which are present in first array and not in second
- Count elements present in first array but not in second
- Sum of elements whose square root is present in the array
- Check if a key is present in every segment of size k in an array
- Sum of element whose prime factors are present in array
- Count pairs with average present in the same array
- Minimum De-arrangements present in array of AP (Arithmetic Progression)
- Find index of an extra element present in one sorted array
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.



