Un número piramidal cuadrado representa la suma de los cuadrados de los primeros números naturales . Los primeros números piramidales cuadrados son 1, 5, 14, 30, 55, 91, 140, 204, 285, 385, 506, …
Geométricamente, estos números representan el número de esferas que se apilarán para formar una pirámide con base cuadrada. Consulte esta imagen de Wiki para obtener más claridad.
Dado un número s (1 <= s <= 1000000000). Si s es la suma de los cuadrados de los primeros n números naturales, imprima n, de lo contrario imprima -1.
Ejemplos:
Input : 14 Output : 3 Explanation : 1*1 + 2*2 + 3*3 = 14 Input : 26 Output : -1
Una solución simple es ejecutar todos los números a partir de 1, calcular la suma actual. Si la suma actual es igual a la suma dada, devolvemos verdadero, de lo contrario, falso.
C++
// C++ program to check if a // given number is sum of // squares of natural numbers. #include <iostream> using namespace std; // Function to find if the // given number is sum of // the squares of first n // natural numbers int findS(int s) { int sum = 0; // Start adding squares of // the numbers from 1 for (int n = 1; sum < s; n++) { sum += n * n; // If sum becomes equal to s // return n if (sum == s) return n; } return -1; } // Drivers code int main() { int s = 13; int n = findS(s); n == -1 ? cout << "-1" : cout << n; return 0; }
C
// C program to check if a // given number is sum of // squares of natural numbers. #include <stdio.h> // Function to find if the // given number is sum of // the squares of first n // natural numbers int findS(int s) { int sum = 0; // Start adding squares of // the numbers from 1 for (int n = 1; sum < s; n++) { sum += n * n; // If sum becomes equal to s // return n if (sum == s) return n; } return -1; } // Drivers code int main() { int s = 13; int n = findS(s); n == -1 ? printf("-1") : printf("%d",n); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java program to check if a // given number is sum of // squares of natural numbers. class GFG { // Function to find if the // given number is sum of // the squares of first // n natural numbers static int findS(int s) { int sum = 0; // Start adding squares of // the numbers from 1 for (int n = 1; sum < s; n++) { sum += n * n; // If sum becomes equal to s // return n if (sum == s) return n; } return -1; } // Drivers code public static void main(String[] args) { int s = 13; int n = findS(s); if (n == -1) System.out.println("-1"); else System.out.println(n); } }
Python3
# Python3 program to find if # the given number is sum of # the squares of first # n natural numbers # Function to find if the given # number is sum of the squares # of first n natural numbers def findS (s): _sum = 0 n = 1 # Start adding squares of # the numbers from 1 while(_sum < s): _sum += n * n n+= 1 n-= 1 # If sum becomes equal to s # return n if _sum == s: return n return -1 # Driver code s = 13 n = findS (s) if n == -1: print("-1") else: print(n)
C#
// C# program to check if a given // number is sum of squares of // natural numbers. using System; class GFG { // Function to find if the given // number is sum of the squares // of first n natural numbers static int findS(int s) { int sum = 0; // Start adding squares of // the numbers from 1 for (int n = 1; sum < s; n++) { sum += n * n; // If sum becomes equal // to s return n if (sum == s) return n; } return -1; } // Drivers code public static void Main() { int s = 13; int n = findS(s); if(n == -1) Console.Write("-1") ; else Console.Write(n); } } // This code is contribute by // Smitha Dinesh Semwal
PHP
<?php // PHP program to check if a // given number is sum of // squares of natural numbers. // Function to find if the given number // is sum of the squares of first n // natural numbers function findS($s) { $sum = 0; // Start adding squares of // the numbers from 1 for ($n = 1; $sum < $s; $n++) { $sum += $n * $n; // If sum becomes equal to s // return n if ($sum == $s) return $n; } return -1; } // Drivers code $s = 13; $n = findS($s); if($n == -1) echo("-1"); else echo($n); // This code is contributed by Ajit. ?>
Javascript
<script> // JavaScript to check if a // given number is sum of // squares of natural numbers. // Function to find if the // given number is sum of // the squares of first // n natural numbers function findS(s) { let sum = 0; // Start adding squares of // the numbers from 1 for (let n = 1; sum < s; n++) { sum += n * n; // If sum becomes equal to s // return n if (sum == s) return n; } return -1; } // Driver code let s = 13; let n = findS(s); if (n == -1) document.write("-1"); else document.write(n); // This code is contributed by souravghosh0416. </script>
PRODUCCIÓN :
-1
Una solución alternativa es utilizar el método de Newton Raphson .
Sabemos que la suma de los cuadrados de los primeros n números naturales es n * (n + 1) * (2*n + 1) / 6 .
Podemos escribir las soluciones como
k * (k + 1) * (2*k + 1) / 6 = s
k * (k + 1) * (2*k + 1) – 6s = 0
Podemos encontrar las raíces de las cúbicas anteriores ecuación usando el método de Newton Raphson, luego verifique si la raíz es un número entero o no.
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA