Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | C ++ program for the above approach ; Function to check if a number can be expressed as a power of 2 ; f N is power of two ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the resultant count of integers ; Iterate over the range [ 1 , N ] ; Check if i is power of 2 ; Increment the count if i is not power of 2 ; Print the value of count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerof2 ( unsigned int n ) { return ( ( n & ( n - 1 ) ) && n ) ; } void countNum ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { bool flag = isPowerof2 ( i ) ; if ( ! flag ) { count ++ ; } } cout << count << " STRNEWLINE " ; } int main ( ) { int N = 100 ; countNum ( N ) ; return 0 ; }