Related Articles
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers
• Last Updated : 04 May, 2021

Given 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 = 10
Output: 4
Explanation: 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 = 100
Output: 7

Naive 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++

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

Time Complexity: O(N)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized based on the observation that the integers which are not powers of 2, except for 2, can be expressed as the sum of two or more consecutive positive integers. Therefore, the count of such integers over the range [1, N] is given by (log2 N + 1).

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to count numbers that``// cannot be expressed as sum of``// two or more consecutive +ve integers``void` `countNum(``int` `N)``{``    ``// Stores the count``// of such numbers``    ``int` `ans = log2(N) + 1;` `    ``cout << ans << ``"\n"``;``}` `// Driver Code``int` `main()``{``    ``int` `N = 100;``    ``countNum(N);` `    ``return` `0;``}`

## Java

 `// java program for the above approach``import` `java.io.*;``import` `java.lang.*;``import` `java.util.*;` `public` `class` `GFG {` `    ``// Function to count numbers that``    ``// cannot be expressed as sum of``    ``// two or more consecutive +ve integers``    ``static` `void` `countNum(``int` `N)``    ``{``      ` `        ``// Stores the count``        ``// of such numbers``        ``int` `ans = (``int``)(Math.log(N) / Math.log(``2``)) + ``1``;` `        ``System.out.println(ans);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `N = ``100``;``        ``countNum(N);``    ``}``}` `// This code is contributed by Kingash.`
Output:
`7`

Time Complexity: O(log N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up