# Check if N can be represented as sum of squares of two consecutive integers

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

Output:

Yes


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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.