Skip to content
Related Articles

Related Articles

Improve Article
Last digit of a number raised to last digit of N factorial
  • Last Updated : 29 Apr, 2021

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. X^{\left ( N! \right )mod 10}  .
Examples: 
 

Input: X = 5, N = 2 
Output:
Explanation: 
Since, 2! mod 10 = 2 
therefore 52 = 25 and the last digit of 25 is 5.
Input: X = 10, N = 4 
Output:
Explanation: 
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 X mod 10  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: 
     
NumberCyclicity
01
11
24
34
42
51
61
74
84
92
  •  

Below are the steps based on the above observations: 
 

  1. If X is not a multiple of 10 then divide the evaluated value of X^{\left ( N! \right )mod 10}  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.
  2. Else if remainder(say r) is a non-zero then answer is l^{r} mod 10  , where ‘l’ is the last digit of X.
  3. Else if X is a multiple of 10 then the answer will be 0 always.

Below is the implementation of the above approach: 
 

C++




// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to find a^b using
// binary exponentiation
long power(long a, long b, long c)
{
     
    // Initialise result
    long result = 1;
 
    while (b > 0)
    {
         
        // If b is odd then,
        // multiply result by a
        if ((b & 1) == 1)
        {
            result = (result * a) % c;
        }
         
        // b must be even now
        // Change b to b/2
        b /= 2;
 
        // Change a = a^2
        a = (a * a) % c;
    }
    return result;
}
 
// Function to find the last digit
// of the given equation
long calculate(long X, long N)
{
    int a[10];
 
    // To store cyclicity
    int cyclicity[11];
 
    // Store cyclicity from 1 - 10
    cyclicity[1] = 1;
    cyclicity[2] = 4;
    cyclicity[3] = 4;
    cyclicity[4] = 2;
    cyclicity[5] = 1;
    cyclicity[6] = 1;
    cyclicity[7] = 4;
    cyclicity[8] = 4;
    cyclicity[9] = 2;
    cyclicity[10] = 1;
 
    // Observation 1
    if (N == 0 || N == 1)
    {
        return (X % 10);
    }
     
    // Observation 3
    else if (N == 2 || N == 3 || N == 4)
    {
        long temp = (long)1e18;
         
        // To store the last digits
        // of factorial 2, 3, and 4
        a[2] = 2;
        a[3] = 6;
        a[4] = 4;
 
        // Find the last digit of X
        long v = X % 10;
 
        // Step 1
        if (v != 0)
        {
            int u = cyclicity[(int)v];
             
            // Divide a[N] by cyclicity
            // of v
            int r = a[(int)N] % u;
 
            // If remainder is 0
            if (r == 0)
            {
                 
                // Step 1.1
                if (v == 2 || v == 4 ||
                    v == 6 || v == 8)
                {
                    return 6;
                }
                 
                // Step 1.2
                else if (v == 5)
                {
                    return 5;
                }
 
                // Step 1.3
                else if (v == 1 || v == 3 ||
                         v == 7 || v == 9)
                {
                    return 1;
                }
            }
             
            // If r is non-zero,
            // then return (l^r) % 10
            else
            {
                return (power(v, r, temp) % 10);
            }
        }
         
        // Else return 0
        else
        {
            return 0;
        }
    }
 
    // Else return 1
    return 1;
}
 
// Driver Code
int main()
{
     
    // Given Numbers
    int X = 18;
    int N = 4;
 
    // Function Call
    long result = calculate(X, N);
 
    // Print the result
    cout << result;
}
 
// This code is contributed by spp____

Java




// Java program for the above approach
import java.util.*;
class TestClass {
 
    // Function to find a^b using
    // binary exponentiation
    public static long power(long a,
                             long b,
                             long c)
    {
        // Initialise result
        long result = 1;
 
        while (b > 0) {
 
            // If b is odd then,
            // multiply result by a
            if ((b & 1) = = 1) {
                result = (result * a) % c;
            }
 
            // b must be even now
            // Change b to b/2
            b / = 2;
 
            // Change a = a^2
            a = (a * a) % c;
        }
        return result;
    }
 
