给定一个整数数组arr [] ,任务是从该数组中查找所有梅森数字的总和。如果数字大于0且小于2的幂,则为Mersenne数。前几个Mersenne数为1、3、7、15、31、63、127,…
例子:
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: 4
方法:初始化sum = 0并开始遍历数组的所有元素,如果当前元素小于2的幂并大于0,则更新sum = sum + arr [i] 。最后打印总和。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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