Find the unit place digit of sum of N factorials

13 Jun, 2022
Given a number N, the task is to find units place digit of the first N natural numbers factorials, i.e. 1!+2!+3!+....N! where N<=10e18.Examples:  
Input: n = 2 
Output: 3
1! + 2! = 3
Last digit is 3

Input: n = 3
Output: 9
1! + 2! + 3! = 9
Last digit is 9
 
Naive Approach: In this approach, simply calculate factorial of each number and find sum of these. Finally get the unit place digit of sum. This will take a lot of time and unnecessary calculations.Efficient Approach: In this approach, only unit’s digit of N is to be calculated in the range [1, 5], because: 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 so on.As 5!=120, and factorial of number greater than 5 have trailing zeros. So, N>=5 doesn’t contribute in unit place while doing sum.Therefore:  
if (n < 5)
    ans = (1 ! + 2 ! +..+ n !) % 10;
else
    ans = (1 ! + 2 ! + 3 ! + 4 !) % 10;

Note : We know (1! + 2! + 3! + 4!) % 10 = 3
So we always return 3 when n is greater 
than 4.
Below is the implementation of the efficient approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find the unit place digit// of the first N natural numbers factorials#include <iostream>using namespace std; // Function to find the unit's place digitint get_unit_digit(long long int N){     // Let us write for cases when    // N is smaller than or equal    // to 4.    if (N == 0 || N == 1)       return 1;    else if (N == 2)       return 3;    else  if (N == 3)       return 9;     // We know following    // (1! + 2! + 3! + 4!) % 10 = 3    else // (N >= 4)       return 3;} // Driver codeint main(){    long long int N = 1;     for (N = 0; N <= 10; N++)        cout << "For N = " << N             << " : " << get_unit_digit(N)             << endl;     return 0;}
// Java  program to find the unit place digit// of the first N natural numbers factorials import java.io.*; class GFG {          // Function to find the unit's place digitstatic int get_unit_digit(  int N){     // Let us write for cases when    // N is smaller than or equal    // to 4.    if (N == 0 || N == 1)    return 1;    else if (N == 2)    return 3;    else if (N == 3)    return 9;     // We know following    // (1! + 2! + 3! + 4!) % 10 = 3    else // (N >= 4)    return 3;} // Driver code         public static void main (String[] args) {               int N = 1;     for (N = 0; N <= 10; N++)            System.out.println ("For N = " + N            + " : " + get_unit_digit(N));    }}//This Code is Contributed by ajit
# Python3 program to find the unit# place digit of the first N natural# numbers factorials # Function to find the unit's place digitdef get_unit_digit(N):         # Let us write for cases when    # N is smaller than or equal    # to 4.    if (N == 0 or N == 1):        return 1    elif (N == 2):        return 3    elif(N == 3):        return 9             # We know following    # (1! + 2! + 3! + 4!) % 10 = 3    else:        return 3 # Driver codeN = 1for N in range(11):    print("For N = ", N, ":",        get_unit_digit(N), sep = ' ') # This code is contributed# by sahilshelangia
// C# program to find the unit// place digit of the first N// natural numbers factorialsusing System; class GFG{     // Function to find the unit's// place digitstatic int get_unit_digit( int N){     // Let us write for cases when    // N is smaller than or equal    // to 4.    if (N == 0 || N == 1)    return 1;    else if (N == 2)    return 3;    else if (N == 3)    return 9;     // We know following    // (1! + 2! + 3! + 4!) % 10 = 3    else // (N >= 4)    return 3;} // Driver codestatic public void Main (){    int N = 1;     for (N = 0; N <= 10; N++)        Console.WriteLine ("For N = " + N +                " : " + get_unit_digit(N));}} // This Code is Contributed by akt_mit
<?php// PHP program to find the unit place digit// of the first N natural numbers factorials // Function to find the unit's place digitfunction get_unit_digit($N){     // Let us write for cases when    // N is smaller than or equal    // to 4.    if ($N == 0 || $N == 1)        return 1;    else if ($N == 2)        return 3;    else if ($N == 3)        return 9;     // We know following    // (1! + 2! + 3! + 4!) % 10 = 3    else // (N >= 4)        return 3;} // Driver code$N = 1; for ($N = 0; $N <= 10; $N++)    echo "For N = " . $N.         " : " . get_unit_digit($N) . "\n"; // This code is contributed// by ChitraNayal?>
<script> // Javascript program to find the unit place digit// of the first N natural numbers factorials // Function to find the unit's place digitfunction get_unit_digit(N){     // Let us write for cases when    // N is smaller than or equal    // to 4.    if (N == 0 || N == 1)      return 1;    else if (N == 2)      return 3;    else if (N == 3)      return 9;    // We know following    // (1! + 2! + 3! + 4!) % 10 = 3    else // (N >= 4)      return 3;} // Driver codevar N = 1;for (N = 0; N <= 10; N++)    document.write( "For N = " + N        + " : " + get_unit_digit(N)+"<br>")      </script>
For N = 0 : 1
For N = 1 : 1
For N = 2 : 3
For N = 3 : 9
For N = 4 : 3
For N = 5 : 3
For N = 6 : 3
For N = 7 : 3
For N = 8 : 3
For N = 9 : 3
For N = 10 : 3
 
sahilshelangia
jit_t
ukasp
rutvik_56
vinayedula
factorial
series-sum
Mathematical
Mathematical
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Minimum number of jumps to reach end
Program to find GCD or HCF of two numbers
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Program for Decimal to Binary Conversion
Modulo 10^9+7 (1000000007)