    // Function to find the last digit
    // of the given equation
    public static long calculate(long X,
                                 long N)
    {
        int a[] = new int[10];
 
        // To store cyclicity
        int cyclicity[] = new int[11];
 
        // Store cyclicity from 1 - 10
        cyclicity[1] = 1;
        cyclicity[2] = 4;
        cyclicity[3] = 4;
        cyclicity[4] = 2;
        cyclicity[5] = 1;
        cyclicity[6] = 1;
        cyclicity[7] = 4;
        cyclicity[8] = 4;
        cyclicity[9] = 2;
        cyclicity[10] = 1;
 
        // Observation 1
        if (N = = 0 || N = = 1) {
            return (X % 10);
        }
        // Observation 3
        else if (N = = 2
                       || N
                 = = 3
                     || N
                 = = 4) {
 
            long temp = (long)1e18;
 
            // To store the last digits
            // of factorial 2, 3, and 4
            a[2] = 2;
            a[3] = 6;
            a[4] = 4;
 
            // Find the last digit of X
            long v = X % 10;
 
            // Step 1
            if (v ! = 0) {
                int u = cyclicity[(int)v];
 
                // Divide a[N] by cyclicity
                // of v
                int r = a[(int)N] % u;
 
                // If remainder is 0
                if (r = = 0) {
 
                    // Step 1.1
                    if (v = = 2
                              || v
                        = = 4
                            || v
                        = = 6
                            || v
                        = = 8) {
                        return 6;
                    }
 
                    // Step 1.2
                    else if (v = = 5) {
                        return 5;
                    }
 
                    // Step 1.3
                    else if (
                        v = = 1
                              || v
                        = = 3
                            || v
                        = = 7
                            || v
                        = = 9) {
                        return 1;
                    }
                }
 
                // If r is non-zero,
                // then return (l^r) % 10
                else {
                    return (power(v,
                                  r,
                                  temp)
                            % 10);
                }
            }
 
            // Else return 0
            else {
                return 0;
            }
        }
 
        // Else return 1
        return 1;
    }
 
    // Driver's Code
    public static void main(String args[])
        throws Exception
    {
 
        // Given Numbers
        int X = 18;
        int N = 4;
 
        // Function Call
        long result = calculate(X, N);
 
        // Print the result
        System.out.println(result);
    }
}

Python3




# Python3 program for the above approach
 
# Function to find a^b using
# binary exponentiation
def power(a, b, c):
     
    # Initialise result
    result = 1
 
    while (b > 0):
         
        # If b is odd then,
        # multiply result by a
        if ((b & 1) == 1):
            result = (result * a) % c
         
        # b must be even now
        # Change b to b/2
        b //= 2
 
        # Change a = a^2
        a = (a * a) % c
         
    return result
 
# Function to find the last digit
# of the given equation
def calculate(X, N):
 
    a = 10 * [0]
 
    # To store cyclicity
    cyclicity = 11 * [0]
 
    # Store cyclicity from 1 - 10
    cyclicity[1] = 1
    cyclicity[2] = 4
    cyclicity[3] = 4
    cyclicity[4] = 2
    cyclicity[5] = 1
    cyclicity[6] = 1
    cyclicity[7] = 4
    cyclicity[8] = 4
    cyclicity[9] = 2
    cyclicity[10] = 1
 
    # Observation 1
    if (N == 0 or N == 1):
        return (X % 10)
     
    # Observation 3
    elif (N == 2 or N == 3 or N == 4):
        temp = 1e18;
         
        # To store the last digits
        # of factorial 2, 3, and 4
        a[2] = 2
        a[3] = 6
        a[4] = 4
 
        # Find the last digit of X
        v = X % 10
 
        # Step 1
        if (v != 0):
            u = cyclicity[v]
             
            # Divide a[N] by cyclicity
            # of v
            r = a[N] % u
 
            # If remainder is 0
            if (r == 0):
                 
                # Step 1.1
                if (v == 2 or v == 4 or
                    v == 6 or v == 8):
                    return 6
                 
                # Step 1.2
                elif (v == 5):
                    return 5
 
                # Step 1.3
                elif (v == 1 or v == 3 or
                      v == 7 or v == 9):
                    return 1
             
            # If r is non-zero,
            # then return (l^r) % 10
            else:
                return (power(v, r, temp) % 10)
         
        # Else return 0
        else:
            return 0
 
    # Else return 1
    return 1
 
# Driver Code
if __name__ == "__main__":
     
    # Given numbers
    X = 18
    N = 4
 
    # Function call
    result = calculate(X, N)
 
    # Print the result
    print(result)
 
# This code is contributed by chitranayal

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find a^b using
// binary exponentiation
static long power(long a, long b, long c)
{
     
    // Initialise result
    long result = 1;
 
    while (b > 0)
    {
         
        // If b is odd then,
        // multiply result by a
        if ((b & 1) == 1)
        {
            result = (result * a) % c;
        }
 
        // b must be even now
        // Change b to b/2
        b /= 2;
 
        // Change a = a^2
        a = (a * a) % c;
    }
    return result;
}
 
