Given two number X and N, the task is to find the last digit of X raised to last digit of N factorial, i.e. .
Input: X = 5, N = 2
Since, 2! mod 10 = 2
therefore 52 = 25 and the last digit of 25 is 5.
Input: X = 10, N = 4
Since, 4! mod 10 = 24 mod 10 = 4
therefore 104 = 10000 and the last digit of 10000 is 0.
Approach: The most efficient way to solve this problem is to find any pattern in the required last digit, with the help of last digit of N! and last digit of X raised to Y
Below is the various observation of the above-given equation:
- If N = 0 or N = 1, then the last digit is 1 or respectively.
- Since 5! is 120, therefore for N ≥ 5 the value of (N! mod 10) will be zero.
- Now we are left with digit 2, 3, 4. For this we have:
for N = 2,
N! mod 10 = 2! mod 10 = 2
for N = 3,
N! mod 10 = 3! mod 10 = 6
for N = 4,
N! mod 10 = 4! mod 10 = 24 mod 10 = 4
Now for X2, X4, and X6
we will check that after which nth power of Xn the value of last digit repeats,
i.e, after which nth power of last digit of Xn the value of last digit repeats.
- Below is the table for what power of the last digit from 0 to 9 in any number repeats:
Number Cyclicity 0 1 1 1 2 4 3 4 4 2 5 1 6 1 7 4 8 4 9 2
Below are the steps based on the above observations:
- If X is not a multiple of 10 then divide the evaluated value of by cyclicity of the last digit of X. If remainder(say r) is 0 then do the following:
- If the last digit of X is any of 2, 4, 6, or 8 then the answer will be 6.
- If the last digit of X is any of 1, 3, 7, or 9 then the answer will be 1.
- If the last digit of X is 5 then answer will be 5.
- Else if remainder(say r) is a non-zero then answer is , where ‘l’ is the last digit of X.
- Else if X is a multiple of 10 then the answer will be 0 always.
Below is the implementation of the above approach:
Time Complexity: O(1)
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.
- Find last five digits of a given five digit number raised to power five
- First digit in factorial of a number
- Maximum number with same digit factorial product
- K-th digit in 'a' raised to power 'b'
- Find the last digit when factorial of A divides factorial of B
- Find unit digit of x raised to power y
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Count of Numbers in Range where first digit is equal to last digit of the number
- Find the remainder when First digit of a number is divided by its Last digit
- Last non-zero digit of a factorial
- Largest number less than N with digit sum greater than the digit sum of N
- Find last digit in factorial
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count 'd' digit positive integers with 0 as a digit
- Count n digit numbers not having a particular digit
- Check if frequency of each digit is less than the digit
- Least Greater number with same digit sum
- Nth number whose sum of digit is multiple of 10
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.