📜  计算不超过N的整数,该整数可以表示为两个或多个连续数字的总和

📅  最后修改于: 2021-04-17 16:20:58             🧑  作者: Mango

给定正整数N ,任务是计算直到N的整数数量,该数量可以表示为两个或多个连续数字的总和。

例子:

天真的方法:请按照以下步骤解决问题:

  1. [1,N]范围内的所有整数进行迭代并检查每个整数是否可以表示为两个或多个连续整数的和。
  2. 若要检查一个数字是否可以表示为两个或多个连续数字的总和,请参阅本文。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check if the number N
// can be expressed as sum of 2 or
// more consecutive numbers or not
bool isPossible(int N)
{
    return ((N & (N - 1)) && N);
}
 
// Function to count integrs in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
void countElements(int N)
{
    // Stores the required count
    int count = 0;
 
    for (int i = 1; i <= N; i++) {
 
        if (isPossible(i))
            count++;
    }
    cout << count;
}
// Driver Code
int main()
{
    int N = 15;
    countElements(N);
 
    return 0;
}


Java
// Java program of the above approach
import java.io.*;
class GFG
{
 
// Function to check if the number N
// can be expressed as sum of 2 or
// more consecutive numbers or not
static int isPossible(int N)
{
    return (((N & (N - 1)) & N));
}
 
// Function to count integrs in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
static void countElements(int N)
{
   
    // Stores the required count
    int count = 0;
    for (int i = 1; i <= N; i++)
    {
        if (isPossible(i) != 0)
            count++;
    }
    System.out.println(count);
}
 
// Driver Code
    public static void main(String[] args)
{
    int N = 15;
    countElements(N);
}
}
 
// This code is contributed by jana_sayantan.


Python3
# Python3 Program to implement
# the above approach
 
# Function to check if the number N
# can be expressed as sum of 2 or
# more consecutive numbers or not
def isPossible(N):
    return ((N & (N - 1)) and N)
 
# Function to count integrs in the range
# [1, N] that can be expressed as sum of
# 2 or more consecutive numbers
def countElements(N):
   
    # Stores the required count
    count = 0
    for i in range(1, N + 1):
        if (isPossible(i)):
            count += 1
 
    print (count)
     
# Driver Code
if __name__ == '__main__':
    N = 15
    countElements(N)
 
    # This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to check if the number N
// can be expressed as sum of 2 or
// more consecutive numbers or not
static int isPossible(int N)
{
    return (((N & (N - 1)) & N));
}
 
// Function to count integrs in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
static void countElements(int N)
{
   
    // Stores the required count
    int count = 0;
    for (int i = 1; i <= N; i++)
    {
        if (isPossible(i) != 0)
            count++;
    }
    Console.Write(count);
}
 
// Driver Code
static public void Main()
{
    int N = 15;
    countElements(N);
}
}
 
// This code is contributed by code_hunt.


Javascript


C++
// C++ implementation
// of the above approach
#include 
using namespace std;
 
// Function to count integrs in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
void countElements(int N)
{
    int Cur_Ele = 1;
    int Count = 0;
 
    // Count powers of 2 up to N
    while (Cur_Ele <= N) {
 
        // Increment count
        Count++;
 
        // Update current power of 2
        Cur_Ele = Cur_Ele * 2;
    }
 
    cout << N - Count;
}
 
// Driver Code
int main()
{
    int N = 15;
    countElements(N);
 
    return 0;
}


Java
// Java implementation
// of the above approach
import java.util.*;
class GFG
{
 
