📌  相关文章
📜  检查N是否可以表示为两个连续整数的平方和

📅  最后修改于: 2021-04-24 21:34:25             🧑  作者: Mango

给定整数N ,任务是检查N是否可以表示为两个连续整数的平方和。

例子:

方法:该等式可以表示为:

最后,检查使用此公式计算的值是一个整数,这意味着N可以表示为2个连续整数的平方和

下面是上述方法的实现:

C++
// C++ implementation to check that
// a number is sum of squares of 2
// consecutive numbers or not
 
#include 
using namespace std;
 
// Function to check that the
// a number is sum of squares of 2
// consecutive numbers or not
bool isSumSquare(int N)
{
    float n
        = (2 + sqrt(8 * N - 4))
          / 2;
 
    // Condition to check if the
    // a number is sum of squares of 2
    // consecutive numbers or not
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    int i = 13;
 
    // Function call
    if (isSumSquare(i)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java implementation to check that
// a number is sum of squares of 2
// consecutive numbers or not
import java.lang.Math;
 
class GFG{
     
// Function to check that the
// a number is sum of squares of 2
// consecutive numbers or not
public static boolean isSumSquare(int N)
{
    double n = (2 + Math.sqrt(8 * N - 4)) / 2;
     
    // Condition to check if the
    // a number is sum of squares of 2
    // consecutive numbers or not
    return(n - (int)n) == 0;
}
 
// Driver code
public static void main(String[] args)
{
    int i = 13;
 
    // Function call
    if (isSumSquare(i))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation to check that
# a number is sum of squares of 2
# consecutive numbers or not
import math
 
# Function to check that the a
# number is sum of squares of 2
# consecutive numbers or not
def isSumSquare(N):
 
    n = (2 + math.sqrt(8 * N - 4)) / 2
     
    # Condition to check if the a
    # number is sum of squares of
    # 2 consecutive numbers or not
    return (n - int(n)) == 0
 
# Driver code
if __name__=='__main__':
     
    i = 13
     
    # Function call
    if isSumSquare(i):
        print('Yes')
    else :
        print('No')
 
# This code is contributed by rutvik_56


C#
// C# implementation to check that
// a number is sum of squares of 2
// consecutive numbers or not
using System;
class GFG{
     
// Function to check that the
// a number is sum of squares of 2
// consecutive numbers or not
public static bool isSumSquare(int N)
{
    double n = (2 + Math.Sqrt(8 * N - 4)) / 2;
     
    // Condition to check if the
    // a number is sum of squares of 2
    // consecutive numbers or not
    return(n - (int)n) == 0;
}
 
// Driver code
public static void Main(String[] args)
{
    int i = 13;
 
    // Function call
    if (isSumSquare(i))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
Yes

注意:为了打印整数,我们可以轻松地求解上述方程式以求根。

时间复杂度: O(1)

辅助空间: O(1)