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

📅  最后修改于: 2021-10-25 11:21:20             🧑  作者: Mango

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

天真的方法:
解决这个问题最简单的方法是迭代到N ,并通过将每个数字相加一次来计算总和,除了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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程