Given two numbers x and y, find unit digit of xy.
Examples :
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.
Method 1 (Simple) Compute value of xy and find its last digit. This method causes overflow for slightly larger values of x and y.
Method 2 (Efficient)
1) Find last digit of x.
2) Compute x^y under modulo 10 and return its value.
C++
#include <bits/stdc++.h>
using namespace std;
int unitDigitXRaisedY( int x, int y)
{
int res = 1;
for ( int i = 0; i < y; i++)
res = (res * x) % 10;
return res;
}
int main()
{
cout << unitDigitXRaisedY(4, 2);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int unitDigitXRaisedY( int x, int y)
{
int res = 1 ;
for ( int i = 0 ; i < y; i++)
res = (res * x) % 10 ;
return res;
}
public static void main(String args[]) throws IOException
{
System.out.println(unitDigitXRaisedY( 4 , 2 ));
}
}
|
Python3
def unitDigitXRaisedY( x , y ):
res = 1
for i in range (y):
res = (res * x) % 10
return res
print ( unitDigitXRaisedY( 4 , 2 ))
|
C#
using System;
class GFG
{
static int unitDigitXRaisedY( int x, int y)
{
int res = 1;
for ( int i = 0; i < y; i++)
res = (res * x) % 10;
return res;
}
public static void Main()
{
Console.WriteLine(unitDigitXRaisedY(4, 2));
}
}
|
PHP
<?php
function unitDigitXRaisedY( $x , $y )
{
$res = 1;
for ( $i = 0; $i < $y ; $i ++)
$res = ( $res * $x ) % 10;
return $res ;
}
echo (unitDigitXRaisedY(4, 2));
?>
|
Javascript
<script>
function unitDigitXRaisedY(x, y)
{
let res = 1;
for (let i = 0; i < y; i++)
res = (res * x) % 10;
return res;
}
document.write(unitDigitXRaisedY(4, 2));
</script>
|
Output :
6
Time Complexity: O(y), where y is the power
Auxiliary Space: O(1), as no extra space is required
Further Optimizations: We can compute modular power in Log y.
Method 3 (Direct based on cyclic nature of last digit)
This method depends on the cyclicity with the last digit of x that is
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
So here we directly mod the power y with 4 because this is the last power after this all number’s repetition start
after this we simply power with number x last digit then we get the unit digit of produced number.
C++
#include<iostream>
#include<math.h>
using namespace std;
int unitnumber( int x, int y)
{
x = x % 10;
if (y!=0)
y = y % 4 + 4;
return ((( int )( pow (x, y))) % 10);
}
int main()
{
int x = 133, y = 5;
cout << unitnumber(x, y);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int unitnumber( int x, int y)
{
x = x % 10 ;
if (y!= 0 )
y = y % 4 + 4 ;
return ((( int )(Math.pow(x, y))) % 10 );
}
public static void main (String[] args)
{
int x = 133 , y = 5 ;
System.out.println(unitnumber(x, y));
}
}
|
Python3
import math
def unitnumber(x, y):
x = x % 10
if y! = 0 :
y = y % 4 + 4
return ((( int )(math. pow (x, y))) % 10 )
x = 133 ; y = 5
print (unitnumber(x, y))
|
C#
using System;
class GFG {
static int unitnumber( int x, int y)
{
x = x % 10;
if (y!=0)
y = y % 4 + 4;
return ((( int )(Math.Pow(x, y))) % 10);
}
public static void Main ()
{
int x = 133, y = 5;
Console.WriteLine(unitnumber(x, y));
}
}
|
PHP
<?php
function unitnumber( $x , $y )
{
$x = $x % 10;
if ( $y !=0)
$y = $y % 4 + 4;
return (((int)(pow( $x , $y ))) % 10);
}
$x = 133; $y = 5;
echo (unitnumber( $x , $y ));
?>
|
Javascript
<script>
function unitnumber(x, y)
{
x = x % 10;
if (y != 0)
y = y % 4 + 4;
return ((parseInt(Math.pow(x, y))) % 10);
}
let x = 133;
let y = 5;
document.write(unitnumber(x, y));
</script>
|
Time Complexity: O(log n)
Auxiliary Space: O(1)
Approach: Binomial Expansion method
Here are the steps to find the unit digit of x raised to power y using the Binomial Expansion method:
1. Handle special cases:
If y is 0, return 1 as any number raised to power 0 is 1.
If x is 0, return 0 as any number raised to power 0 is 1 and the unit digit of 0 is 0.
2. Calculate the y-th term in the expansion of (x+10)^y using the binomial theorem:
The y-th term in the expansion is given by: C(y, 0)x^y10^0 + C(y, 1)*x^(y-1)*10^1 + … + C(y, y)x^010^y
Here, C(y, k) represents the binomial coefficient, which is equal to y! / (k! * (y-k)!).
We only need to calculate the last term in this expansion, which is C(y, y)x^010^y.
3. Find the unit digit of the y-th term:
The unit digit of the y-th term is the same as the last digit of the y-th term.
We can find the last digit of the y-th term by taking the remainder of the term when divided by 10.
4. Return the unit digit found in step 3 as the result.
C++
#include <iostream>
#include <cmath>
using namespace std;
int unit_digit( int x, int y) {
if (y == 0) {
return 1;
}
if (x == 0) {
return 0;
}
int term = pow (x + 10, y);
int last_digit = term % 10;
return last_digit;
}
int main() {
cout << unit_digit(2, 1) << endl;
cout << unit_digit(4, 2) << endl;
return 0;
}
|
Java
import java.lang.Math;
public class Main {
public static int unitDigit( int x, int y) {
if (y == 0 ) {
return 1 ;
}
if (x == 0 ) {
return 0 ;
}
int term = ( int ) Math.pow(x + 10 , y);
int lastDigit = term % 10 ;
return lastDigit;
}
public static void main(String[] args) {
System.out.println(unitDigit( 2 , 1 ));
System.out.println(unitDigit( 4 , 2 ));
}
}
|
Python3
def unit_digit(x, y):
if y = = 0 :
return 1
if x = = 0 :
return 0
term = (x + 10 ) * * y
last_digit = term % 10
return last_digit
print (unit_digit( 2 , 1 ))
print (unit_digit( 4 , 2 ))
|
C#
using System;
public class MainClass {
public static int UnitDigit( int x, int y)
{
if (y == 0) {
return 1;
}
if (x == 0) {
return 0;
}
int term = ( int )Math.Pow(x + 10, y);
int lastDigit = term % 10;
return lastDigit;
}
public static void Main( string [] args)
{
Console.WriteLine(UnitDigit(2, 1));
Console.WriteLine(UnitDigit(4, 2));
}
}
|
Javascript
function unitDigit(x, y) {
if (y == 0) {
return 1;
}
if (x == 0) {
return 0;
}
let term = Math.pow(x + 10, y);
let lastDigit = term % 10;
return lastDigit;
}
console.log(unitDigit(2, 1));
console.log(unitDigit(4, 2));
|
The time complexity is O(log y), where y is the input variable
The auxiliary space also O(1)
Thanks to DevanshuAgarwal for suggesting above solution.
How to handle large numbers?
Efficient method for Last Digit Of a^b for Large Numbers
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Apr, 2023
Like Article
Save Article