Check if a given number divides the sum of the factorials of its digits
Last Updated :
09 Nov, 2023
Given an integer N, the task is to check whether N divides the sum of the factorials of its digits.
Examples:
Input: N = 19
Output: Yes
1! + 9! = 1 + 362880 = 362881, which is divisible by 19.
Input: N = 20
Output: No
0! + 2! = 1 + 4 = 5, which is not divisible by 20.
Approach: First, store the factorials of all the digits from 0 to 9 in an array. And, for the given number N check if it divides the sum of the factorials of its digits.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int n)
{
int fac[10];
fac[0] = fac[1] = 1;
for ( int i = 2; i < 10; i++)
fac[i] = fac[i - 1] * i;
int sum = 0;
int x = n;
while (x) {
sum += fac[x % 10];
x /= 10;
}
if (sum % n == 0)
return true ;
return false ;
}
int main()
{
int n = 19;
if (isPossible(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean isPossible( int n)
{
int fac[] = new int [ 10 ];
fac[ 0 ] = fac[ 1 ] = 1 ;
for ( int i = 2 ; i < 10 ; i++)
fac[i] = fac[i - 1 ] * i;
int sum = 0 ;
int x = n;
while (x != 0 )
{
sum += fac[x % 10 ];
x /= 10 ;
}
if (sum % n == 0 )
return true ;
return false ;
}
public static void main (String[] args)
{
int n = 19 ;
if (isPossible(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isPossible(n):
fac = [ 0 for i in range ( 10 )]
fac[ 0 ] = 1
fac[ 1 ] = 1
for i in range ( 2 , 10 , 1 ):
fac[i] = fac[i - 1 ] * i
sum = 0
x = n
while (x):
sum + = fac[x % 10 ]
x = int (x / 10 )
if ( sum % n = = 0 ):
return True
return False
if __name__ = = '__main__' :
n = 19
if (isPossible(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isPossible( int n)
{
int [] fac = new int [10];
fac[0] = fac[1] = 1;
for ( int i = 2; i < 10; i++)
fac[i] = fac[i - 1] * i;
int sum = 0;
int x = n;
while (x != 0)
{
sum += fac[x % 10];
x /= 10;
}
if (sum % n == 0)
return true ;
return false ;
}
public static void Main ()
{
int n = 19;
if (isPossible(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isPossible(n)
{
var fac = new Array(10);
fac[0] = fac[1] = 1;
for ( var i = 2; i < 10; i++)
fac[i] = fac[i - 1] * i;
var sum = 0;
var x = n;
while (x != 0)
{
sum += fac[x % 10];
x = parseInt(x / 10);
}
if (sum % n == 0)
return true ;
return false ;
}
var n = 19;
if (isPossible(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
PHP
<?php
function isPossible( $n )
{
$fac = array ();
$fac [0] = $fac [1] = 1;
for ( $i = 2; $i < 10; $i ++)
$fac [ $i ] = $fac [ $i - 1] * $i ;
$sum = 0;
$x = $n ;
while ( $x )
{
$sum += $fac [ $x % 10];
$x /= 10;
}
if ( $sum % $n == 0)
return true;
return false;
}
$n = 19;
if (isPossible( $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Approach 2:
Here’s another approach to check if a number n divides the sum of the factorials of its digits:
- Traverse the digits of the given number from right to left.
- Initialize a variable ‘sum’ to 0.
- For each digit, calculate its factorial and add it to ‘sum’.
- If at any point ‘sum’ becomes greater than or equal to ‘n’, check if it is divisible by ‘n’. If yes, return true.
- If all digits have been traversed and ‘sum’ is divisible by ‘n’, return true. Otherwise, return false.
Here’s the implementation of this approach in C++, Java and Python.
C++
#include <iostream>
using namespace std;
bool isPossible( int n)
{
int sum = 0;
int x = n;
while (x > 0)
{
int digit = x % 10;
int fact = 1;
for ( int i = 2; i <= digit; i++)
{
fact *= i;
}
sum += fact;
if (sum >= n && sum % n == 0)
{
return true ;
}
x /= 10;
}
return (sum % n == 0);
}
int main()
{
int n = 19;
if (isPossible(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static boolean isPossible( int n) {
int sum = 0 ;
int x = n;
while (x > 0 ) {
int digit = x % 10 ;
int fact = 1 ;
for ( int i = 2 ; i <= digit; i++) {
fact *= i;
}
sum += fact;
if (sum >= n && sum % n == 0 ) {
return true ;
}
x /= 10 ;
}
return (sum % n == 0 );
}
public static void main(String[] args) {
int n = 19 ;
if (isPossible(n)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
def is_possible(n):
sum = 0
x = n
while x > 0 :
digit = x % 10
fact = 1
for i in range ( 2 , digit + 1 ):
fact * = i
sum + = fact
if sum > = n and sum % n = = 0 :
return True
x / / = 10
return sum % n = = 0
def main():
n = 19
if is_possible(n):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
main()
|
C#
using System;
public class GFG
{
public static bool IsPossible( int n)
{
int sum = 0;
int x = n;
while (x > 0)
{
int digit = x % 10;
int fact = 1;
for ( int i = 2; i <= digit; i++)
{
fact *= i;
}
sum += fact;
if (sum >= n && sum % n == 0)
{
return true ;
}
x /= 10;
}
return (sum % n == 0);
}
public static void Main( string [] args)
{
int n = 19;
if (IsPossible(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isPossible(n) {
let sum = 0;
let x = n;
while (x > 0) {
let digit = x % 10;
let fact = 1;
for (let i = 2; i <= digit; i++) {
fact *= i;
}
sum += fact;
if (sum >= n && sum % n === 0) {
return true ;
}
x = Math.floor(x / 10);
}
return sum % n === 0;
}
let n = 19;
if (isPossible(n)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...