Calculate sum of all integers from 1 to N, excluding perfect power of 2

Given a positive integer N, the task is to calculate the sum of all integers from 1 to N but excluding the number which is a perfect power of 2.
Examples:Â
Â
Input: N = 2Â
Output: 0
Input: N = 1000000000Â
Output: 499999998352516354Â
Â
Â
Naive Approach:Â
The naive approach is to iterate every number from 1 to N and compute the sum in the variable by excluding the number which is a perfect power of 2. But to compute the sum to the number 10^9, the above approach will give Time Limit Error.
Time Complexity: O(N)
Efficient Approach:Â
To find desired sum, below are the steps:Â
Â
- Find the sum of all the number till N using the formula discussed in this article in O(1) time.
- Since sum of all perfect power of 2 forms a Geometric Progression. Hence the sum of all powers of 2 less than N is calculated by the below formula:Â
Â
The number of element with perfect power of 2 less than N is given by log2N,Â
Let r = log2NÂ
And the sum of all numbers which are perfect power of 2 is given by 2r – 1.Â
Â
- Â
- Subtract the sum of all perfect powers of 2 calculated above from the sum of first N numbers to get the result.
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the// approach#include <bits/stdc++.h>using namespace std;Â
// Function to find the required// summationvoid findSum(int N){    // Find the sum of first N    // integers using the formula    int sum = (N) * (N + 1) / 2;         int r = log2(N) + 1;         // Find the sum of numbers     // which are exact power of    // 2 by using the formula    int expSum = pow(2, r) - 1;   Â
    // Print the final Sum    cout << sum - expSum << endl;}Â
// Driver's Codeint main(){Â Â Â Â int N = 2;Â
    // Function to find the    // sum    findSum(N);    return 0;} |
Java
// Java implementation of the above approach import java.lang.Math;Â
class GFG{Â Â Â Â Â // Function to find the required // summation public static void findSum(int N) { Â
    // Find the sum of first N     // integers using the formula     int sum = (N) * (N + 1) / 2;              int r = (int)(Math.log(N) /                   Math.log(2)) + 1;              // Find the sum of numbers     // which are exact power of     // 2 by using the formula     int expSum = (int)(Math.pow(2, r)) - 1;             // Print the final Sum     System.out.println(sum - expSum); } Â
// Driver Codepublic static void main(String[] args){Â Â Â Â int N = 2; Â
    // Function to find the sum     findSum(N); }}Â
// This code is contributed by divyeshrabadiya07 |
Python3
# Python 3 implementation of the# approachfrom math import log2,powÂ
# Function to find the required# summationdef findSum(N):    # Find the sum of first N    # integers using the formula    sum = (N) * (N + 1) // 2         r = log2(N) + 1         # Find the sum of numbers     # which are exact power of    # 2 by using the formula    expSum = pow(2, r) - 1Â
    # Print the final Sum    print(int(sum - expSum))Â
# Driver's Codeif __name__ == '__main__':Â Â Â Â N = 2Â
    # Function to find the    # sum    findSum(N)     # This code is contributed by Surendra_Gangwar |
C#
// C# implementation of the above approach using System;Â
class GFG{ Â Â Â Â Â // Function to find the required // summation public static void findSum(int N) { Â
    // Find the sum of first N     // integers using the formula     int sum = (N) * (N + 1) / 2;              int r = (int)(Math.Log(N) /                   Math.Log(2)) + 1;              // Find the sum of numbers     // which are exact power of     // 2 by using the formula     int expSum = (int)(Math.Pow(2, r)) - 1;          // Print the final Sum     Console.Write(sum - expSum); } Â
// Driver Code public static void Main(string[] args) { Â Â Â Â int N = 2; Â
    // Function to find the sum     findSum(N); } } Â
// This code is contributed by rutvik_56 |
Javascript
<script>Â
// Javascript implementation of the above approach Â
// Function to find the required // summation function findSum(N) {          // Find the sum of first N     // integers using the formula     var sum = (N) * (N + 1) / 2;              var r = (Math.log(N) /              Math.log(2)) + 1;              // Find the sum of numbers     // which are exact power of     // 2 by using the formula     var expSum = (Math.pow(2, r)) - 1;             // Print the final Sum     document.write(sum - expSum); }      // Driver codevar N = 2; Â
// Function to find the sum findSum(N); Â
// This code is contributed by KirtiÂ
</script> |
0
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



