给定两个数字x和y,找到x y的单位数字。
例子 :
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.
方法1(简单)计算x y的值并找到其最后一位。此方法导致x和y值稍大的溢出。
方法2(高效)
1)查找x的最后一位。
2)在模10下计算x ^ y并返回其值。
C++
// Efficient C++ program to
// find unit digit of x^y.
#include
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
Javascript
C++
// C++ code to find the unit digit of x
// raised to power y.
#include
#include
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
Javascript
输出 :
6
进一步优化:我们可以在Log y中计算模块化功率。
方法3(基于最后一位的循环性质直接进行)
此方法取决于x的最后一位的周期性
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
所以在这里我们直接用4修改幂y,因为这是所有数字重复开始后的最后一个幂
在此之后,我们简单地用数字x最后一位加幂,然后得到产生的数字的单位位数。
C++
// C++ code to find the unit digit of x
// raised to power y.
#include
#include
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
Java脚本
输出 :
3
感谢DevanshuAgarwal建议上述解决方案。
如何处理大量数字?
大数a ^ b的最后一位数字的有效方法