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

**log**,_{2}N

Let**r**= log_{2}N

And the sum of all numbers which are perfect power of 2 is given by**2**.^{r}– 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 ` `// summation ` `void` `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 Code ` `int` `main() ` `{ ` ` ` `int` `N = 2; ` ` ` ` ` `// Function to find the ` ` ` `// sum ` ` ` `findSum(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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 Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `2` `; ` ` ` ` ` `// Function to find the sum ` ` ` `findSum(N); ` `} ` `} ` ` ` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the ` `# approach ` `from` `math ` `import` `log2,` `pow` ` ` `# Function to find the required ` `# summation ` `def` `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 Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `2` ` ` ` ` `# Function to find the ` ` ` `# sum ` ` ` `findSum(N) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

0

**Time Complexity:** 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:

- Find the sum of numbers from 1 to n excluding those which are powers of K
- Check if a number is a perfect square having all its digits as a perfect square
- Calculate Root Mean Kth power of all array elements
- Find the sum of power of bit count raised to the power B
- Count numbers upto N which are both perfect square and perfect cube
- Find smallest perfect square number A such that N + A is also a perfect square number
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Perfect power (1, 4, 8, 9, 16, 25, 27, ...)
- Smallest N digit number which is a perfect fourth power
- Count perfect power of K in a range [L, R]
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Java Program to Calculate Power of a Number
- Find power of power under mod of a prime
- Check if given number is a power of d where d is a power of 2
- Compute power of power k times % m
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Pair of integers having difference of their fifth power as X
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)

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.