// Function to find the last digit
// of the given equation
public static long calculate(long X,
                             long N)
{
    int[] a = new int[10];
 
    // To store cyclicity
    int[] cyclicity = new int[11];
 
    // Store cyclicity from 1 - 10
    cyclicity[1] = 1;
    cyclicity[2] = 4;
    cyclicity[3] = 4;
    cyclicity[4] = 2;
    cyclicity[5] = 1;
    cyclicity[6] = 1;
    cyclicity[7] = 4;
    cyclicity[8] = 4;
    cyclicity[9] = 2;
    cyclicity[10] = 1;
 
    // Observation 1
    if (N == 0 || N == 1)
    {
        return (X % 10);
    }
    // Observation 3
    else if (N == 2 || N == 3 || N == 4)
    {
        long temp = (long)1e18;
 
        // To store the last digits
        // of factorial 2, 3, and 4
        a[2] = 2;
        a[3] = 6;
        a[4] = 4;
 
        // Find the last digit of X
        long v = X % 10;
 
        // Step 1
        if (v != 0)
        {
            int u = cyclicity[(int)v];
 
            // Divide a[N] by cyclicity
            // of v
            int r = a[(int)N] % u;
 
            // If remainder is 0
            if (r == 0)
            {
                 
                // Step 1.1
                if (v == 2 || v == 4 ||
                    v == 6 || v == 8)
                {
                    return 6;
                }
 
                // Step 1.2
                else if (v == 5)
                {
                    return 5;
                }
 
                // Step 1.3
                else if ( v == 1 || v == 3 ||
                          v == 7 || v == 9)
                {
                    return 1;
                }
            }
 
            // If r is non-zero,
            // then return (l^r) % 10
            else
            {
                return (power(v, r, temp) % 10);
            }
        }
 
        // Else return 0
        else
        {
            return 0;
        }
    }
 
    // Else return 1
    return 1;
}
 
// Driver code
static void Main()
{
     
    // Given numbers
    int X = 18;
    int N = 4;
 
    // Function call
    long result = calculate(X, N);
 
    // Print the result
    Console.Write(result);
}
}
 
// This code is contributed by divyeshrabadiya07

Javascript




<script>
      // JavaScript program for the above approach
 
      // Function to find a^b using
      // binary exponentiation
      function power(a, b, c) {
        // Initialise result
        var result = 1;
 
        while (b > 0) {
          // If b is odd then,
          // multiply result by a
          if ((b & 1) == 1) {
            result = (result * a) % c;
          }
 
          // b must be even now
          // Change b to b/2
          b /= 2;
 
          // Change a = a^2
          a = (a * a) % c;
        }
        return result;
      }
 
      // Function to find the last digit
      // of the given equation
      function calculate(X, N) {
        var a = [...Array(10)];
 
        // To store cyclicity
        var cyclicity = [...Array(11)];
 
        // Store cyclicity from 1 - 10
        cyclicity[1] = 1;
        cyclicity[2] = 4;
        cyclicity[3] = 4;
        cyclicity[4] = 2;
        cyclicity[5] = 1;
        cyclicity[6] = 1;
        cyclicity[7] = 4;
        cyclicity[8] = 4;
        cyclicity[9] = 2;
        cyclicity[10] = 1;
 
        // Observation 1
        if (N == 0 || N == 1) {
          return X % 10;
        }
 
        // Observation 3
        else if (N == 2 || N == 3 || N == 4) {
          var temp = 1e18;
 
          // To store the last digits
          // of factorial 2, 3, and 4
          a[2] = 2;
          a[3] = 6;
          a[4] = 4;
 
          // Find the last digit of X
          var v = X % 10;
 
          // Step 1
          if (v != 0) {
            var u = cyclicity[parseInt(v)];
 
            // Divide a[N] by cyclicity
            // of v
            var r = a[parseInt(N)] % u;
 
            // If remainder is 0
            if (r == 0)
            {
             
              // Step 1.1
              if (v == 2 || v == 4 || v == 6 || v == 8) {
                return 6;
              }
 
              // Step 1.2
              else if (v == 5) {
                return 5;
              }
 
              // Step 1.3
              else if (v == 1 || v == 3 || v == 7 || v == 9) {
                return 1;
              }
            }
 
            // If r is non-zero,
            // then return (l^r) % 10
            else {
              return power(v, r, temp) % 10;
            }
          }
 
          // Else return 0
          else {
            return 0;
          }
        }
 
        // Else return 1
        return 1;
      }
 
      // Driver Code
      // Given Numbers
      var X = 18;
      var N = 4;
 
      // Function Call
      var result = calculate(X, N);
       
      // Print the result
      document.write(result);
       
      // This code is contributed by rdtank.
    </script>
Output: 
6

 

Time Complexity: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :