📜  前N个自然数的总和与2的所有幂加两次

📅  最后修改于: 2021-05-25 04:37:54             🧑  作者: Mango

给定一个整数N ,任务是计算前N个自然数的总和,将2的所有幂加到该总和上。
例子:

天真的方法:
解决此问题的最简单方法是迭代到N ,并通过除2的幂之外的每个数加一次来继续计算总和,该2的幂需要加两次。
时间复杂度: O(N)
辅助空间: O(1)
高效方法:
请按照以下步骤优化上述方法:

  1. 通过公式(N *(N + 1))/ 2计算前N个自然数的总和。
  2. 现在,需要再次添加2的所有幂。 2到N的所有幂的总和可以计算为2 log 2 (N)+ 1-1
  3. 因此,所需的总和是:

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to raise N to the
// power P and return the value
double power(int N, int P)
{
    return pow(N, P);
}
 
// Function to calculate the
// log base 2 of an integer
int Log2(int N)
{
 
    // Calculate log2(N) indirectly
    // using log() method
    int result = (int)(log(N) / log(2));
    return result;
}
 
// Function to calculate and
// return the required sum
double specialSum(int n)
{
 
    // Sum of first N natural
    // numbers
    double sum = n * (n + 1) / 2;
 
    // Sum of all powers of 2
    // up to N
    int a = Log2(n);
    sum = sum + power(2, a + 1) - 1;
 
    return sum;
}
 
// Driver code
int main()
{
    int n = 4;
 
    cout << (specialSum(n)) << endl;
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java program to implement
// the above approach
import java.util.*;
import java.lang.Math;
 
class GFG {
 
    // Function to raise N to the
    // power P and return the value
    static double power(int N, int P)
    {
        return Math.pow(N, P);
    }
 
    // Function to calculate the
    // log base 2 of an integer
    public static int log2(int N)
    {
 
        // Calculate log2(N) indirectly
        // using log() method
        int result = (int)(Math.log(N)
                        / Math.log(2));
        return result;
    }
 
    // Function to calculate and
    // return the required sum
    static double specialSum(int n)
    {
 
        // Sum of first N natural
        // numbers
        double sum = n * (n + 1) / 2;
 
        // Sum of all powers of 2
        // up to N
        int a = log2(n);
        sum = sum + power(2, a + 1) - 1;
 
        return sum;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int n = 4;
 
        System.out.println(specialSum(n));
    }
}


Python3
# Python3 program to implement
# the above approach
import math
 
# Function to raise N to the
# power P and return the value
def power(N, P):
     
    return math.pow(N, P)
 
# Function to calculate the
# log base 2 of an integer
def Log2(N):
     
    # Calculate log2(N) indirectly
    # using log() method
    result = (math.log(N) // math.log(2))
     
    return result
 
# Function to calculate and
# return the required sum
def specialSum(n):
     
    # Sum of first N natural
    # numbers
    sum = n * (n + 1) // 2
 
    # Sum of all powers of 2
    # up to N
    a = Log2(n)
    sum = sum + power(2, a + 1) - 1
     
    return sum
     
# Driver code   
if __name__=="__main__":
     
    n = 4
    print(specialSum(n))
 
# This code is contributed by rutvik_56


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
    // Function to raise N to the
    // power P and return the value
    static double power(int N, int P)
    {
        return Math.Pow(N, P);
    }
 
    // Function to calculate the
    // log base 2 of an integer
    public static int log2(int N)
    {
 
        // Calculate log2(N) indirectly
        // using log() method
        int result = (int)(Math.Log(N) /
                        Math.Log(2));
        return result;
    }
 
    // Function to calculate and
    // return the required sum
    static double specialSum(int n)
    {
 
        // Sum of first N natural
        // numbers
        double sum = (double)(n) * (n + 1) / 2;
 
        // Sum of all powers of 2
        // up to N
        int a = log2(n);
        sum = (sum) + power(2, a + 1) - 1;
 
        return sum;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int n = 4;
 
        Console.Write(specialSum(n));
    }
}
 
// This code is contributed by Ritik Bansal


Javascript


输出:
17.0

时间复杂度: O(log 2 (N))
辅助空间: O(1)