20
\$\begingroup\$

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
\$\endgroup\$
  • 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

39 Answers 39

1
2
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Japt -R, 21 19 bytes

Ë+S+(DsÊÉ ÆD=ìxÃâ Ê

Try it

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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].
f(n,p){char*t,s[99];for(p=0;sprintf(s,"%d",n),1[t=s];p++)for(n=0;*t;n+=*t++-48);n=p;}

Try it online!

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

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python 2, 62 bytes

f=lambda x,i=0:f(`sum(int(i)for i in x)`,i+1)if len(x)>1else i
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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)))
        )
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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)
| improve this answer | |
\$\endgroup\$
1
2

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.