📌  相关文章
📜  计数最多N个不能表示为至少两个连续正整数之和的数字

📅  最后修改于: 2021-05-25 08:56:00             🧑  作者: Mango

给定正整数N ,任务是从范围[1,N]中查找整数的数量,以使该整数不能表示为两个或多个连续的正整数之和。

例子:

天真的方法:可以基于以下观察结果来解决给定的问题:如果一个数是2的幂,那么它就不能表示为连续数的和。请按照以下步骤解决给定的问题:

  • 初始化一个变量,例如count ,该变量存储了无法表示为两个或多个连续整数之和的范围[1,N]内的数字的计数。
  • 迭代范围[1,N] ,如果数字i为2的完美幂,则将count的值增加1
  • 完成上述步骤后,打印count的值作为结果。

下面是上述方法的实现:

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;
}


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.


输出:
7

时间复杂度: O(N)
辅助空间: O(1)

高效方法:还可以基于以下观察来优化上述方法2以外,不是2的幂的整数可以表示为两个或多个连续的正整数之和。因此,此类整数在[1,N]范围内的计数由(log 2 N +1)给出。

下面是上述方法的实现:

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.
输出:
7

时间复杂度: O(log N)
辅助空间: O(1)