  // Function to count integrs in the range
  // [1, N] that can be expressed as sum of
  // 2 or more consecutive numbers
  static void countElements(int N)
  {
    int Cur_Ele = 1;
    int Count = 0;
 
    // Count powers of 2 up to N
    while (Cur_Ele <= N)
    {
 
      // Increment count
      Count++;
 
      // Update current power of 2
      Cur_Ele = Cur_Ele * 2;
    }
 
    System.out.print(N - Count);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 15;
    countElements(N);
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# python 3 implementation
# of the above approach
 
# Function to count integrs in the range
# [1, N] that can be expressed as sum of
# 2 or more consecutive numbers
def countElements(N):
    Cur_Ele = 1
    Count = 0
 
    # Count powers of 2 up to N
    while (Cur_Ele <= N):
        # Increment count
        Count += 1
 
        # Update current power of 2
        Cur_Ele = Cur_Ele * 2
 
    print(N - Count)
 
# Driver Code
if __name__ == '__main__':
    N = 15
    countElements(N)


C#
// C# implementation
// of the above approach
using System;
 
public class GFG
{
 
  // Function to count integrs in the range
  // [1, N] that can be expressed as sum of
  // 2 or more consecutive numbers
  static void countElements(int N)
  {
    int Cur_Ele = 1;
    int Count = 0;
 
    // Count powers of 2 up to N
    while (Cur_Ele <= N)
    {
 
      // Increment count
      Count++;
 
      // Update current power of 2
      Cur_Ele = Cur_Ele * 2;
    }
 
    Console.Write(N - Count);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 15;
    countElements(N);
  }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
11

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

高效方法:要优化上述方法,请按照以下步骤解决问题

  1. 除2的幂以外的所有数字都可以表示为连续数字的总和。
  2. 计数的2≤Ñ功率的数量并将其存储在一个变量中,k比如说计数
  3. 打印(N –计数)作为必需的答案。

下面是上述方法的实现:

C++

// C++ implementation
// of the above approach
#include 
using namespace std;
 
// Function to count integrs in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
void countElements(int N)
{
    int Cur_Ele = 1;
    int Count = 0;
 
    // Count powers of 2 up to N
    while (Cur_Ele <= N) {
 
        // Increment count
        Count++;
 
        // Update current power of 2
        Cur_Ele = Cur_Ele * 2;
    }
 
    cout << N - Count;
}
 
// Driver Code
int main()
{
    int N = 15;
    countElements(N);
 
    return 0;
}

Java

// Java implementation
// of the above approach
import java.util.*;
class GFG
{
 
  // Function to count integrs in the range
  // [1, N] that can be expressed as sum of
  // 2 or more consecutive numbers
  static void countElements(int N)
  {
    int Cur_Ele = 1;
    int Count = 0;
 
    // Count powers of 2 up to N
    while (Cur_Ele <= N)
    {
 
      // Increment count
      Count++;
 
      // Update current power of 2
      Cur_Ele = Cur_Ele * 2;
    }
 
    System.out.print(N - Count);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 15;
    countElements(N);
  }
}
 
// This code is contributed by shikhasingrajput

Python3

# python 3 implementation
# of the above approach
 
# Function to count integrs in the range
# [1, N] that can be expressed as sum of
# 2 or more consecutive numbers
def countElements(N):
    Cur_Ele = 1
    Count = 0
 
    # Count powers of 2 up to N
    while (Cur_Ele <= N):
        # Increment count
        Count += 1
 
        # Update current power of 2
        Cur_Ele = Cur_Ele * 2
 
    print(N - Count)
 
# Driver Code
if __name__ == '__main__':
    N = 15
    countElements(N)

C#

// C# implementation
// of the above approach
using System;
 
public class GFG
{
 
  // Function to count integrs in the range
  // [1, N] that can be expressed as sum of
  // 2 or more consecutive numbers
  static void countElements(int N)
  {
    int Cur_Ele = 1;
    int Count = 0;
 
    // Count powers of 2 up to N
    while (Cur_Ele <= N)
    {
 
      // Increment count
      Count++;
 
      // Update current power of 2
      Cur_Ele = Cur_Ele * 2;
    }
 
    Console.Write(N - Count);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 15;
    countElements(N);
  }
}
 
// This code is contributed by 29AjayKumar

Java脚本


输出:
11

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