The shortest code to pass all possibilities wins.

In mathematics, the persistence of a number measures how many times a certain operation must be applied to its digits until some certain fixed condition is reached. You can determine the additive persistence of a positive integer by adding the digits of the integer and repeating. You would keep adding the digits of the sum until a single digit number is found. The number of repetitions it took to reach that single digit number is the additive persistence of that number.

Example using 84523:

84523
8 + 4 + 5 + 2 + 3 = 22
2 + 2 = 4

It took two repetitions to find the single digit number.
So the additive persistence of 84523 is 2.


You will be given a sequence of positive integers that you have to calculate the additive persistence of. Each line will contain a different integer to process. Input may be in any standard I/O methods.

For each integer, you must output the integer, followed by a single space, followed by its additive persistence. Each integer processed must be on its own line.

## Test Cases

Input Output

99999999999 3
10 1
8 0
19999999999999999999999 4
6234 2
74621 2
39 2
2677889 3
0 0

• Your test cases include some values which are over 2^64, and your spec says that the program only has to handle values up to 2^32. Might be worth clearing that up. – Peter Taylor Mar 25 '11 at 23:40
• @Peter Taylor, forgot to remove those limits. If a program can handle the input I have provided, it shouldn't have an issue with limits. – Kevin Brown Mar 25 '11 at 23:49
• Isn't 999999999999's persistence 2 instead of 3? – Eelvex Mar 26 '11 at 2:13
• @Evelex, that was an incorrect last minute change I guess. Fixed. – Kevin Brown Mar 26 '11 at 21:45
• Several answers here aren't doing output on stdout but rather use J's "interactive" output by returning results after taking command line input. (This includes 2 other J answers and, I'm guessing, the K answer.) Is this considered legit? Because I can shed 18-ish characters if so. – Jesse Millikan Mar 27 '11 at 1:29

# Bash+coreutils, 83 bytes

[ $1 -le 9 ]&&exit$2
let x=$2+1 for z in fold -w1<<<$1
do let y+=$z done a$y $x  Try it online! Should be saved to a script called a and placed in the system's PATH, as it calls itself recursively. Takes input from command line, like a 1999. Returns by exit code. TIO has some limitations on what you can do with a script, so there's some boilerplate code to make this run in the header. Prints an error to stderr for input larger than bash integers can handle, but since the actual computation is done with strings, it still gives the right result anyway. # Japt -R, 21 19 bytes Ë+S+(DsÊÉ ÆD=ìxÃâ Ê  Try it # C (gcc), 87 85 bytes • Saved two bytes thanks to ceilingcat; pulling the t=s assignment forward and using (t=s) ~ *((t=s)+1) ~ *(1+(t=s)) ~ 1[t=s]. f(n,p){char*t,s;for(p=0;sprintf(s,"%d",n),1[t=s];p++)for(n=0;*t;n+=*t++-48);n=p;}  Try it online! • @ceilingcat That is quite neat. – Jonathan Frech Jun 25 '18 at 21:33 # Julia, 92 (29) bytes f(n)=n>9&&(f∘sum∘digits)(n)+1  Edit: With correct printing it's 92: f(n)=n>9&&(f∘sum∘digits)(n)+1 while(n=parse(Int128,readline()))≢π println("$n ",f(n)%Int)end


# Python 2, 62 bytes

f=lambda x,i=0:f(sum(int(i)for i in x),i+1)if len(x)>1else i


# Python 3, 76 bytes

def f(n,i=0,p=0):p=p or n;f(sum(map(int,str(n))),i+1,p)if n>9else print(p,i)


Try it online!

Without I/O restrictions:

# Python 3, 54 bytes

f=lambda n,i=0:f(sum(map(int,str(n))),i+1)if n>9else i


Try it online!

# Python 3, 50 bytes

f=lambda n:0if n<10else-~f(eval('+'.join(str(n))))


Recursive function. Takes an integer, returns an integer.

Explanation:

# create a lambda function which takes one argument, and assign it to f
f=lambda n:\
# if n is 0-9, it's persistence 0
0if n<10\
# otherwise, add one to the running total and recursively call f
# ( ~ will invert the returned number, equivalent to -n-1)
# (negating that gives you -(-n-1) = n+1)
else-~f(
# convert integet to string, join each character with a '+'
# gives a string like '1+2+3+4+...+n'
# evaluate the string as an expression, giving a new integer
# pass that integer back into f
eval('+'.join(str(n)))
)


# Pyth, 17 bytes

FG.Qs[Gdtl.usjNTG


Try it online!

If I can instead of values on different lines, take a list of numbers (in the format [9999999999, 10, 8, etc.]), then -1 byte by replacing .Q with Q

F                  # For
G.Q               #   G in the complete input (split by newlines)
s[             #     join the list as string, create list from the following values:
Gd           #       G, " " (for output formatting),
tl         #       decrement length of
.u    G  #          List of all intermediate results until there's a returning result, with starting value G
s      #            reduce on + (add all list elements) the list:
j     #              convert to integer as list:
N    #                The current value (implicit input to .u)
T   #                in base 10 (T=10)


func a(_ s:S,_ i:I)->(S,I){if s.count<=1{return(s,i)};return a(S(s.compactMap{I(S($0))}.reduce(0,+)),i+1)}  Try it online! # Jelly, 12 bytes żDS$Ƭ€Ẉ’ƊK€Y


Try it online!

Takes input as a list of integers. +3 bytes to input as a multiline string.

+3 bytes (or +6) for restrictive I/O formats, yay

## How it works

żDS$Ƭ€Ẉ’ƊK€Y - Main link. Takes a list l on the left Ɗ - To l:$         -   Group the previous 2 links into a monad f(n):
D           -     Digits
S          -     Sum
Ƭ€       -   Over each n in l, repeatedly apply f(n) until a fixed point,
yielding [n, f(n), f(f(n)), ...]
Ẉ      -   Get the length of each loop
’     -   Decrement each
ż            - Zip the loop lengths with l
K€  - Join each by spaces
Y - Join by newlines