Find Last Digit of a^b for Large Numbers
Last Updated :
01 Aug, 2022
You are given two integer numbers, the base a (number of digits d, such that 1 <= d <= 1000) and the index b (0 <= b <= 922*10^15). You have to find the last digit of a^b.
Examples:
Input : 3 10
Output : 9
Input : 6 2
Output : 6
Input : 150 53
Output : 0
After taking few examples, we can notice below pattern.
Number | Last digits that repeat in cycle
1 | 1
2 | 4, 8, 6, 2
3 | 9, 7, 1, 3
4 | 6, 4
5 | 5
6 | 6
7 | 9, 3, 1, 7
8 | 4, 2, 6, 8
9 | 1, 9
In the given table, we can see that maximum length for cycle repetition is 4.
Example: 2*2 = 4*2 = 8*2 = 16*2 = 32 last digit in 32 is 2 that means after multiplying 4 times digit repeat itself. So the algorithm is very simple .
Source : Brilliants.org
Algorithm :
- Since number are very large we store them as a string.
- Take last digit in base a.
- Now calculate b%4. Here b is very large.
- If b%4==0 that means b is completely divisible by 4, so our exponent now will be exp = 4 because by multiplying number 4 times, we get the last digit according to cycle table in above diagram.
- If b%4!=0 that means b is not completely divisible by 4, so our exponent now will be exp=b%4 because by multiplying number exponent times, we get the last digit according to cycle table in above diagram.
- Now calculate ldigit = pow( last_digit_in_base, exp ).
- Last digit of a^b will be ldigit%10.
Below is the implementation of above algorithm.
C++
#include <bits/stdc++.h>
using namespace std;
int Modulo(int a, char b[])
{
int mod = 0;
for (int i = 0; i < strlen(b); i++)
mod = (mod * 10 + b[i] - '0') % a;
return mod;
}
int LastDigit(char a[], char b[])
{
int len_a = strlen(a), len_b = strlen(b);
if (len_a == 1 && len_b == 1 && b[0] == '0' && a[0] == '0')
return 1;
if (len_b == 1 && b[0] == '0')
return 1;
if (len_a == 1 && a[0] == '0')
return 0;
int exp = (Modulo(4, b) == 0) ? 4 : Modulo(4, b);
int res = pow(a[len_a - 1] - '0', exp);
return res % 10;
}
int main()
{
char a[] = "117", b[] = "3";
cout << LastDigit(a, b);
return 0;
}
|
Java
import java.io.*;
import java.math.*;
class GFG {
static int Modulo(int a, char b[])
{
int mod = 0;
for (int i = 0; i < b.length; i++)
mod = (mod * 10 + b[i] - '0') % a;
return mod;
}
static int LastDigit(char a[], char b[])
{
int len_a = a.length, len_b = b.length;
if (len_a == 1 && len_b == 1 && b[0] == '0' && a[0] == '0')
return 1;
if (len_b == 1 && b[0] == '0')
return 1;
if (len_a == 1 && a[0] == '0')
return 0;
int exp = (Modulo(4, b) == 0) ? 4 : Modulo(4, b);
int res = (int)(Math.pow(a[len_a - 1] - '0', exp));
return res % 10;
}
public static void main(String args[]) throws IOException
{
char a[] = "117".toCharArray(), b[] = { '3' };
System.out.println(LastDigit(a, b));
}
}
|
Python3
def last_digit(a, b):
a = int(a)
b = int(b)
if a == 0 and b == 0:
return 1
if b == 0:
return 1
if a == 0:
return 0
if b % 4 == 0:
res = 4
else:
res = b % 4
num = pow(a, res)
return num % 10
a = "117"
b = "3"
print(last_digit(a,b))
|
C#
using System;
class GFG {
static int Modulo(int a, char[] b)
{
int mod = 0;
for (int i = 0; i < b.Length; i++)
mod = (mod * 10 + b[i] - '0') % a;
return mod;
}
static int LastDigit(char[] a, char[] b)
{
int len_a = a.Length, len_b = b.Length;
if (len_a == 1 && len_b == 1 &&
b[0] == '0' && a[0] == '0')
return 1;
if (len_b == 1 && b[0] == '0')
return 1;
if (len_a == 1 && a[0] == '0')
return 0;
int exp = (Modulo(4, b) == 0) ? 4
: Modulo(4, b);
int res = (int)(Math.Pow(a[len_a - 1]
- '0', exp));
return res % 10;
}
public static void Main()
{
char[] a = "117".ToCharArray(),
b = { '3' };
Console.Write(LastDigit(a, b));
}
}
|
PHP
<?php
function Modulo($a, $b)
{
$mod = 0;
for ($i = 0; $i < strlen($b); $i++)
$mod = ($mod * 10 + $b[$i] - '0') % $a;
return $mod;
}
function LastDigit($a, $b)
{
$len_a = strlen($a); $len_b = strlen($b);
if ($len_a == 1 && $len_b == 1 &&
$b[0] == '0' && $a[0] == '0')
return 1;
if ($len_b == 1 && $b[0] == '0')
return 1;
if ($len_a == 1 && $a[0] == '0')
return 0;
$exp = (Modulo(4, $b) == 0) ? 4 :
Modulo(4, $b);
$res = pow($a[$len_a - 1] - '0', $exp);
return $res % 10;
}
$a = "117";
$b = "3";
echo LastDigit($a, $b);
?>
|
Javascript
<script>
function Modulo(a, b)
{
let mod = 0;
for (let i = 0; i < b.length; i++)
mod = (mod * 10 + b[i] - '0') % a;
return mod;
}
function LastDigit(a, b)
{
let len_a = a.length;
let len_b = b.length;
if (len_a == 1 && len_b == 1 &&
b[0] == '0' && a[0] == '0')
return 1;
if (len_b == 1 && b[0] == '0')
return 1;
if (len_a == 1 && a[0] == '0')
return 0;
exp = (Modulo(4, b) == 0) ? 4 :
Modulo(4, b);
res = Math.pow(a[len_a - 1] - '0', exp);
return res % 10;
}
let a = "117";
let b = "3";
document.write(LastDigit(a, b));
</script>
|
Output :
3
This article is reviewed by team geeksforgeeks.
Shashank Mishra ( Gullu )
Please Login to comment...