Skip to content

Instantly share code, notes, and snippets.

@chrisdothtml
Last active March 27, 2019 19:16
Show Gist options
  • Save chrisdothtml/7a21a8c6dcdcbf155f3d72f53a410ea2 to your computer and use it in GitHub Desktop.
Save chrisdothtml/7a21a8c6dcdcbf155f3d72f53a410ea2 to your computer and use it in GitHub Desktop.
/*
* 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