Saturday, January 4, 2025
Google search engine
HomeData Modelling & AICheck if a given number divides the sum of the factorials of...

Check if a given number divides the sum of the factorials of its digits

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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that returns true if n divides
// the sum of the factorials of its digits
bool isPossible(int n)
{
 
    // To store factorials of digits
    int fac[10];
    fac[0] = fac[1] = 1;
 
    for (int i = 2; i < 10; i++)
        fac[i] = fac[i - 1] * i;
 
    // To store sum of the factorials
    // of the digits
    int sum = 0;
 
    // Store copy of the given number
    int x = n;
 
    // Store sum of the factorials
    // of the digits
    while (x) {
        sum += fac[x % 10];
        x /= 10;
    }
 
    // If it is divisible
    if (sum % n == 0)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int n = 19;
 
    if (isPossible(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
     
    // Function that returns true if n divides
    // the sum of the factorials of its digits
    static boolean isPossible(int n)
    {
     
        // To store factorials of digits
        int fac[] = new int[10];
        fac[0] = fac[1] = 1;
     
        for (int i = 2; i < 10; i++)
            fac[i] = fac[i - 1] * i;
     
        // To store sum of the factorials
        // of the digits
        int sum = 0;
     
        // Store copy of the given number
        int x = n;
     
        // Store sum of the factorials
        // of the digits
        while (x != 0)
        {
            sum += fac[x % 10];
            x /= 10;
        }
     
        // If it is divisible
        if (sum % n == 0)
            return true;
     
        return false;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 19;
     
        if (isPossible(n))
            System.out.println("Yes");
        else
            System.out.println("No");
     
    }
}
 
// This code is contributed by Ryuga


Python3




# Python 3 implementation of the approach
 
# Function that returns true if n divides
# the sum of the factorials of its digits
def isPossible(n):
     
    # To store factorials of digits
    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
 
    # To store sum of the factorials
    # of the digits
    sum = 0
 
    # Store copy of the given number
    x = n
 
    # Store sum of the factorials
    # of the digits
    while (x):
        sum += fac[x % 10]
        x = int(x / 10)
 
    # If it is divisible
    if (sum % n == 0):
        return True
 
    return False
 
# Driver code
if __name__ == '__main__':
    n = 19
 
    if (isPossible(n)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by
# Surendra_Gangwar


C#




// C# implementation of the approach
using System;
class GFG
{
     
    // Function that returns true if n divides
    // the sum of the factorials of its digits
    static bool isPossible(int n)
    {
     
        // To store factorials of digits
        int[] fac = new int[10];
        fac[0] = fac[1] = 1;
     
        for (int i = 2; i < 10; i++)
            fac[i] = fac[i - 1] * i;
     
        // To store sum of the factorials
        // of the digits
        int sum = 0;
     
        // Store copy of the given number
        int x = n;
     
        // Store sum of the factorials
        // of the digits
        while (x != 0)
        {
            sum += fac[x % 10];
            x /= 10;
        }
     
        // If it is divisible
        if (sum % n == 0)
            return true;
     
        return false;
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 19;
     
        if (isPossible(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by Code_Mech.


Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function that returns true if n divides
// the sum of the factorials of its digits
function isPossible(n)
{
     
    // To store factorials of digits
    var fac = new Array(10);
    fac[0] = fac[1] = 1;
 
    for(var i = 2; i < 10; i++)
        fac[i] = fac[i - 1] * i;
 
    // To store sum of the factorials
    // of the digits
    var sum = 0;
 
    // Store copy of the given number
    var x = n;
 
    // Store sum of the factorials
    // of the digits
    while (x != 0)
    {
        sum += fac[x % 10];
        x = parseInt(x / 10);
    }
 
    // If it is divisible
    if (sum % n == 0)
        return true;
 
    return false;
}
 
// Driver Code
var n = 19;
     
if (isPossible(n))
    document.write("Yes");
else
    document.write("No");
   
// This code is contributed by Khushboogoyal499
 
</script>


PHP




<?php
// PHP implementation of the approach
 
// Function that returns true if n divides
// the sum of the factorials of its digits
function isPossible($n)
{
 
    // To store factorials of digits
    $fac = array();
    $fac[0] = $fac[1] = 1;
 
    for ($i = 2; $i < 10; $i++)
        $fac[$i] = $fac[$i - 1] * $i;
 
    // To store sum of the factorials
    // of the digits
    $sum = 0;
 
    // Store copy of the given number
    $x = $n;
 
    // Store sum of the factorials
    // of the digits
    while ($x)
    {
        $sum += $fac[$x % 10];
        $x /= 10;
    }
 
    // If it is divisible
    if ($sum % $n == 0)
        return true;
 
    return false;
}
 
// Driver code
$n = 19;
 
if (isPossible($n))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by Akanksha Rai
?>


Output

Yes







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;
 
// Function that returns true if n divides
// the sum of the factorials of its digits
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);
}
 
// Driver code
int main()
{
    int n = 19;
 
    if (isPossible(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java




import java.util.*;
 
public class GFG {
    // Function that returns true if n divides the sum of
   // the factorials of its digits
    public static boolean isPossible(int n) {
      // Initialize sum to 0
        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;  // Calculate the factorial of the digit
            }
            sum += fact;  // Add the factorial to the sum
            if (sum >= n && sum % n == 0) {
                return true// If sum is divisible by n, return true
            }
            x /= 10// Move to the next digit
        }
        return (sum % n == 0);  // Return true if sum is divisible by n, otherwise false
    }
 
    // Driver code
    public static void main(String[] args) {
        int n = 19;
 
        if (isPossible(n)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}


Python3




# Function that returns true if n divides
# the sum of the factorials of its digits
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
 
# Driver code
def main():
    n = 19
 
    if is_possible(n):
        print("Yes")
    else:
        print("No")
 
if __name__ == '__main__':
    main()


C#




using System;
 
public class GFG
{
    // Function that returns true if n divides
    // the sum of the factorials of its digits
    public static bool IsPossible(int n)
    {
        int sum = 0;
        int x = n;
 
        while (x > 0)
        {
            int digit = x % 10;
            int fact = 1;
 
            // Calculate factorial of the current digit
            for (int i = 2; i <= digit; i++)
            {
                fact *= i;
            }
 
            // Add the factorial to the sum
            sum += fact;
 
            // Check if the sum is greater than or equal to n and divisible by n
            if (sum >= n && sum % n == 0)
            {
                return true;
            }
 
            // Move to the next digit
            x /= 10;
        }
 
        // Check if the sum is divisible by n
        return (sum % n == 0);
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int n = 19;
 
        if (IsPossible(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}


Javascript




<script>
// JavaScript code of the above approach
 
// Function that returns true if n divides
// the sum of the factorials of its digits
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;
}
 
// Driver code
let n = 19;
 
if (isPossible(n)) {
    document.write("Yes");
}
else {
    document.write("No");
}
 
// This code is contributed by Susobhan Akhuli
</script>


Output

Yes







Time Complexity: O(logn)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments