Find last two digits of sum of N factorials

Given a number N, the task is to find unit and tens places digit of the first N natural numbers factorials, i.e last last two digit of 1!+2!+3!+….N! where N<=10e18.

Examples:

Input : n = 2 
Output :3
1! + 2! = 3
Last two digit  is 3

Input :4
Output :33
1!+2!+3!+4!=33
Last two digit is 33

Naive Approach:In this approach, simply calculate factorial of each number and find sum of these. Finally get the unit and tens place digit of sum. This will take a lot of time and unnecessary calculations.

Efficient Approach: In this approach, only unit’s and ten’s digit of N is to be calculated in the range [1, 10], because:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8!=40320
9!=362880
10!=3628800
so on.

As 10 != 3628800, and factorial of number greater than 10 have two trailing zeros. So, N>=10 doesn’t contribute in unit and tens place while doing sum.

Therefore,

if (n < 10)
ans = (1 ! + 2 ! +..+ n !) % 100;
else
ans = (1 ! + 2 ! + 3 ! + 4 !+ 5 ! + 6 ! + 7 ! + 8 ! + 9 ! + 10 !) % 100;

Note : We know (1! + 2! + 3! + 4!+…+10!) % 100 = 13
So we always return 3 when n is greater
than 4.

Below is the implementation of above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the unit place digit
// of the first N natural numbers factorials
#include <iostream>
using namespace std;
#define ll long int
// Function to find the unit's and ten's place digit
int get_last_two_digit(long long int N)
{
  
    // Let us write for cases when
    // N is smaller than or equal
    // to 10.
    if (N <= 10) {
        ll ans = 0, fac = 1;
        for (int i = 1; i <= N; i++) {
            fac = fac * i;
            ans += fac;
        }
        return ans % 100;
    }
  
    // We know following
    // (1! + 2! + 3! + 4!...+10!) % 100 = 13
    else // (N >= 10)
        return 13;
}
  
// Driver code
int main()
{
    long long int N = 1;
    for (N = 1; N <= 10; N++)
        cout << "For N = " << N
             << " : " << get_last_two_digit(N)
             << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java program to find the unit place digit
//of the first N natural numbers factorials
public class AAA {
  
    //Function to find the unit's and ten's place digit
    static int get_last_two_digit(long N)
    {
  
     // Let us write for cases when
     // N is smaller than or equal
     // to 10.
     if (N <= 10) {
         long ans = 0, fac = 1;
         for (int i = 1; i <= N; i++) {
             fac = fac * i;
             ans += fac;
         }
         return (int)ans % 100;
     }
  
     // We know following
     // (1! + 2! + 3! + 4!...+10!) % 100 = 13
     else // (N >= 10)
         return 13;
    }
  
    //Driver code
    public static void main(String[] args) {
          
        long N = 1;
         for (N = 1; N <= 10; N++)
             System.out.println( "For N = " + N
                  + " : " + get_last_two_digit(N));
    }
  
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the unit 
# place digit of the first N natural
# numbers factorials
  
# Function to find the unit's
# and ten's place digit
def get_last_two_digit(N):
      
    # Let us write for cases when
    # N is smaller than or equal
    # to 10
    if N <= 10:
        ans = 0
        fac = 1
        for i in range(1, N + 1):
            fac = fac * i
            ans += fac
        ans = ans % 100
        return ans
          
    # We know following
    # (1! + 2! + 3! + 4!...+10!) % 100 = 13
    # // (N >= 10)
    else:
        return 13
  
# Driver Code
N = 1
for N in range(1, 11):
    print("For N = ", N, ": "
           get_last_two_digit(N), sep = ' ')
  
# This code is contributed 
# by sahilshelangia

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the unit 
// place digit of the first N
// natural numbers factorials
using System;
  
class GFG
{
  
// Function to find the unit's 
// and ten's place digit
static int get_last_two_digit(long N)
{
  
// Let us write for cases when
// N is smaller than or equal
// to 10.
if (N <= 10)
{
    long ans = 0, fac = 1;
    for (int i = 1; i <= N; i++) 
    {
        fac = fac * i;
        ans += fac;
    }
    return (int)ans % 100;
}
  
// We know following
// (1! + 2! + 3! + 4!...+10!) % 100 = 13
else // (N >= 10)
    return 13;
}
  
// Driver code
public static void Main() 
{
    long N = 1;
    for (N = 1; N <= 10; N++)
        Console.WriteLine( "For N = " + N + 
            " : " + get_last_two_digit(N));
}
}
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the unit place digit
// of the first N natural numbers factorials
  
// Function to find the unit's
// and ten's place digit
function get_last_two_digit($N)
{
  
    // Let us write for cases when
    // N is smaller than or equal
    // to 10.
    if ($N <= 10) 
    {
        $ans = 0; $fac = 1;
        for ($i = 1; $i <= $N; $i++)
        {
            $fac = $fac * $i;
            $ans += $fac;
        }
        return $ans % 100;
    }
  
    // We know following
    // (1! + 2! + 3! + 4!...+10!) % 100 = 13
    else // (N >= 10)
        return 13;
}
  
// Driver code
$N = 1;
for ($N = 1; $N <= 10; $N++)
    echo "For N = " . $N . " : "
          get_last_two_digit($N) . "\n";
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)

chevron_right


Output:

For N = 1 : 1
For N = 2 : 3
For N = 3 : 9
For N = 4 : 33
For N = 5 : 53
For N = 6 : 73
For N = 7 : 13
For N = 8 : 33
For N = 9 : 13
For N = 10 : 13


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.