Categories
Coderbyte Data Structures and Algorithms Maths Python

[Coderbyte] – Multiplicative Persistence [Easy]

In this post, we are going to solve a challenge from Coderbyte called Multiplicative Persistence. The challenge is as follows:

Have the function MultiplicativePersistence(num) take the num parameter being passed which will always be a positive integer and return its multiplicative persistence which is the number of times you must multiply the digits in num until you reach a single digit. For example: if num is 39 then your program should return 3 because 3 * 9 = 27 then 2 * 7 = 14 and finally 1 * 4 = 4 and you stop at 4.

We need to return the number of times you must multiply the digits in a given number until you get to a single figure. The example in the above snippet illustrates the idea better. 

FIRST STEP – CONVERT THE NUMBER TO STRING

The first step in solving this problem is to convert the given number to String. The reason for doing so is to be able to loop over each digit in the number. 

def MultiplicativePersistence(num): 
  numStr = str(num)
  count = 0

We also initialise the variable count, which represents the number of times you must multiply the digits of the given number until you reach a single digit. This is the variable that will be returned. 

SECOND STEP – LOOP UNTIL CONDITION IS MET

The next step is to loop until the given number becomes a one-digit number. For example, if the number is “3” then the condition len(numStr) > 1 results to false and the count variable is returned (more on this later). Otherwise, the loop continues. 

def MultiplicativePersistence(num): 
  numStr = str(num)
  count = 0
  
  while len(numStr) > 1:
    total = 1

In this step, we also declare and initialise a variable called total. What is the purpose of this variable? This variable is assigned the result of multiplying each digit in the number; see the next step to understand. 

THIRD STEP – LOOP OVER THE GIVEN NUMBER

The code snippet illustrates the purpose of the variable called total. This variable is assigned the result of multiplying all digits in a number. For example, if the number is “393”, total is assigned the result of 3 x 9 x 3 (total = 81). The total is still a two-digit number. len(numStr) > 1 is satisfied, and it loops again over the number 81. Now the total is 8 (8 x 1 = 8), which means len(numStr) > 1 returns false and we are done. 

def MultiplicativePersistence(num): 
  numStr = str(num)
  count = 0
  
  while len(numStr) > 1:
    total = 1
    
    for digit in numStr:
      total *= int(digit)

What next? The next step is to increment the counter at each iteration of the while loop. Thus, here comes the…

FOURTH STEP – INCREMENT THE COUNTER AND RETURN IT

First of all, we re-assign the value of total to the variable numStr at each iteration of the while loop. The reason for doing so is to keep looping until we reach a single-digit number.

def MultiplicativePersistence(num): 
  numStr = str(num)
  count = 0
  
  while len(numStr) > 1:
    total = 1
    
    for digit in numStr:
      total *= int(digit)
      
    numStr = str(total)
    count += 1
      
  return count

We also increment the counter. Each time the while loop runs, it means that we need to perform one more multiplication. Once len(numStr) > 1 returns false, the program jumps straight to the return statement. And we are done!

FULL SOLUTION AND REPL.IT

If you have any comments or improvements leave a comment below! I am interested in seeing other perspectives and approaches.

The full solution can be seen and run on my Repl. Click me!

Advertisements

Leave a Reply