Find the unit place digit of sum of N factorials
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
Brute Force Approach:
In this approach, we are calculating the factorial of each number and then adding it to the sum. To prevent overflow, we are taking the modulo with 10 after every multiplication and addition operation. Finally, we return the unit place digit of the sum.
Below is implementation of the above approach:
C++
#include <iostream>
using namespace std;
int get_unit_digit( long long int N) {
if (N == 0) {
return 1;
}
long long int sum = 0;
for ( long long int i = 1; i <= N; i++) {
long long int fact = 1;
for ( long long int j = 1; j <= i; j++) {
fact *= j;
fact %= 10;
}
sum += fact;
sum %= 10;
}
return sum;
}
int main() {
long long int N = 1;
for (N = 0; N <= 10; N++)
cout << "For N = " << N
<< " : " << get_unit_digit(N)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int get_unit_digit( long N)
{
if (N == 0 ) {
return 1 ;
}
long sum = 0 ;
for ( long i = 1 ; i <= N; i++) {
long fact = 1 ;
for ( long j = 1 ; j <= i; j++) {
fact *= j;
fact %= 10 ;
}
sum += fact;
sum %= 10 ;
}
return ( int )sum;
}
public static void main(String[] args)
{
long N;
for (N = 0 ; N <= 10 ; N++) {
System.out.println( "For N = " + N + " : "
+ get_unit_digit(N));
}
}
}
|
Python3
def get_unit_digit(N):
if N = = 0 :
return 1
sum = 0
for i in range ( 1 , N + 1 ):
fact = 1
for j in range ( 1 , i + 1 ):
fact * = j
fact % = 10
sum + = fact
sum % = 10
return sum
if __name__ = = "__main__" :
for N in range ( 11 ):
print (f "For N = {N} : {get_unit_digit(N)}" )
|
C#
using System;
class Program {
static int GetUnitDigit( long N)
{
if (N == 0) {
return 1;
}
long sum = 0;
for ( long i = 1; i <= N; i++) {
long fact = 1;
for ( long j = 1; j <= i; j++) {
fact *= j;
fact %= 10;
}
sum += fact;
sum %= 10;
}
return ( int )sum;
}
static void Main( string [] args)
{
long N = 1;
for (N = 0; N <= 10; N++)
Console.WriteLine( "For N = " + N + " : "
+ GetUnitDigit(N));
}
}
|
Javascript
function get_unit_digit(N) {
if (N === 0) {
return 1;
}
let sum = 0;
for (let i = 1; i <= N; i++) {
let fact = 1;
for (let j = 1; j <= i; j++) {
fact *= j;
fact %= 10;
}
sum += fact;
sum %= 10;
}
return sum;
}
let N = 1;
for (N = 0; N <= 10; N++)
console.log( "For N = " + N + " : " + get_unit_digit(N));
|
Output
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
Time Complexity: O(N ^ 2)
Auxiliary Space: O(1)
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.
Algorithm :
Step 1: Start
Step 2: Start a static function called get_unit_digit which take integer value as its parameter called N.
Step 3: Now inside the above function set some conditions:
a. If N is 0 or 1, return 1.
b. If N is 2, return 3.
c. If N is 3, return 9.
d. If N is greater than or equal to 4, return 3.
Step 4: End
Below is the implementation of the efficient approach:
C++
#include <iostream>
using namespace std;
int get_unit_digit( long long int N)
{
if (N == 0 || N == 1)
return 1;
else if (N == 2)
return 3;
else if (N == 3)
return 9;
else
return 3;
}
int main()
{
long long int N = 1;
for (N = 0; N <= 10; N++)
cout << "For N = " << N
<< " : " << get_unit_digit(N)
<< endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int get_unit_digit( int N)
{
if (N == 0 || N == 1 )
return 1 ;
else if (N == 2 )
return 3 ;
else if (N == 3 )
return 9 ;
else
return 3 ;
}
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));
}
}
|
Python3
def get_unit_digit(N):
if (N = = 0 or N = = 1 ):
return 1
elif (N = = 2 ):
return 3
elif (N = = 3 ):
return 9
else :
return 3
N = 1
for N in range ( 11 ):
print ( "For N = " , N, ":" ,
get_unit_digit(N), sep = ' ' )
|
C#
using System;
class GFG
{
static int get_unit_digit( int N)
{
if (N == 0 || N == 1)
return 1;
else if (N == 2)
return 3;
else if (N == 3)
return 9;
else
return 3;
}
static public void Main ()
{
int N = 1;
for (N = 0; N <= 10; N++)
Console.WriteLine ( "For N = " + N +
" : " + get_unit_digit(N));
}
}
|
Javascript
<script>
function get_unit_digit(N)
{
if (N == 0 || N == 1)
return 1;
else if (N == 2)
return 3;
else if (N == 3)
return 9;
else
return 3;
}
var N = 1;
for (N = 0; N <= 10; N++)
document.write( "For N = " + N
+ " : " + get_unit_digit(N)+ "<br>" )
</script>
|
PHP
<?php
function get_unit_digit( $N )
{
if ( $N == 0 || $N == 1)
return 1;
else if ( $N == 2)
return 3;
else if ( $N == 3)
return 9;
else
return 3;
}
$N = 1;
for ( $N = 0; $N <= 10; $N ++)
echo "For N = " . $N .
" : " . get_unit_digit( $N ) . "\n" ;
?>
|
Output
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
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
22 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...