Dados dos números x e y, encuentre el dígito unitario de x y .
Ejemplos:
Input : x = 2, y = 1 Output : 2 Explanation 2^1 = 2 so units digit is 2. Input : x = 4, y = 2 Output : 6 Explanation 4^2 = 16 so units digit is 6.
Método 1 (Simple) Calcule el valor de x y y encuentre su último dígito. Este método provoca un desbordamiento para valores ligeramente mayores de x e y.
Método 2 (Eficiente)
1) Encuentra el último dígito de x.
2) Calcule x^y bajo el módulo 10 y devuelva su valor.
C++
// Efficient C++ program to // find unit digit of x^y. #include <bits/stdc++.h> using namespace std; // Returns unit digit of x // raised to power y int unitDigitXRaisedY(int x, int y) { // Initialize result as 1 to // handle case when y is 0. int res = 1; // One by one multiply with x // mod 10 to avoid overflow. for (int i = 0; i < y; i++) res = (res * x) % 10; return res; } // Driver program int main() { cout << unitDigitXRaisedY(4, 2); return 0; }
Java
// Efficient Java program to find // unit digit of x^y. import java.io.*; class GFG { // Returns unit digit of x raised to power y static int unitDigitXRaisedY(int x, int y) { // Initialize result as 1 to // handle case when y is 0. int res = 1; // One by one multiply with x // mod 10 to avoid overflow. for (int i = 0; i < y; i++) res = (res * x) % 10; return res; } // Driver program public static void main(String args[])throws IOException { System.out.println(unitDigitXRaisedY(4, 2)); } } // This code is contributed by Nikita Tiwari.
Python3
# Python3 code to find # unit digit of x^y. # Returns unit digit of # x raised to power y def unitDigitXRaisedY( x , y ): # Initialize result as 1 to # handle case when y is 0. res = 1 # One by one multiply with x # mod 10 to avoid overflow. for i in range(y): res = (res * x) % 10 return res # Driver program print( unitDigitXRaisedY(4, 2)) # This code is contributed by Abhishek Sharma44.
C#
// Efficient Java program to find // unit digit of x^y. using System; class GFG { // Returns unit digit of x raised to power y static int unitDigitXRaisedY(int x, int y) { // Initialize result as 1 to // handle case when y is 0. int res = 1; // One by one multiply with x // mod 10 to avoid overflow. for (int i = 0; i < y; i++) res = (res * x) % 10; return res; } // Driver program public static void Main() { Console.WriteLine(unitDigitXRaisedY(4, 2)); } } // This code is contributed by vt_m.
PHP
<?php // Efficient PHP program to // find unit digit of x^y. // Returns unit digit of x // raised to power y function unitDigitXRaisedY($x, $y) { // Initialize result as 1 to // handle case when y is 0. $res = 1; // One by one multiply with x // mod 10 to avoid overflow. for ($i = 0; $i < $y; $i++) $res = ($res * $x) % 10; return $res; } // Driver Code echo(unitDigitXRaisedY(4, 2)); // This code is contributed by Ajit. ?>
Javascript
<script> // Efficient Javascript program to // find unit digit of x^y. // Returns unit digit of x // raised to power y function unitDigitXRaisedY(x, y) { // Initialize result as 1 to // handle case when y is 0. let res = 1; // One by one multiply with x // mod 10 to avoid overflow. for(let i = 0; i < y; i++) res = (res * x) % 10; return res; } // Driver Code document.write(unitDigitXRaisedY(4, 2)); // This code is contributed by _saurabh_jaiswal. </script>
Producción :
6
Complejidad de Tiempo: O(y), donde y es la potencia
Espacio Auxiliar: O(1), ya que no se requiere espacio extra
Optimizaciones adicionales: podemos calcular la potencia modular en Log y .
Método 3 (Directo basado en la naturaleza cíclica del último dígito)
Este método depende de la ciclicidad con el último dígito de x que es
x | power 2 | power 3 | power 4 | Cyclicity 0 | .................................. | .... repeat with 0 1 | .................................. | .... repeat with 1 2 | 4 | 8 | 6 | .... repeat with 2 3 | 9 | 7 | 1 | .... repeat with 3 4 | 6 |....................... | .... repeat with 4 5 | .................................. | .... repeat with 5 6 | .................................. | .... repeat with 6 7 | 9 | 3 | 1 | .... repeat with 7 8 | 4 | 2 | 6 | .... repeat with 8 9 | 1 | ...................... | .... repeat with 9
Así que aquí modificamos directamente la potencia y con 4 porque esta es la última potencia después del comienzo de la repetición de todos los números.
Después de esto, simplemente potenciamos con el último dígito del número x y luego obtenemos el dígito unitario del número producido.
C++
// C++ code to find the unit digit of x // raised to power y. #include<iostream> #include<math.h> using namespace std; // find unit digit int unitnumber(int x, int y) { // Get last digit of x x = x % 10; // Last cyclic modular value if(y!=0) y = y % 4 + 4; // here we simply return the // unit digit or the power // of a number return (((int)(pow(x, y))) % 10); } int main() { int x = 133, y = 5; // get unit digit number here we pass // the unit digit of x and the last // cyclicity number that is y%4 cout << unitnumber(x, y); return 0; }
Java
// Java code to find the unit // digit of x raised to power y. import java.io.*; import java.util.*; class GFG { // find unit digit static int unitnumber(int x, int y) { // Get last digit of x x = x % 10; // Last cyclic modular value if(y!=0) y = y % 4 + 4; // here we simply return the // unit digit or the power // of a number return (((int)(Math.pow(x, y))) % 10); } public static void main (String[] args) { int x = 133, y = 5; // get unit digit number here we pass // the unit digit of x and the last // cyclicity number that is y%4 System.out.println(unitnumber(x, y)); } } // This code is contributed by Gitanjali.
Python3
# Python3 code to find the unit # digit of x raised to power y. import math # Find unit digit def unitnumber(x, y): # Get last digit of x x = x % 10 # Last cyclic modular value if y!=0: y = y % 4 + 4 # Here we simply return # the unit digit or the # power of a number return (((int)(math.pow(x, y))) % 10) # Driver code x = 133; y = 5 # Get unit digit number here we pass # the unit digit of x and the last # cyclicity number that is y%4 print(unitnumber(x, y)) # This code is contributed by Gitanjali.
C#
// C# code to find the unit // digit of x raised to power y. using System; class GFG { // find unit digit static int unitnumber(int x, int y) { // Get last digit of x x = x % 10; // Last cyclic modular value if(y!=0) y = y % 4 + 4; // here we simply return the // unit digit or the power // of a number return (((int)(Math.Pow(x, y))) % 10); } // Driver code public static void Main () { int x = 133, y = 5; // get unit digit number here we pass // the unit digit of x and the last // cyclicity number that is y%4 Console.WriteLine(unitnumber(x, y)); } } // This code is contributed by vt_m.
PHP
<?php // PHP code to find the unit // digit of x raised to power y. // find unit digit function unitnumber($x, $y) { // Get last digit of x $x = $x % 10; // Last cyclic modular value if($y!=0) $y = $y % 4 + 4; // here we simply return the // unit digit or the power // of a number return (((int)(pow($x, $y))) % 10); } // Driver code $x = 133; $y = 5; // get unit digit number here we pass // the unit digit of x and the last // cyclicity number that is y%4 echo(unitnumber($x, $y)); // This code is contributed by Ajit. ?>
Javascript
<script> // Javascript code to find the unit // digit of x raised to power y. // find unit digit function unitnumber(x, y) { // Get last digit of x x = x % 10; // Last cyclic modular value if (y != 0) y = y % 4 + 4; // here we simply return the // unit digit or the power // of a number return ((parseInt(Math.pow(x, y))) % 10); } // Driver code let x = 133; let y = 5; // get unit digit number here we pass // the unit digit of x and the last // cyclicity number that is y%4 document.write(unitnumber(x, y)); // This code is contributed by _saurabh_jaiswal. </script>
Producción :
3
Complejidad temporal: O(log n)
Espacio auxiliar: O(1)
Gracias a DevanshuAgarwal por sugerir la solución anterior.
¿Cómo manejar números grandes?
Método eficiente para el último dígito de a^b para números grandes
Publicación traducida automáticamente
Artículo escrito por Shahnawaz_Ali y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA