Wednesday, 27 Oct 2021

# Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers

Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integersGiven a positive integer N, the task is to find the count of integers from the range [1, N] such that the integer cannot be expressed as sum of two or more consecutive positive integers.Examples:Input: N = 10Output: 4Explanation: The integers that cannot be expressed as sum of two or more consecutive integers are {1, 2, 4, 8}. Therefore, the count of integers is 4.Input: N = 100Output: 7Naive Approach: The given problem can be solved based on the observation that if a number is a power of two, then it cannot be expressed as a sum of consecutive numbers. Follow the steps below to solve the given problem:Initialize a variable, say count that stores the count of numbers over the range [1, N] that cannot be expressed as a sum of two or more consecutive integers.Iterate over the range [1, N], and if the number i is a perfect power of 2, then increment the value of count by 1.After completing the above steps, print the value of count as the result.Below is the implementation of the above approach:C++  #include 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