Additive Persistence

In mathematics, the persistence of a number is the number of times one must apply a given operation to an integer before reaching a fixed point at which the operation no longer alters the number.

For additive persistence, we sum up each digit of a given number to form a new number. We repeat the process until we have a single digit number.

Example 1: The additive persistence of 2718 is 2:

2718 ---> 2 + 7 + 1 + 8 = 18

18 ---> 1 + 8 = 9

Since 9 is just a single digit, we stop here. We have repeated the addition operation 2 times, so the additive persistence is 2.

Example 2: The additive persistence of 35786 is 3:

35786 ---> 3 + 5 + 7 + 8 + 6 = 29

29 ---> 2 + 9 = 11

11 ---> 1 + 1 = 2

We repeated the addition operation 3 times, so the additive persistence is 3.

Write a function to find the additive persistence of a number

Have fun!

def additivePersistence(number)
  # input your magic here
  # feel free to modify the input/parameters to suit your program
  # hint: recursion can be useful here
end

results matching ""

    No results matching ""