Tuesday, 12 January 2016

Kata Day 47: Persistent Bugger


Problem Description: Write a function, persistence, that takes in a positive parameter num and returns its 
multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a 
single digit.

For example:

 persistence(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
                       // and 4 has only one digit

 persistence(999) === 4 // because 9*9*9 = 729, 7*2*9 = 126,
                        // 1*2*6 = 12, and finally 1*2 = 2

 persistence(4) === 0 // because 4 is already a one-digit number

Level:6kyu

Link To Kata:  Persistent Bugger

This Kata asks us to return the count for which we can multiply the digits of a number until we reach a single digit. A fairly easy kata.

We start solving this kata by converting the given int to a string and then we loop through the string. We initialize a variable prod equal to 1 and repetitively multiply every digit of the input number with the prod. After looping through every digit of the input digit in string format we change the value of input integer to that of prod. We keep repeating this process until we reach a point where we get a single digit value of input number. Every time we repeat through this process we keep an account of counter and keep increasing it by 1.

In the end we return the count as the solution to this kata.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 47

Please mention your suggestions or your doubts in the comment below.

Happy coding. :)

No comments:

Post a Comment