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:

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
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Peter Taylor Mar 25 '11 at 23:40
  • \$\begingroup\$ @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. \$\endgroup\$ – Kevin Brown Mar 25 '11 at 23:49
  • 5
    \$\begingroup\$ Isn't 999999999999's persistence 2 instead of 3? \$\endgroup\$ – Eelvex Mar 26 '11 at 2:13
  • \$\begingroup\$ @Evelex, that was an incorrect last minute change I guess. Fixed. \$\endgroup\$ – Kevin Brown Mar 26 '11 at 21:45
  • \$\begingroup\$ 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. \$\endgroup\$ – Jesse Millikan Mar 27 '11 at 1:29

40 Answers 40


Bash+coreutils, 83 bytes

[ $1 -le 9 ]&&exit $2
let x=$2+1
for z in `fold -w1<<<$1`
do let y+=$z
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)[1] ~ *((t=s)+1) ~ *(1+(t=s)) ~ 1[t=s].

Try it online!

  • \$\begingroup\$ @ceilingcat That is quite neat. \$\endgroup\$ – Jonathan Frech Jun 25 '18 at 21:33

Julia, 92 (29) bytes


Edit: With correct printing it's 92:

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.


# 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)
            # 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

Pyth, 17 bytes


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)

Swift 5, 106 bytes

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


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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.