Sum of first N natural numbers with all powers of 2 added twice
Last Updated :
21 Jul, 2022
Given an integer N, the task is to calculate the sum of first N natural numbers adding all powers of 2 twice to the sum.
Examples:
Input: N = 4
Output: 17
Explanation:
Sum = 2+4+3+8 = 17
Since 1, 2 and 4 are 2 0, 2 1 and 2 2 respectively, they are added twice to the sum.
Input: N = 5
Output: 22
Explanation:
The sum is equal to 2+4+3+8+5 = 22,
because 1, 2 and 4 are 2 0, 2 1 and 2 2 respectively.
Naive Approach:
The simplest approach to solve this problem is to iterate upto N, and keep calculating the sum by adding every number once except the powers of 2, which needs to be added twice.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach:
Follow the steps below to optimize the above approach:
- Calculate sum of first N natural numbers by the formula (N * (N + 1)) / 2.
- Now, all powers of 2 needs to be added once more. Sum of all powers of 2 up to N can be calculated as 2 log2(N) + 1 – 1.
- Hence, the required sum is:
(N * (N + 1)) / 2 + 2 log2(N) + 1 – 1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double power( int N, int P)
{
return pow (N, P);
}
int Log2( int N)
{
int result = ( int )( log (N) / log (2));
return result;
}
double specialSum( int n)
{
double sum = n * (n + 1) / 2;
int a = Log2(n);
sum = sum + power(2, a + 1) - 1;
return sum;
}
int main()
{
int n = 4;
cout << (specialSum(n)) << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.Math;
class GFG {
static double power( int N, int P)
{
return Math.pow(N, P);
}
public static int log2( int N)
{
int result = ( int )(Math.log(N)
/ Math.log( 2 ));
return result;
}
static double specialSum( int n)
{
double sum = n * (n + 1 ) / 2 ;
int a = log2(n);
sum = sum + power( 2 , a + 1 ) - 1 ;
return sum;
}
public static void main(String[] args)
{
int n = 4 ;
System.out.println(specialSum(n));
}
}
|
Python3
import math
def power(N, P):
return math. pow (N, P)
def Log2(N):
result = (math.log(N) / / math.log( 2 ))
return result
def specialSum(n):
sum = n * (n + 1 ) / / 2
a = Log2(n)
sum = sum + power( 2 , a + 1 ) - 1
return sum
if __name__ = = "__main__" :
n = 4
print (specialSum(n))
|
C#
using System;
class GFG
{
static double power( int N, int P)
{
return Math.Pow(N, P);
}
public static int log2( int N)
{
int result = ( int )(Math.Log(N) /
Math.Log(2));
return result;
}
static double specialSum( int n)
{
double sum = ( double )(n) * (n + 1) / 2;
int a = log2(n);
sum = (sum) + power(2, a + 1) - 1;
return sum;
}
public static void Main( string [] args)
{
int n = 4;
Console.Write(specialSum(n));
}
}
|
Javascript
<script>
function power(N, P)
{
return Math.pow(N, P);
}
function Log2(N)
{
let result = (Math.floor(Math.log(N) / Math.log(2)));
return result;
}
function specialSum(n)
{
let sum = n * (n + 1) / 2;
let a = Log2(n);
sum = sum + power(2, a + 1) - 1;
return sum;
}
let n = 4;
document.write(specialSum(n) + "<br>" );
</script>
|
Time Complexity: O(log2(N))
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...