📌  相关文章
📜  检查给定数字是否可以表示为前X个自然数之和的对和

📅  最后修改于: 2021-05-06 08:02:13             🧑  作者: Mango

给定一个整数N ,任务是检查N是否为一对整数的总和,可以将其表示为前X个自然数的总和,其中X可以是任何正整数。如果满足要求的条件。打印“是”。否则,打印“否”。

例子:

方法:想法是选择小于等于N的自然数M的总和,并检查MN – M是否为前几个自然数的序列之和。请按照以下步骤解决问题:

  • 循环遍历以计算K个自然数的总和:
  • 然后,计算剩余的总和,并通过以下公式检查总和是否为总和:
  • 通过计算该数字的两倍的平方根来检查上面计算出的数字是否满足要求的条件,并检查连续数字的乘积是否等于该数字的两倍。
  • 如果满足以上条件,则打印“是”。否则,打印“否”。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include
using namespace std;
 
// Function to check if the number
// is pair-sum of sum of first X
// natural numbers
void checkSumOfNatural(int n)
{
    int i = 1;
    bool flag = false;
     
    // Check if the given number
    // is sum of pair of special numbers
    while (i * (i + 1) < n * 2)
    {
         
        // X is the sum of first
        // i natural numbers
        int X = i * (i + 1);
         
        // t = 2 * Y
        int t = n * 2 - X;
        int k = sqrt(t);
         
        // Condition to check if
        // Y is a special number
        if (k * (k + 1) == t)
        {
            flag = true;
            break;
        }
        i += 1;
    }
     
    if (flag)
        cout << "YES";
    else
        cout << "NO";
}
 
// Driver Code
int main()
{
    int n = 25;
     
    // Function call
    checkSumOfNatural(n);
 
    return 0;
}
 
// This code is contributed by rutvik_56


Java
// Java program of the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
 
// Function to check if the number
// is pair-sum of sum of first X
// natural numbers
static void checkSumOfNatural(int n)
{
    int i = 1;
    boolean flag = false;
     
    // Check if the given number
    // is sum of pair of special numbers
    while (i * (i + 1) < n * 2)
    {
         
        // X is the sum of first
        // i natural numbers
        int X = i * (i + 1);
         
        // t = 2 * Y
        int t = n * 2 - X;
        int k = (int)Math.sqrt(t);
         
        // Condition to check if
        // Y is a special number
        if(k * (k + 1) == t)
        {
            flag = true;
            break;
        }
        i += 1;
    }
     
    if (flag)
        System.out.println("YES");
    else
        System.out.println("NO");
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 25;
     
    // Function call
    checkSumOfNatural(n);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program of the
# above approach
 
import math
 
# Function to check if the number
# is pair-sum of sum of first X
# natural numbers
def checkSumOfNatural(n):
    i = 1
    flag = False
     
    # Check if the given number
    # is sum of pair of special numbers
    while i*(i + 1) < n * 2:
         
        # X is the sum of first
        # i natural numbers
        X = i*(i + 1)
         
        # t = 2 * Y
        t = n * 2 - X
        k = int(math.sqrt(t))
         
        # Condition to check if
        # Y is a special number
        if k*(k + 1) == t:
            flag = True
            break
        i += 1
     
    if flag:
        print('YES')
    else:
        print('NO')
 
# Driver Code       
if __name__ == "__main__":
    n = 25
     
    # Function Call
    checkSumOfNatural(n)


C#
// C# program of
// the above approach
using System;
class GFG{
 
// Function to check if the number
// is pair-sum of sum of first X
// natural numbers
static void checkSumOfNatural(int n)
{
  int i = 1;
  bool flag = false;
 
  // Check if the given number
  // is sum of pair of special numbers
  while (i * (i + 1) < n * 2)
  {
    // X is the sum of first
    // i natural numbers
    int X = i * (i + 1);
 
    // t = 2 * Y
    int t = n * 2 - X;
    int k = (int)Math.Sqrt(t);
 
    // Condition to check if
    // Y is a special number
    if(k * (k + 1) == t)
    {
      flag = true;
      break;
    }
    i += 1;
  }
 
  if (flag)
    Console.WriteLine("YES");
  else
    Console.WriteLine("NO");
}
 
// Driver Code
public static void Main(String[] args)
{
  int n = 25;
 
  // Function call
  checkSumOfNatural(n);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
YES

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