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

• Last Updated : 21 Nov, 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 2bool 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 integersvoid 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 Codeint main(){    int N = 100;    countNum(N);     return 0;}

## Java

 // Java program for the above approachimport java.util.*; class GFG{ // Function to check if a number can// be expressed as a power of 2static boolean isPowerof2(int n){         // f N is power of    // two    return ((n & (n - 1)) > 0 && n > 0);} // Function to count numbers that// cannot be expressed as sum of// two or more consecutive +ve integersstatic 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        boolean flag = isPowerof2(i);         // Increment the count if i        // is not power of 2        if (!flag)        {            count++;        }    }     // Print the value of count    System.out.print(count + "\n");} // Driver Codepublic static void main(String[] args){    int N = 100;         countNum(N);}} // This code is contributed by shikhasingrajput

## Python3

 # Python3 program for the above approach # Function to check if a number can# be expressed as a power of 2def isPowerof2(n):    # if N is power of    # two    return ((n & (n - 1)) and n) # Function to count numbers that# cannot be expressed as sum of# two or more consecutive +ve integersdef countNum(N):    # Stores the resultant    # count of integers    count = 0     # Iterate over the range [1, N]    for i in range(1, N + 1):               # Check if i is power of 2        flag = isPowerof2(i)         # Increment the count if i        # is not power of 2        if (not flag):            count += 1     # Print the value of count    print(count) # Driver Codeif __name__ == '__main__':     N = 100    countNum(N) # This code is contributed by mohit kumar 29.

## C#

 // C# program for the above approachusing System; class GFG{ // Function to check if a number can// be expressed as a power of 2static bool isPowerof2(int n){         // f N is power of    // two    return ((n & (n - 1)) > 0 && n > 0);} // Function to count numbers that// cannot be expressed as sum of// two or more consecutive +ve integersstatic 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    Console.Write(count + "\n");} // Driver Codepublic static void Main(String[] args){    int N = 100;         countNum(N);}} // This code is contributed by 29AjayKumar

## Javascript



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 integersvoid countNum(int N){    // Stores the count// of such numbers    int ans = log2(N) + 1;     cout << ans << "\n";} // Driver Codeint main(){    int N = 100;    countNum(N);     return 0;}

## Java

 // Java program for the above approachimport 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.

## Python3

 # Python 3 program for the above approachimport math # Function to count numbers that# cannot be expressed as sum of# two or more consecutive +ve integersdef countNum(N):     # Stores the count        # of such numbers    ans = int(math.log2(N)) + 1     print(ans) # Driver Codeif __name__ == "__main__":     N = 100    countNum(N)     # This code is contributed by ukasp.

## C#

 // C# program for the above approachusing System; class GFG{     // Function to count numbers that// cannot be expressed as sum of// two or more consecutive +ve integersstatic void countNum(int N){         // Stores the count    // of such numbers    int ans = (int)(Math.Log(N) /                    Math.Log(2)) + 1;    Console.WriteLine(ans);} // Driver Codestatic void Main(){    int N = 100;    countNum(N);}} // This code is contributed by SoumikMondal.

## Javascript



Output:

7

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

My Personal Notes arrow_drop_up