Dada una array de enteros arr[] , la tarea es encontrar la suma de todos los números de Mersenne de la array. Un número es un número de Mersenne si es mayor que 0 y es uno menor que alguna potencia de 2. Los primeros números de Mersenne son 1, 3, 7, 15, 31, 63, 127, …
Ejemplos:
Entrada: arr[] = {17, 6, 7, 63, 3}
Salida: 73
Solo 7, 63 y 3 son números de Mersenne, es decir, 7 + 63 + 3 = 73Entrada: arr[] = {1, 3, 11, 45}
Salida: 4
Enfoque: Inicialice sum = 0 y comience a recorrer todos los elementos de la array, si el elemento actual es uno menos que alguna potencia de 2 y es mayor que 0, entonces actualice sum = sum + arr[i] . Imprime la suma al final.
A continuación se muestra la implementación del enfoque anterior:
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>
73
Complejidad de tiempo: O (nlogn)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por facebookruppal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA