Related Articles
Check if N can be represented as sum of squares of two consecutive integers
• Last Updated : 08 Apr, 2021

Given an integer N, the task is to check whether N can be represented as a sum of squares of two consecutive integers or not.

Examples:

Input: N = 5
Output: Yes
Explanation:
The integer 5 = 12 + 22 where 1 and 2 are consecutive numbers.

Input: 13
Output: Yes
Explanation:
13 = 22 + 32

Approach: This equation can be represented as:

=> => => Finally, check the value of computed using this formula is an integer, which means that N can be represented as the sum of squares 2 consecutive integers

Below is the implementation of the above approach:

## 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 notbool 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 Codeint 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 notimport java.lang.Math; class GFG{     // Function to check that the// a number is sum of squares of 2// consecutive numbers or notpublic 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 codepublic 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 notimport math # Function to check that the a# number is sum of squares of 2# consecutive numbers or notdef 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 codeif __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 notusing System;class GFG{     // Function to check that the// a number is sum of squares of 2// consecutive numbers or notpublic 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 codepublic 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

 
Output:
Yes

Note: In order to print the integers, we can easily solve the above equation to get the roots.

Time Complexity: O(1)

Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up