给定一个整数N,任务是找到2 0 + 2 1 + 2 2 + 2 3 +…的和。 + 2 n 。
例子:
Input: 5
Output: 31
20 + 21 + 22 + 23 + 24
= 1 + 2+ 4 + 8 + 16
= 31
Input: 10
Output: 1023
20 + 21 + 2 2 + 23 + 2 4 + 25 + 26 + 27 + 2 8 + 29
= 1 + 2+ 4 + 8 + 16 + 32 +64 + 128 + 256 + 512
= 1023
一个幼稚的方法是计算总和是将2的每个幂乘以0到n。
下面是上述方法的实现:
C++
// C++ program to find sum
#include
using namespace std;
// function to calculate sum of series
int calculateSum(int n)
{
// initialize sum as 0
int sum = 0;
// loop to calculate sum of series
for (int i = 0; i < n; i++) {
// calculate 2^i
// and add it to sum
sum = sum + (1 << i);
}
return sum;
}
// Driver code
int main()
{
int n = 10;
cout << "Sum of series of power of 2 is : "
<< calculateSum(n);
}
Java
// Java program to find sum
class GFG {
// function to calculate sum of series
static int claculate sum(int n)
{
// initialize sum as 0
int sum = 0;
// loop to calculate sum of series
for (int i = 0; i < n; i++) {
// calculate 2^i
// and add it to sum
sum = sum + (1 << i);
}
return sum;
}
// Main function
public static void main(String[] args)
{
int n = 10;
System.out.println("Sum of the series : " + calculateSum(n));
}
};
Python3
# Python3 program to calculate
# sum of series of power of 2
# function to calculate sum of series
def calculateSum(n):
sum = 0
# loop to calculate sum of series
for i in range (0, n):
# calculate 2 ^ i
sum = sum+ (1 << i)
return sum
# Driver code
n = 10
print("Sum of series ", calculateSum(n))
C#
// C# program to find sum
using System;
class GFG
{
// function to calculate
// sum of series
static int calculateSum(int n)
{
// initialize sum as 0
int sum = 0;
// loop to calculate
// sum of series
for (int i = 0; i < n; i++)
{
// calculate 2^i
// and add it to sum
sum = sum + (1 << i);
}
return sum;
}
// Driver code
public static void Main()
{
int n = 10;
Console.WriteLine("Sum of the series : " +
calculateSum(n));
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
PHP
Javascript
C++
// C++ program to find sum
#include
using namespace std;
int calculateSum(int n)
{
// calculate and return 2^(n+1) -1
return (1 << (n + 1)) - 1;
}
int main()
{
int n = 10;
cout << "Sum of series of power of 2 is :"
<< calculateSum(n);
}
Java
// Java program to calculate
// sum of series of power of 2
class GFG {
// function to calculate sum of series
static int claculate sum(int n)
{
// calculate 2^(n+1)
int sum = (1 << (n + 1));
return sum - 1;
}
// Driver code
public static void main(String[] args)
{
int n = 10;
System.out.println("Sum of the series of power 2 is : "
+ calculateSum(n));
}
};
Python3
# Python3 program to calculate
# sum of series of 2's power
# function to calculate sum of series
def calculateSum(n):
# calculate 2^(n + 1)
sum = (1 << (n + 1))
return sum-1
# Driver code
n = 10
print("Sum of series ", calculateSum(n))
C#
// C# program to calculate
// sum of series of power of 2
using System;
class GFG
{
// function to calculate
// sum of series
static int calculateSum(int n)
{
// calculate 2^(n+1)
int sum = (1 << (n + 1));
return sum - 1;
}
// Driver code
public static void Main()
{
int n = 10;
Console.Write("Sum of the series " +
"of power 2 is : " +
calculateSum(n));
}
// This code is contributed
// by Smitha
}
PHP
Javascript
输出:
Sum of series of power of 2 is : 1023
时间复杂度: O(n)
一种有效的方法是找到2 ^(n + 1)并从中减去1,因为我们知道2 ^ n可以写成:
2n = ( 20+21+22+23+24 +...... 2n-1) +1
下面是上述方法的实现:
C++
// C++ program to find sum
#include
using namespace std;
int calculateSum(int n)
{
// calculate and return 2^(n+1) -1
return (1 << (n + 1)) - 1;
}
int main()
{
int n = 10;
cout << "Sum of series of power of 2 is :"
<< calculateSum(n);
}
Java
// Java program to calculate
// sum of series of power of 2
class GFG {
// function to calculate sum of series
static int claculate sum(int n)
{
// calculate 2^(n+1)
int sum = (1 << (n + 1));
return sum - 1;
}
// Driver code
public static void main(String[] args)
{
int n = 10;
System.out.println("Sum of the series of power 2 is : "
+ calculateSum(n));
}
};
Python3
# Python3 program to calculate
# sum of series of 2's power
# function to calculate sum of series
def calculateSum(n):
# calculate 2^(n + 1)
sum = (1 << (n + 1))
return sum-1
# Driver code
n = 10
print("Sum of series ", calculateSum(n))
C#
// C# program to calculate
// sum of series of power of 2
using System;
class GFG
{
// function to calculate
// sum of series
static int calculateSum(int n)
{
// calculate 2^(n+1)
int sum = (1 << (n + 1));
return sum - 1;
}
// Driver code
public static void Main()
{
int n = 10;
Console.Write("Sum of the series " +
"of power 2 is : " +
calculateSum(n));
}
// This code is contributed
// by Smitha
}
的PHP
Java脚本
输出:
Sum of series of power of 2 is :2047
时间复杂度: O(1)