Calculate sum of all integers from 1 to N , excluding perfect power of 2 | C ++ implementation of the approach ; Function to find the required summation ; Find the sum of first N integers using the formula ; Find the sum of numbers which are exact power of 2 by using the formula ; Print the final Sum ; Driver 's Code ; Function to find the sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSum ( int N ) { int sum = ( N ) * ( N + 1 ) / 2 ; int r = log2 ( N ) + 1 ; int expSum = pow ( 2 , r ) - 1 ; cout << sum - expSum << endl ; } int main ( ) { int N = 2 ; findSum ( N ) ; return 0 ; }