Dado un número hecho de un solo dígito, encuentre su cuadrado. Se puede suponer que los dígitos individuales son 3, 6 y 9. Los números pueden ser muy grandes, es decir, pueden exceder long long int.
Ejemplos:
Input : 33 66 99 Output : Square of 33 is : 1089 Square of 66 is : 4356 Square of 99 is : 9801 Input : 333 666 999 Output : Square of 333 is : 110889 Square of 666 is : 443556 Square of 999 is : 998001
Método 1 (Escribir como múltiplos de 1111…1)
Un hecho interesante es que cada uno de esos números se puede representar como un múltiplo de 1111…1. Por ejemplo, 33333 = 3 * 11111. Los cuadrados de 11, 111, 1111, 11111… son 121, 12321, 1234321, 123454321,… respectivamente. Entonces, una solución simple es encontrar el cuadrado de 111…11 y luego multiplicar el resultado por 3 o 6 o 9 (Podemos usar la multiplicación con un número grande ).
Método 2 (usando patrones de dígitos)
Para 333….333 Cuente el no. de dígitos e imprima de la siguiente manera:
Supongamos que no. del dígito es n, luego escriba n-1 veces 1 y luego escriba una vez 0 y luego escriba n-1 vez 8 y por último escriba 9.
Ejemplo:
{ 3333 } = 11108889
Para 666….666 Cuente el no. de dígitos e imprima de la siguiente manera:
Suponga que el número de dígitos es n, luego escriba n-1 por 4 y luego escriba una vez por 3 y luego escriba n-1 por 5 y por último escriba 6.
Ejemplo:
{ 6666 } = 44435556
Para 999….999 Cuente el no. de dígitos e imprímalos de la siguiente manera:
suponga que el número de dígitos es n, luego escriba n-1 veces 9 y luego escriba una vez 8 y luego escriba n-1 vez 0 y por último escriba 1.
Ejemplo:
{ 9999 } = 99980001
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find square of // these large numbers #include <iostream> using namespace std; // Function to find the square of // 333...333, 666...666 and 999...999 string find_Square_369(string num) { char a, b, c, d; // if the number is 333...333 if (num[0] == '3') a = '1', b = '0', c = '8', d = '9'; // if the number is 666...666 else if (num[0] == '6') a = '4', b = '3', c = '5', d = '6'; // if the number is 999...999 else a = '9', b = '8', c = '0', d = '1'; // variable for hold result string result = ""; // find the no of digit int size = num.size(); // add size-1 time a in result for (int i = 1; i < num.size(); i++) result += a; // add one time b in result result += b; // add size-1 time c in result for (int i = 1; i < num.size(); i++) result += c; // add one time d in result result += d; // return result return result; } // Drivers code int main() { string num_3, num_6, num_9; num_3 = "3333"; num_6 = "6666"; num_9 = "9999"; string result = ""; // find square of 33..33 result = find_Square_369(num_3); cout << "Square of " << num_3 << " is : " << result << endl; // find square of 66..66 result = find_Square_369(num_6); cout << "Square of " << num_6 << " is : " << result << endl; // find square of 66..66 result = find_Square_369(num_9); cout << "Square of " << num_9 << " is : " << result << endl; return 0; }
Java
// Java program to find square of // these large numbers class GFG { // Function to find the square of // 333...333, 666...666 and 999...999 static String find_Square_369(String num) { char a, b, c, d; // if the number is 333...333 if (num.charAt(0) == '3') {a = '1'; b = '0'; c = '8'; d = '9';} // if the number is 666...666 else if (num.charAt(0) == '6') {a = '4'; b = '3'; c = '5'; d = '6';} // if the number is 999...999 else {a = '9'; b = '8'; c = '0'; d = '1';} // variable for hold result String result = ""; // find the no of digit int size = num.length(); // add size-1 time a in result for (int i = 1; i < size; i++) result += a; // add one time b in result result += b; // add size-1 time c in result for (int i = 1; i < size; i++) result += c; // add one time d in result result += d; // return result return result; } // Drivers code public static void main(String[] args) { String num_3, num_6, num_9; num_3 = "3333"; num_6 = "6666"; num_9 = "9999"; String result = ""; // find square of 33..33 result = find_Square_369(num_3); System.out.println("Square of " + num_3 + " is : " + result); // find square of 66..66 result = find_Square_369(num_6); System.out.println("Square of " + num_9 + " is : " + result); // find square of 66..66 result = find_Square_369(num_9); System.out.println("Square of " + num_9 + " is : " + result); } } // This code is contributed by Smitha.
Python 3
# Python 3 program to find square of # these large numbers # Function to find the square of # 333...333, 666...666 and 999...999 def find_Square_369(num): # if the number is 333...333 if (num[0] == '3'): a = '1' b = '0' c = '8' d = '9' # if the number is 666...666 elif (num[0] == '6'): a = '4' b = '3' c = '5' d = '6' # if the number is 999...999 else: a = '9' b = '8' c = '0' d = '1' # variable for hold result result = "" # find the no of digit size = len(num) # add size-1 time a in result for i in range(1, size): result += a # add one time b in result result += b # add size-1 time c in result for i in range(1, size): result += c # add one time d in result result += d # return result return result # Drivers code # Your Python 3 Code num_3 = "3333" num_6 = "6666" num_9 = "9999" result = "" # find square of 33..33 result = find_Square_369(num_3) print("Square of " + num_3 + " is : " + result); # find square of 66..66 result = find_Square_369(num_6) print("Square of " + num_6 + " is : " + result); # find square of 66..66 result = find_Square_369(num_9) print("Square of " + num_9 + " is : " + result); # This code is contributed by Smitha
C#
// C# program to find square of // these large numbers using System; class GFG { // Function to find the square of // 333...333, 666...666 and 999...999 static string find_Square_369(string num) { char a, b, c, d; // if the number is 333...333 if (num[0] == '3') {a = '1'; b = '0'; c = '8'; d = '9';} // if the number is 666...666 else if (num[0] == '6') {a = '4'; b = '3'; c = '5'; d = '6';} // if the number is 999...999 else {a = '9'; b = '8'; c = '0'; d = '1';} // variable for hold result string result = ""; // find the no of digit int size = num.Length; // add size-1 time a in result for (int i = 1; i < size; i++) result += a; // add one time b in result result += b; // add size-1 time c in result for (int i = 1; i < size; i++) result += c; // add one time d in result result += d; // return result return result; } // Drivers code public static void Main() { string num_3, num_6, num_9; num_3 = "3333"; num_6 = "6666"; num_9 = "9999"; string result = ""; // find square of 33..33 result = find_Square_369(num_3); Console.Write("Square of " + num_3 + " is : " + result + "\n"); // find square of 66..66 result = find_Square_369(num_6); Console.Write("Square of " + num_9 + " is : " + result + "\n"); // find square of 66..66 result = find_Square_369(num_9); Console.Write("Square of " + num_9 + " is : " + result + "\n"); } } // This code is contributed by Smitha
PHP
<?php // PHP program to find square of // these large numbers // Function to find the square of // 333...333, 666...666 and 999...999 function find_Square_369($num) { // if the number is 333...333 if ($num[0] == '3') { $a = '1'; $b = '0'; $c = '8'; $d = '9'; } // if the number is 666...666 else if ($num[0] == '6') { $a = '4'; $b = '3'; $c = '5'; $d = '6'; } // if the number is 999...999 else { $a = '9'; $b = '8'; $c = '0'; $d = '1'; } // variable for hold result $result = ""; // find the no of digit $size = strlen($num); // add size-1 time a in result for ($i = 1; $i < $size; $i++) $result = $result.$a; // add one time b in result $result = $result.$b; // add size-1 time c in result for ($i = 1; $i < $size; $i++) $result = $result.$c; // add one time d in result $result = $result.$d; // return result return $result; } // Drivers code $num_3 = "3333"; $num_6 = "6666"; $num_9 = "9999"; $result = ""; // find square of 33..33 $result = find_Square_369($num_3); echo "Square of " . $num_3 . " is : " . $result ."\n" ; // find square of 66..66 $result = find_Square_369($num_6); echo "Square of " . $num_6 . " is : " .$result ."\n"; // find square of 66..66 $result = find_Square_369($num_9); echo "Square of " . $num_9 . " is : ".$result ."\n"; return 0; ?>
Javascript
<script> // Javascript program to find square of // these large numbers // Function to find the square of // 333...333, 666...666 and 999...999 function find_Square_369(num) { let a, b, c, d; // if the number is 333...333 if (num[0] == '3') {a = '1'; b = '0'; c = '8'; d = '9';} // if the number is 666...666 else if (num[0] == '6') {a = '4'; b = '3'; c = '5'; d = '6';} // if the number is 999...999 else {a = '9'; b = '8'; c = '0'; d = '1';} // variable for hold result let result = ""; // find the no of digit let size = num.length; // add size-1 time a in result for (let i = 1; i < size; i++) result += a; // add one time b in result result += b; // add size-1 time c in result for (let i = 1; i < size; i++) result += c; // add one time d in result result += d; // return result return result; } // Drivers code let num_3, num_6, num_9; num_3 = "3333"; num_6 = "6666"; num_9 = "9999"; let result = ""; // find square of 33..33 result = find_Square_369(num_3); document.write("Square of " + num_3 + " is : " + result+"<br>"); // find square of 66..66 result = find_Square_369(num_6); document.write("Square of " + num_9 + " is : " + result+"<br>"); // find square of 66..66 result = find_Square_369(num_9); document.write("Square of " + num_9 + " is : " + result+"<br>"); // This code is contributed by avanitrachhadiya2155 </script>
Producción :
Square of 3333 is : 11108889 Square of 6666 is : 44435556 Square of 9999 is : 99980001