Created
March 2, 2018 13:03
-
-
Save KamuelaFranco/dd16cfe3169a7465cb9a54eb8c4cf91e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isFactorion = (number) => { | |
const stringifiedNumber = String(number); | |
let total = 0; | |
for (let stringDigit of stringifiedNumber) { | |
const numericalDigit = Number.parseInt(stringDigit); | |
total += singleDigitFactorial(numericalDigit); | |
} | |
if (number === total) { | |
return true; | |
} | |
return false; | |
}; | |
const singleDigitFactorial = (digit) => { | |
if (digit < 0 || digit > 9 || digit % 1 !== 0) { | |
throw new Error('Supplied digit must be an integer from 0-9'); | |
} | |
switch (digit) { | |
case 0: | |
case 1: return 1; | |
case 2: return 2; | |
case 3: return 6; | |
case 4: return 24; | |
case 5: return 120; | |
case 6: return 720; | |
case 7: return 5040; | |
case 8: return 40320; | |
case 9: return 362880; | |
default: throw new Error('Could not derive factorial from input'); | |
} | |
}; | |
for (let i = 0; i < 100000000; i += 1) { | |
if (isFactorion(i)) { | |
console.log(`${i} is a factorion.`); | |
} | |
} | |
// 1, 2, 145, 40585 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment