Skip to content

Instantly share code, notes, and snippets.

@nkgokul
Last active August 24, 2021 02:00
Show Gist options
  • Save nkgokul/4a4f4a63ba8cd74b5097256bb9694346 to your computer and use it in GitHub Desktop.
Save nkgokul/4a4f4a63ba8cd74b5097256bb9694346 to your computer and use it in GitHub Desktop.
Persistence of a number
function digit_product(number) {
let digit_product = 1;
while (number) {
digit_product *= number % 10;
number = Math.floor(number / 10);
}
return digit_product;
}
function persistence(number) {
if (number < 10) return 0;
return persistence(digit_product(number)) + 1;
}
find = 1;
while (persistence(find) < 5) {
find++;
}
console.log(find);
test_input_array = [10, 25, 39, 77];
test_input_array.map(number => console.log(number + " " + persistence(number)));
// console.log(persistence(999))
// console.log(find + " " + persistence(find));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment