How Can I Complete My Python Code for the "Persistence Bugger"?

I'm not sure how to complete my code for the "persistence bugger" problem. The goal is to create a function called "persistence" to return the number of iterations until the digits of a number multiplied together equal a single-digit number.

For example: persistence(39) = 3, because 3*9 = 27, 2*7 = 14, and 1*4 = 4; hence, three iterations.

Below is my code so far, and I'm not sure where to go from here or if what I have is correct. Any help would be appreciated.

def persistence(num):
  num_str = str(num)
  total = 1
  for i in num_str:
    total = total * int(i)
  while len(str(total)) <> 1:
    total = total * total(i)
    #not sure what to do from here...
1

2 Answers

You'll notice that if you want to do what you did to 39 to 27, you'll be repeating code. This is a case where recursion can help (calling the function in itself):

def persistence(num):
    if num < 10:
        return 0 # Only one digit. Can't iterate over it
    num_str = str(num)
    total = 1
    for i in num_str:
        total = total * int(i)
    return 1 + persistence(total) # We do 1 + because we just did an iteration

Let's imagine the input was 39:

  1. 39 is not less than 10 so we go to the next stage.
  2. We do the same code you provided to get the total of multiplying the digits
  3. We now have a new number (27) allocated at total. We repeat the code above by calling the function again, but instead of passing 39, we pass through 27.
  4. 27 is not less than 10 so we go to the next stage
  5. We get the multiplication of digits
  6. We repeat until we get a total of 4 (1*4).
  7. We call persistence(4), but it returns 0 because 4 < 10. No iteration has been done for the number 4 (hence we return 0)
  8. At this point, recursion has stopped. Python now backtracks through all previous calls. It adds 0 + 1 + 1 + 1 to give 3.

Recursion is a little tricky to wrap your head around at first, but basically it's a function which calls itself, but it has "base cases" to stop the function running indefinitely. In this case, our base case is if the number is less than 10 (it has one digit). No iterations take place on this number.

1

TerryA's answer is really good, and when you want to keep applying the result of a function to the same function again (as in this example) recurssion is normally a very good idea. Just for the sake of completeness though, the solution can be implemented with a simple while loop, not too dissimilar to what you have attempted:

def persistance(num):
    counter=0
    while num>9:
        counter+=1
        num_str=str(num)
        total=1
        for i in num_str:
            total=total* int(i)
        num=total
    print (counter)

The counter keeps track of how many times the loop runs, which gives you your final answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest