You are given two integers a and b in the form of strings. You need to find the last digit of a^b (ab).
Examples:
Input: a = "3", b = "10"
Output: 9
Explanation: 310 = 59049. The last digit is 9.Input: a = "6", b = "2"
Output: 6
Explanation: 62 = 36. The last digit is 6.
Try It Yourself
Using Cyclicity of Last Digit (Mod 10) - O(n) Time O(1) Space
The last digit of powers follows a repeating cycle (maximum length = 4). Instead of computing large powers, we reduce the exponent using modulo 4 and use only the last digit of the base.
Here are few examples numbers and last digits of their powers
- 1: 1, 1, 1, 1, 1, 1, .... [Cycle Length is 1]
- 2: 4, 8, 6, 2, 4, 8, 6, 2,... [Cycle Length is 4]
- 3: 9, 7, 1, 3, 9, 7, 1, 3,... [Cycle Length is 4]
- 4: 6, 4, 6, 4, 6, 4, 6, 4, ...... [Cycle Length is 2]
- 5: 5, 5, 5, 5, 5, ... [Cycle Length is 1]
- 6: 6, 6, 6, 6, 6...... [Cycle Length is 1]
- 7: 9, 3, 1, 7, 9, 3, 1, 7,...... [Cycle Length is 4]
- 8: 4, 2, 6, 8, 4, 2, 6, 8, ..... [Cycle Length is 4]
- 9: 1, 9, 1, 9, 1, 9...... [Cycle Length is 2]
Algorithm:
- Extract the last digit of base a.
- Compute b % 4 using string-based modulo (since b is large).
- If b % 4 == 0, take the exponent as 4, otherwise b % 4.
- Compute power using reduced exponent.
- Return the last digit of the result (% 10).
Why does this work?
- Any number’s last digit depends o (a^b) mod 10. The possible last digits are 0–9. When you keep multiplying, results must eventually repeat (finite possibilities)
- If the base 'a' is coprime with 10 (i.e., last digit is 1, 3, 7, or 9), then (a^4) mod 10 is 1. After every 4 powers, the pattern resets. so last digits repeat every 4.
- If the base is not coprime with 10 (i.e., last digit is 2, 4, 5, 6 or 8), then we can find the pattern case by case basis as there are finite possibilities. For example for 2, 5 or 6, it is always the same digit.
// C++ code to find last digit of a^b
#include <bits/stdc++.h>
using namespace std;
int Modulo(int a, string b)
{
int mod = 0;
// calculate (b mod a) to make
// b in range 0 <= b < a
for (int i = 0; i < b.length(); i++)
mod = (mod * 10 + b[i] - '0') % a;
return mod;
}
int getLastDigit(string a, string b)
{
int len_a = a.length(), len_b = b.length();
// exponent is 0
if (len_b == 1 && b[0] == '0')
return 1;
// base is 0
if (len_a == 1 && a[0] == '0')
return 0;
// if exponent is divisible by 4 that means last
// digit will be pow(a, 4) % 10
// otherwise last digit will be pow(a, b%4) % 10
int exp = (Modulo(4, b) == 0) ? 4 : Modulo(4, b);
int res = pow(a[len_a - 1] - '0', exp);
return res % 10;
}
// Driver program to run test case
int main()
{
char a[] = "3", b[] = "10";
cout << getLastDigit(a, b);
return 0;
}
import java.util.*;
public class GfG {
int Modulo(int a, String b) {
int mod = 0;
// calculate (b mod a) to make
// b in range 0 <= b < a
for (int i = 0; i < b.length(); i++)
mod = (mod * 10 + b.charAt(i) - '0') % a;
return mod;
}
int getLastDigit(String a, String b) {
int len_a = a.length(), len_b = b.length();
// exponent is 0
if (len_b == 1 && b.charAt(0) == '0')
return 1;
// base is 0
if (len_a == 1 && a.charAt(0) == '0')
return 0;
// if exponent is divisible by 4 that means last
// digit will be pow(a, 4) % 10
// otherwise last digit will be pow(a, b%4) % 10
int exp = (Modulo(4, b) == 0) ? 4 : Modulo(4, b);
int res = (int)Math.pow(a.charAt(len_a - 1) - '0', exp);
return res % 10;
}
public static void main(String[] args) {
GfG m = new GfG();
String a = "3", b = "10";
System.out.println(m.getLastDigit(a, b));
}
}
def Modulo(a, b):
mod = 0
# calculate (b mod a) to make
# b in range 0 <= b < a
for i in range(len(b)):
mod = (mod * 10 + int(b[i])) % a
return mod
def getLastDigit(a, b):
len_a = len(a)
len_b = len(b)
# exponent is 0
if len_b == 1 and b[0] == '0':
return 1
# base is 0
if len_a == 1 and a[0] == '0':
return 0
# if exponent is divisible by 4 that means last
# digit will be pow(a, 4) % 10
# otherwise last digit will be pow(a, b%4) % 10
exp = 4 if Modulo(4, b) == 0 else Modulo(4, b)
res = pow(int(a[len_a - 1]), exp)
return res % 10
if __name__ == "__main__":
a = "3"
b = "10"
print(getLastDigit(a, b))
using System;
public class GfG
{
public int Modulo(int a, string b)
{
int mod = 0;
// calculate (b mod a) to make
// b in range 0 <= b < a
for (int i = 0; i < b.Length; i++)
mod = (mod * 10 + b[i] - '0') % a;
return mod;
}
public int GetLastDigit(string a, string b)
{
int len_a = a.Length, len_b = b.Length;
// if a and b both are 0
if (len_a == 1 && len_b == 1 && a[0] == '0' && b[0] == '0')
return 1;
// exponent is 0
if (len_b == 1 && b[0] == '0')
return 1;
// base is 0
if (len_a == 1 && a[0] == '0')
return 0;
int mod = Modulo(4, b);
int exp = (mod == 0) ? 4 : mod;
int res = (int)Math.Pow(a[len_a - 1] - '0', exp);
return res % 10;
}
public static void Main()
{
GfG p = new GfG();
string a = "3", b = "10";
Console.WriteLine(p.GetLastDigit(a, b));
}
}
function Modulo(a, b) {
let mod = 0;
// calculate (b mod a) to make
// b in range 0 <= b < a
for (let i = 0; i < b.length; i++) {
mod = (mod * 10 + parseInt(b[i])) % a;
}
return mod;
}
function getLastDigit(a, b) {
let len_a = a.length, len_b = b.length;
// exponent is 0
if (len_b == 1 && b[0] == '0')
return 1;
// base is 0
if (len_a == 1 && a[0] == '0')
return 0;
// if exponent is divisible by 4 that means last
// digit will be pow(a, 4) % 10
// otherwise last digit will be pow(a, b%4) % 10
let exp = (Modulo(4, b) == 0)? 4 : Modulo(4, b);
let res = Math.pow(parseInt(a[len_a - 1]), exp);
return Math.floor(res % 10);
}
// Driver program to run test case
let a = "3", b = "10";
console.log(getLastDigit(a, b));
Output
9
Time Complexity: O(n)
Space Complexity: O(1)