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++
// C++ program to implement // the above approach #include <bits/stdc++.h> 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)); } } |
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 conributed by Ritik Bansal |
17.0
Time Complexity: O(log2(N))
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of kth powers of first n natural numbers
- Sum of fifth powers of the first n natural numbers
- Sum of first N natural numbers which are not powers of K
- Sum of fourth powers of the first n natural numbers
- Sum of fourth powers of first n odd natural numbers
- Sum of first N natural numbers by taking powers of 2 as negative number
- Find k numbers which are powers of 2 and have sum N | Set 1
- Print all integers that are sum of powers of two given numbers
- Find the sum of numbers from 1 to n excluding those which are powers of K
- Minimum numbers with one's place as 9 to be added to get N
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Sum of first n natural numbers
- Natural Numbers
- LCM of First n Natural Numbers
- Sum of cubes of first n odd natural numbers
- Sum of all odd natural numbers in range L and R
- Sum of cubes of even and odd natural numbers
- Repeated sum of first N natural numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

