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
-
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– Peter Taylor2011年03月25日 23:40:22 +00:00Commented 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\$Kevin Brown-Silva– Kevin Brown-Silva2011年03月25日 23:49:16 +00:00Commented Mar 25, 2011 at 23:49
-
5\$\begingroup\$ Isn't 999999999999's persistence 2 instead of 3? \$\endgroup\$Eelvex– Eelvex2011年03月26日 02:13:19 +00:00Commented Mar 26, 2011 at 2:13
-
\$\begingroup\$ @Evelex, that was an incorrect last minute change I guess. Fixed. \$\endgroup\$Kevin Brown-Silva– Kevin Brown-Silva2011年03月26日 21:45:41 +00:00Commented 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\$Jesse Millikan– Jesse Millikan2011年03月27日 01:29:55 +00:00Commented Mar 27, 2011 at 1:29
49 Answers 49
Japt -mR, (削除) 21 (削除ここまで) (削除) 19 (削除ここまで) 15 bytes
+S+Uì Å£=ìxÃâ Ê
+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
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;}
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.
-
1\$\begingroup\$ 70 bytes. \$\endgroup\$Jonathan Frech– Jonathan Frech2018年04月15日 18:46:15 +00:00Commented Apr 15, 2018 at 18:46
-
\$\begingroup\$ Building on @JonathanFrech 67 bytes \$\endgroup\$ceilingcat– ceilingcat2019年12月08日 18:36:39 +00:00Commented Dec 8, 2019 at 18:36
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)))
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.
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
$imust be initialized.
(Also, it would print nothing instead of0for single digits without the initialization.)
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
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.
C (gcc), (削除) 87 (削除ここまで) 85 bytes
- Saved two bytes thanks to ceilingcat; pulling the
t=sassignment 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;}
-
\$\begingroup\$ @ceilingcat That is quite neat. \$\endgroup\$Jonathan Frech– Jonathan Frech2018年06月25日 21:33:30 +00:00Commented Jun 25, 2018 at 21:33
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)
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
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
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)}
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
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)}
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}
-
3\$\begingroup\$ Because the next newest answer is a full 6 years old, which i think is before TIO existed \$\endgroup\$fəˈnɛtɪk– fəˈnɛtɪk2018年04月15日 00:35:28 +00:00Commented Apr 15, 2018 at 0:35
AWK, 63 bytes
{for(;1<l=split(1,ドルa,X);i++){1ドル=0;for(j=0;j++<l;)1ドル+=a[j]}}0ドル=i
Raku (Perl 6) (rakudo), 54 bytes
{my ($t,$c)=$^a,0;while $t>9 {$t=[+] $t.comb;$c++};$c}
Explore related questions
See similar questions with these tags.