Last active
March 27, 2019 19:16
-
-
Save chrisdothtml/7a21a8c6dcdcbf155f3d72f53a410ea2 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
/* | |
* Calculate the persistence of a number; | |
* based on Numberphile video: | |
* https://www.youtube.com/watch?v=Wim9WJeDTHQ | |
*/ | |
function calculatePersistence (num) { | |
let result = 1 | |
while ((num = multiplyItems(getDigits(num))) > 0) | |
result++ | |
return result | |
} | |
function getDigits (num) { | |
const result = [] | |
while (num > 0) { | |
const rem = num % 10 | |
result.push(rem) | |
num -= rem | |
num /= 10 | |
} | |
return result | |
} | |
function multiplyItems (items) { | |
return items.reduce((result, item) => | |
(result * item), | |
1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment