30
\$\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
mbomb007
23.6k7 gold badges66 silver badges143 bronze badges
asked Mar 25, 2011 at 23:18
\$\endgroup\$
10
  • 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\$ Commented Mar 25, 2011 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\$ Commented Mar 25, 2011 at 23:49
  • 5
    \$\begingroup\$ Isn't 999999999999's persistence 2 instead of 3? \$\endgroup\$ Commented Mar 26, 2011 at 2:13
  • \$\begingroup\$ @Evelex, that was an incorrect last minute change I guess. Fixed. \$\endgroup\$ Commented Mar 26, 2011 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\$ Commented Mar 27, 2011 at 1:29

49 Answers 49

1
2
1
\$\begingroup\$

Japt -mR, (削除) 21 (削除ここまで) (削除) 19 (削除ここまで) 15 bytes

+S+Uì Å£=ìxÃâ Ê

Try it

+S+Uì Å£=ìxÃâ Ê :Implicit map of each U in input array
+S+ :Append a space and
 Uì : Convert U to digit array
 Å : Slice off the first element
 £ : Map
 = : Reassign to U
 ìx : Convert to digit array and reduce by addition
 Ã : End map
 â : Deduplicate
 Ê : Length
 :Implicit output joined with newlines
answered May 14, 2018 at 14:19
\$\endgroup\$
0
0
\$\begingroup\$

Perl 5, 65 bytes

$b=0;$q=s/\n//r;$_=eval s/./+$&/gr while y///c>1&&++$b;say"$q $b"

Try it online!

answered Apr 15, 2018 at 1:38
\$\endgroup\$
0
\$\begingroup\$

Java (OpenJDK 8), 79 bytes

a->{int d=0;while(a/10>0){int c=0;d++;while(a>0){c+=a%10;a/=10;}a=c;}return d;}

Try it online!

There's probable potential to golf it further, but I'll look into that in the future, but for now, I'm pretty happy with this little result.

answered Apr 15, 2018 at 2:28
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 70 bytes. \$\endgroup\$ Commented Apr 15, 2018 at 18:46
  • \$\begingroup\$ Building on @JonathanFrech 67 bytes \$\endgroup\$ Commented Dec 8, 2019 at 18:36
0
\$\begingroup\$

Python 3, 82 bytes

while 1:f=lambda n:n//10and 1+f(sum(map(int,str(n))));i=input();print(i,f(int(i)))
answered Apr 15, 2018 at 14:57
\$\endgroup\$
0
\$\begingroup\$

Japt, 28 bytes

Ë+S+(@D=X©A<D©ì x aD D<AÃa÷
Ë // Map over the inputs and return each, followed by
 +S+ // a space, followed by the number's persistence.
 D= ©ì x // To find it, fold the number up
 X©A<D aD // if we can (handles unfoldable cases),
 (@ D<AÃa // until it can't be folded up any further.
 ÷ // Then, join everything up with newlines.

Try it online!

answered Apr 25, 2018 at 17:53
\$\endgroup\$
0
\$\begingroup\$

PHP, 72+1 bytes

+1 for -R flag.

for($i=0,$a=$argn;$a>9;$i++)$a=array_sum(str_split($a));echo"$argn $i
";

Run as pipe with -R.

  • running PHP as pipe will execute the code once for every input line
  • but it does not unset variables inbetween; so $i must be initialized.
    (Also, it would print nothing instead of 0 for single digits without the initialization.)
answered May 4, 2018 at 15:52
\$\endgroup\$
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.

answered May 7, 2018 at 3:30
\$\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!

answered Apr 15, 2018 at 0:52
\$\endgroup\$
1
  • \$\begingroup\$ @ceilingcat That is quite neat. \$\endgroup\$ Commented Jun 25, 2018 at 21:33
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
answered Aug 6, 2019 at 15:15
\$\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!

answered Aug 6, 2019 at 9:43
\$\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)))
 )
answered Aug 6, 2019 at 17:01
\$\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)
answered Aug 12, 2019 at 10:18
\$\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!

caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Aug 6, 2019 at 14:06
\$\endgroup\$
0
\$\begingroup\$

><>, 36 bytes

0{:a(7$.0}\~1+!
~n
1:,a}+{%a:/?(1:-%

Try it online!

answered Dec 19, 2022 at 17:25
\$\endgroup\$
0
\$\begingroup\$

Nibbles, 7 bytes (14 nibbles)

:,ドル>>`.$/`@~$+
 # (implicitly map over input):
:$ # prepend input onto
 , # length 
 >> # (without first item) of
 `. # iterate while unique
 $ # starting with input number:
 `@~$ # convert to base-10 digits
 / # and fold over this list
 + # adding its elements

enter image description here

answered Dec 20, 2022 at 9:25
\$\endgroup\$
0
\$\begingroup\$

Go, 77 bytes

func f(n int)int{s:=0
if n<10{return 0}
for;n>0;n/=10{s+=n%10}
return 1+f(s)}

Attempt This Online!

answered Nov 28, 2023 at 15:53
\$\endgroup\$
0
\$\begingroup\$

Tcl, 94 bytes

proc P {v n\ 0} {set s $v
while \$s>9 {set s [expr [join [split $s ""] +]]
incr n}
list $v $n}

Try it online!

answered Apr 15, 2018 at 0:32
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Because the next newest answer is a full 6 years old, which i think is before TIO existed \$\endgroup\$ Commented Apr 15, 2018 at 0:35
0
\$\begingroup\$

AWK, 63 bytes

{for(;1<l=split(1,ドルa,X);i++){1ドル=0;for(j=0;j++<l;)1ドル+=a[j]}}0ドル=i

Attempt This Online!

answered Dec 12, 2024 at 15:19
\$\endgroup\$
0
\$\begingroup\$

Raku (Perl 6) (rakudo), 54 bytes

{my ($t,$c)=$^a,0;while $t>9 {$t=[+] $t.comb;$c++};$c}

Attempt This Online!

answered Apr 28 at 19:28
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.