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.
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
- 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.
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Last digit of a number raised to last digit of N factorial
- Last digit of Product of two Large or Small numbers (a * b)
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of N-digit numbers having digit XOR as single digit
- Find the remainder when First digit of a number is divided by its Last digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Multiply large integers under large modulo
- Print all K digit repeating numbers in a very large number
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Last digit of sum of numbers in the given range in the Fibonacci series
- Count all numbers up to N having M as the last digit
- N digit numbers having difference between the first and last digits as K
- Recursive sum of digit in n^x, where n and x are very large
- Count n digit numbers not having a particular digit
- Program to find last digit of n'th Fibonnaci Number
- Find the last digit of given series
- Find the last digit when factorial of A divides factorial of B
- Find last five digits of a given five digit number raised to power five
- Program to find the last digit of X in base Y
Improved By : nitin mittal