Dado un número entero N , la tarea es comprobar si N se puede representar como una suma de cuadrados de dos números enteros consecutivos o no.
Ejemplos:
Entrada: N = 5
Salida: Sí
Explicación:
El entero 5 = 1 2 + 2 2 donde 1 y 2 son números consecutivos.Entrada: 13
Salida: Sí
Explicación:
13 = 2 2 + 3 2
Enfoque: Esta ecuación se puede representar como:
=>
=>
=>
Finalmente, verifique que el valor de calculado usando esta fórmula sea un número entero, lo que significa que N se puede representar como la suma de los cuadrados de 2 números enteros consecutivos
A continuación se muestra la implementación del enfoque anterior:
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>
Yes
Nota: para imprimir los números enteros, podemos resolver fácilmente la ecuación anterior para obtener las raíces.
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)