Check whether N is a Factorion or not | Javascript implementation of the approach ; Function that returns true if n is a Factorion ; fact [ i ] will store i ! ; A copy of the given integer ; To store the sum of factorials of the digits of n ; Get the last digit ; Add the factorial of the current digit to the sum ; Remove the last digit ; Driver code
const MAX = 10 ; function isFactorion ( n ) { let fact = new Array ( MAX ) ; fact [ 0 ] = 1 ; for ( let i = 1 ; i < MAX ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; let org = n ; let sum = 0 ; while ( n > 0 ) { let d = n % 10 ; sum += fact [ d ] ; n = parseInt ( n / 10 ) ; } if ( sum == org ) return true ; return false ; } let n = 40585 ; if ( isFactorion ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;