# 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 = 1^{2}+ 2^{2}where 1 and 2 are consecutive numbers.

Input:13

Output:Yes

Explanation:

13 = 2^{2}+ 3^{2}

**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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

## Recommended Posts:

- Check whether a number can be represented by sum of two squares
- Check whether a number can be represented as difference of two squares
- Check whether a number can be represented by the product of two squares
- Check if N can be represented as sum of integers chosen from set {A, B}
- Check whether a number can be represented as sum of K distinct positive integers
- Check if a number can be written as sum of three consecutive integers
- Count elements in an Array that can be represented as difference of two perfect squares
- Number of ways in which N can be represented as the sum of two positive integers
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Count of pairs of integers whose difference of squares is equal to N
- Find K consecutive integers such that their sum is N
- Number expressed as sum of five consecutive integers
- Remove two consecutive integers from 1 to N to make sum equal to S
- Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference
- Check if a number can be represented as sum of non zero powers of 2
- Check if a number can be represented as a sum of 2 triangular numbers
- Check if a given number can be represented in given a no. of digits in any base
- Check if given number can be represented as sum of two great numbers
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Check if the sum of perfect squares in an array is divisible by x

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.