Open In App

JavaScript Program to Find the Sum of Digits in a Factorial

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Let’s first define the problem before proceeding to a solution. Any positive number less than or equal to n is the product of all positive integers, and the factorial of a non-negative integer n is denoted by n!. For example, 5!, sometimes known as “five factorial,” is equivalent to 5 × 4 × 3 × 2 × 1 = 120. Adding up the digits of a given number’s factorial is our objective.

Examples:

Input: 10
Output: 27

Input: 100
Output: 648

These are the following appraoches:

Iterative Approach

This method calculates factorial using a loop, then iteratively extracts digits from the result, summing them up, offering efficiency in computation and space.

Example: The factorial() method uses an iterative approach to determine the factorial, but the sumOfDigitsInFactorial() function extracts each digit from the factorial using modulus division and divides the result by 10. Until no more digits can be removed, this process is repeated.

Javascript




function factorial(n) {
    let result = 1;
    // Calculate the factorial
    // using a for loop from 2 to n.
    for (let i = 2; i <= n; i++) {
        result *= i;
    }
    // Return the factorial result.
    return result;
}
 
function sumOfDigitsInFactorial(num) {
    let fact = factorial(num);
    let sum = 0;
    while (fact !== 0) {
        sum += fact % 10;
        fact = Math.floor(fact / 10);
    }
    return sum;
}
 
const num = 5;
const sumOfDigits = sumOfDigitsInFactorial(num);
console.log("Sum of digits in factorial:",
    sumOfDigits);


Output

Sum of digits in factorial: 3

Recursive Approach

The recursive method iteratively calculates the factorial of the given integer by using a function. After taking each digit from the factorial and adding up all of them, it provides a clear and beautiful solution. For big inputs, however, too much recursion may result in stack overflow issues.

Example: The `factorial` function calculates the factorial of a number recursively. The `sumOfDigitsInFactorial` function computes the sum of digits in the factorial of a given number by iterating through its digits using a while loop. The example calculates and prints the sum of digits in the factorial of 5.

Javascript




function factorial(n) {
    if (n === 0 || n === 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}
 
function sumOfDigitsInFactorial(num) {
    let fact = factorial(num);
    let sum = 0;
    while (fact !== 0) {
        sum += fact % 10;
        fact = Math.floor(fact / 10);
    }
    return sum;
}
 
const num = 5;
const sumOfDigits = sumOfDigitsInFactorial(num);
console.log("Sum of digits in factorial:",
    sumOfDigits);


Output

Sum of digits in factorial: 3

Array Conversion Approach

This method involves calculating the factorial sequentially and using built-in JavaScript functions to convert the resultant number into an array of digits. The `reduce} method is then used to calculate the sum of these digits, which is straightforward but may result in cost from array formation.

Example: This example demonstrates how to calculate the factorial of a number and then convert it into an array of digits to compute their sum.

Javascript




function factorial(n) {
    let result = 1;
    for (let i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}
 
function sumOfDigitsInFactorial(num) {
    const fact = factorial(num);
    const digits = Array.from(String(fact),
        Number);
    return digits.reduce((sum, digit) =>
        sum + digit, 0);
}
 
const number = 5;
const sum = sumOfDigitsInFactorial(number);
console.log("Sum of digits in factorial:", sum);


Output

Sum of digits in factorial: 3


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads