File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -29,10 +29,14 @@ const CheckKishnamurthyNumber = (number) => {
29
29
// create a variable to store the sum of all digits factorial.
30
30
let sumOfAllDigitFactorial = 0
31
31
// convert the number to string for convenience.
32
- String ( number ) . split ( '' ) . map ( digit => {
33
- // split one by one digit and calculate factorial and store to the variable.
34
- return ( sumOfAllDigitFactorial += factorial ( Number ( digit ) ) )
35
- } )
32
+ let newNumber = number
33
+ // Extract number digits using the remainder method.
34
+ while ( newNumber > 0 ) {
35
+ const lastDigit = newNumber % 10
36
+ // calculate each digit factorial.
37
+ sumOfAllDigitFactorial += factorial ( lastDigit )
38
+ newNumber = Math . floor ( newNumber / 10 )
39
+ }
36
40
// if the sumOftheFactorial is equal to the given number it means the number is a Krishnamurthy number.
37
41
return sumOfAllDigitFactorial === number
38
42
}
You can’t perform that action at this time.
0 commit comments