Dado un número representado como string str que consta del dígito 1 solamente, es decir , 1, 11, 111, … . La tarea es encontrar la suma de los dígitos del cuadrado del número dado.
Ejemplos:
Entrada: str = 11
Salida: 4
11 2 = 121
1 + 2 + 1 = 4Entrada: str = 1111
Salida: 16
Enfoque ingenuo: encuentre el cuadrado del número dado y luego encuentre la suma de sus dígitos.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the sum // of the digits of num ^ 2 int squareDigitSum(string number) { int summ = 0; int num = stoi(number); // Store the square of num int squareNum = num * num; // Find the sum of its digits while(squareNum > 0) { summ = summ + (squareNum % 10); squareNum = squareNum / 10; } return summ; } // Driver code int main() { string N = "1111"; cout << squareDigitSum(N); return 0; } // This code is contributed by Princi Singh
Java
// Java implementation of the approach // Java implementation of the approach class GFG { // Function to return the sum // of the digits of num ^ 2 static int squareDigitSum(String number) { int summ = 0; int num = Integer.parseInt(number); // Store the square of num int squareNum = num * num; // Find the sum of its digits while(squareNum > 0) { summ = summ + (squareNum % 10); squareNum = squareNum / 10; } return summ; } // Driver code public static void main (String[] args) { String N = "1111"; System.out.println(squareDigitSum(N)); } } // This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach # Function to return the sum # of the digits of num ^ 2 def squareDigitSum(num): summ = 0 num = int(num) # Store the square of num squareNum = num * num # Find the sum of its digits while squareNum > 0: summ = summ + (squareNum % 10) squareNum = squareNum//10 return summ # Driver code if __name__ == "__main__": N = "1111" print(squareDigitSum(N))
C#
// C# implementation of the approach using System; class GFG { // Function to return the sum // of the digits of num ^ 2 static int squareDigitSum(String number) { int summ = 0; int num = int.Parse(number); // Store the square of num int squareNum = num * num; // Find the sum of its digits while(squareNum > 0) { summ = summ + (squareNum % 10); squareNum = squareNum / 10; } return summ; } // Driver code public static void Main (String[] args) { String s = "1111"; Console.WriteLine(squareDigitSum(s)); } } // This code is contributed by Princi Singh
Javascript
<script> // Javascript implementation of the approach // Function to return the sum // of the digits of num ^ 2 function squareDigitSum(number) { var summ = 0; var num = parseInt(number); // Store the square of num var squareNum = num * num; // Find the sum of its digits while (squareNum > 0) { summ = summ + (squareNum % 10); squareNum = parseInt(squareNum / 10); } return summ; } // Driver code var N = "1111"; document.write(squareDigitSum(N)); // This code is contributed by todaysgaurav </script>
16
Enfoque eficiente: Se puede observar que en el cuadrado del número dado, la secuencia [1, 2, 3, 4, 5, 6, 7, 9, 0] se repite en la parte izquierda y la secuencia [0, 9, 8, 7, 6, 5, 4, 3, 2, 1] se repite en la parte derecha. Ambas secuencias aparecen piso (longitud (str) / 9) veces y la suma de ambas secuencias es 81 y el cuadrado del número agrega un 1 adicional al final.
Entonces, la suma de todos estos sería [piso(longitud(string) / 9)] * 81 + 1 .
Y los dígitos del medio tienen una secuencia como si length(str) % 9 = a, entonces la secuencia del medio es [1, 2, 3….a, a – 1, a – 2, … 2] . Ahora, se puede observar que la suma de esta parte[1, 2, 3….a] es igual a (a * (a + 1)) / 2 y la suma de la otra parte [a – 1, a – 2, … 2] es ((a * (a – 1)) / 2) – 1 .
Suma total = piso (longitud (string) / 9) * 81 + 1 + (longitud (string) % 9) 2 – 1 = piso (longitud (string) / 9) * 81 + (longitud (string) % 9) 2 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define lli long long int // Function to return the sum // of the digits of num^2 lli squareDigitSum(string s) { // To store the number of 1's lli lengthN = s.length(); // Find the sum of the digits of num^2 lli result = (lengthN / 9) * 81 + pow((lengthN % 9), 2); return result; } // Driver code int main() { string s = "1111"; cout << squareDigitSum(s); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the sum // of the digits of num^2 static long squareDigitSum(String s) { // To store the number of 1's long lengthN = s.length(); // Find the sum of the digits of num^2 long result = (lengthN / 9) * 81 + (long)Math.pow((lengthN % 9), 2); return result; } // Driver code public static void main (String[] args) { String s = "1111"; System.out.println(squareDigitSum(s)); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach # Function to return the sum # of the digits of num ^ 2 def squareDigitSum(num): # To store the number of 1's lengthN = len(num) # Find the sum of the digits of num ^ 2 result = (lengthN//9)*81 + (lengthN % 9)**2 return result # Driver code if __name__ == "__main__" : N = "1111" print(squareDigitSum(N))
C#
// C# implementation of the approach using System; class GFG { // Function to return the sum // of the digits of num^2 static long squareDigitSum(String s) { // To store the number of 1's long lengthN = s.Length; // Find the sum of the digits of num^2 long result = (lengthN / 9) * 81 + (long)Math.Pow((lengthN % 9), 2); return result; } // Driver code public static void Main (String[] args) { String s = "1111"; Console.WriteLine(squareDigitSum(s)); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript implementation of the approach // Function to return the sum // of the digits of num^2 function squareDigitSum(s) { // To store the number of 1's let lengthN = s.length; // Find the sum of the digits of num^2 let result = parseInt(lengthN / 9) * 81 + Math.pow((lengthN % 9), 2); return result; } // Driver code let s = "1111"; document.write(squareDigitSum(s)); </script>
16
Tiempo Complejidad O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Shubham_Chauhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA