Find Last Digit Of a^b for Large Numbers

2.6

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

This is simple mathematical problem. It is recommended to refer this article for some basic concept of solving such problems. Here both numbers are very large so we have to store them as a string.
table

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 .

Algorithm :

  1. Since number are very large we store them as a string.
  2. Take last digit in base a.
  3. Now calculate b%4. Here b is very large so we follow this approach to calculate mod of large number.
    • 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 C++ implementation of above algorithm.

// C++ code to find last digit of a^b
#include<bits/stdc++.h>
using namespace std;

// Function to find b % a
int Modulo(int a, char b[])
{
    // Initialize result
    int mod = 0;

    // calculating mod of b with a to make
    // b like 0 <= b < a
    for (int i=0; i<strlen(b); i++)
        mod = (mod*10 + b[i] - '0')%a;

    return mod; // return modulo
}

// function to find last digit of a^b
int LastDigit(char a[], char b[])
{
    int len_a = strlen(a), len_b = strlen(b);

    // if a and b both are 0
    if (len_a==1 && len_b==1 && b[0]=='0' && a[0]=='0')
        return 1;

    // if exponent is 0
    if (len_b==1 && b[0]=='0' )
        return 1;

    // if 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.
    // if exponent is not divisible by 4 that means last
    // digit will be pow(a, b%4) % 10
    int exp = (Modulo(4,b)==0)? 4 : Modulo(4, b);

    // Find last digit in 'a' and compute its exponent
    int res = pow(a[len_a - 1] - '0', exp);

    // Return last digit of result
    return res % 10;
}

// Driver program to run test case
int main()
{
    char a[] = "117", b[] = "3";
    cout << LastDigit(a, b);
    return 0;
}

Output:

3

This article is contributed by Shashank Mishra ( Gullu ). This article is reviewed by team geeksforgeeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2.6 Average Difficulty : 2.6/5.0
Based on 17 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.