Saturday, September 21, 2024
Google search engine
HomeData Modelling & AICheck if N can be represented as sum of squares of two...

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 

Approach: This equation can be represented as: 

=> N = K^{2} + (K - 1)^{2}
=> N = 2*K^{2} - 2*K + 1
=> K = \frac{2 + \sqrt{8*N - 4}}{2}

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


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




<script>
 
// Javascript implementation to check that
// a number is sum of squares of 2
// consecutive numbers or not
 
// Function to check that the
// a number is sum of squares of 2
// consecutive numbers or not
function isSumSquare(N)
{
    var 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 - parseInt( n)) == 0;
}
 
// Driver code
var i = 13;
 
// Function call
if (isSumSquare(i))
{
    document.write("Yes");
}
else
{
    document.write("No");
}
 
// This code is contributed by todaysgaurav
 
</script>


Output

Yes



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

Time Complexity: O(logN) because it is using inbuilt sqrt function
Auxiliary Space: O(1)

Approach 2: Iterative Method:

Here’s another approach to check whether a number is the sum of squares of two consecutive numbers or not:

  • Iterate through all possible pairs of consecutive integers (x, x+1) such that x is less than the square root of N.
  • For each pair (x, x+1), compute their sum of squares as x^2 + (x+1)^2.
  • If the sum of squares is equal to N, return true. Otherwise, continue iterating through the pairs of consecutive integers.
  • If no pair of consecutive integers satisfies the condition, return false.

Here’s the implementation of this approach in C++:

C++




#include <bits/stdc++.h>
using namespace std;
 
bool isSumSquare(int N) {
    for (int x = 0; x * x < N; x++) {
        int sum = x * x + (x + 1) * (x + 1);
        if (sum == N) {
            return true;
        }
    }
    return false;
}
 
int main() {
    int N = 13;
    if (isSumSquare(N)) {
        cout << "Yes";
    } else {
        cout << "No";
    }
    return 0;
}


Java




public class Main {
    // Function to check if a number can be expressed as the
    //sum of squares of two consecutive numbers
    static boolean isSumSquare(int N) {
        for (int x = 0; x * x < N; x++) {
            int sum = x * x + (x + 1) * (x + 1);
            if (sum == N) {
                return true;
            }
        }
        return false;
    }
//   Driver code
    public static void main(String[] args) {
        int N = 13;
        if (isSumSquare(N)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}


Python3




def isSumSquare(N):
    for x in range(0, N):
        sum = x * x + (x + 1) * (x + 1)
        if sum == N:
            return True
        elif sum > N:
            return False
    return False
 
N = 13
if isSumSquare(N):
    print("Yes")
else:
    print("No")


C#




using System;
 
public class Program {
  public static bool IsSumSquare(int N)
  {
    for (int x = 0; x * x < N; x++) {
      int sum = x * x + (x + 1) * (x + 1);
      if (sum == N) {
        return true;
      }
    }
    return false;
  }
 
  public static void Main()
  {
    int N = 13;
    if (IsSumSquare(N)) {
      Console.WriteLine("Yes");
    }
    else {
      Console.WriteLine("No");
    }
  }
}
// This code is contributed by user_dtewbxkn77n


Javascript




//Javascript Code
function isSumSquare(N) {
    for (let x = 0; x * x < N; x++) {
        let sum = x * x + (x + 1) * (x + 1);
        if (sum === N) {
            return true;
        }
    }
    return false;
}
 
let N = 13;
if (isSumSquare(N)) {
    console.log("Yes");
} else {
    console.log("No");
}


Output: 

Yes

Time Complexity: O(N) 
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments