# 加性持久性

20

``````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.
``````

## 测试用例

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

1

@Peter Taylor，忘记删除这些限制。如果程序可以处理我提供的输入，则它应该没有限制问题。

5
999999999999的持久性不是2而是3？
Eelvex

@Evelex，我猜这是不正确的最后一刻更改。固定。

Answers:

6

## K-29个字符

```````0:{5:x,-1+#(+/10_vs)\x}'.:'0:"file"
``````
• 35-> 31：取消外部功能。
• 31-> 29：删除括号。

1
`-1+#`=>`#1_`
streetster

4

Python 84个字符

``````while 1:
m=n=int(raw_input());c=0
while n>9:c+=1;n=sum(map(int,str(n)))
print m,c
``````

Quixotic

@Debanjan谢谢。已更正。
fR0DDY 2011年

4

## Haskell，100个字符

``````p[d]=0
p d=1+(p.show.sum\$map((-48+).fromEnum)d)
f n=n++' ':shows(p n)"\n"
main=interact\$(f=<<).lines
``````

'18 -4-25

4

## Python（93字节）

``````f=lambda n,c:n>9and f(sum(map(int,str(n))),c+1)or c
while 1:n=int(raw_input());print n,f(n,0)
``````

st0le 2011年

@ st0le：感谢:-)

`input()`不是`int(raw_input())`....
st0le 2011年

@ st0le：尝试与修改此输入：`06234`
Quixotic

4

# 外壳，10 15字节

+5个字节用于可怕的I / O要求

``````m(wΓ·,LU¡oΣdr)¶
``````

### 说明

``````m(₁r)¶  -- expects newline-separated inputs: "x₁␤x₂␤…␤xₙ"
¶  -- split on newlines: ["x₁","x₂",…,"xₙ"]
m(  )   -- map over each string
( r)   -- | read integer: [x₁,x₂,…,xₙ]
(₁ )   -- | apply the function described below
``````

``````wΓ·,LU¡(Σd)  -- input is an integer, eg: 1234
¡(  )  -- iterate the following forever and collect results in list:
( d)  -- | digits: [1,2,3,4]
(Σ )  -- | sum: 10
-- : [1234,10,1,1,1,…
U       -- keep longest prefix until repetition: [1234,10,1]
Γ           -- pattern match (x = first element (1234), xs = tail ([10,1])) with:
· L        -- | length of xs: 2
,         -- | construct tuple: (1234,2)
w            -- join with space: "1234 2"
``````

3

### bash，105个字符

``````while read x
do
for((i=0,z=x;x>9;i++))do
for((y=0;x>0;y+=x%10,x/=10))do :
done
x=\$y
done
echo \$z \$i
done
``````

3

## 哈斯克尔-114

``````s t n|n>9=s(t+1)\$sum\$map(read.(:[]))\$show n|1>0=show t
f n=show n++" "++s 0n++"\n"
main=interact\$(f.read=<<).lines
``````

ბიმო

3

## Ruby，85个字符

``````puts \$<.map{|n|v=n.chop!;c=0;[c+=1,n="#{n.sum-n.size*48}"] while n[1];[v,c]*' '}*"\n"
``````

3

## Golfscript，40个字符

``````n%{.:\${;\${48-}%{+}*`:\$,}%.,1>\1?+' '\n}%
``````

3

## J-45个字符

``````(,' ',[:":@<:@#+/&.:("."0)^:a:)&><;._2(1!:1)3
``````

Eelvex

1
u ^：n字典条目具有用法信息，但有点密集。^：a：就像其他任何次幂调用一样，但是它收集结果并在连续调用的参数相同（收敛）时结束。
isawdrones 2011年

1
`a:`通过J参考卡 [PDF]中的`^:a:`技巧发现了@Eelvex FWIW
JB

@JB：那`^:a:`是我所知道的唯一参考文献：D
Eelvex 2011年

@Eelvex哦。那时我经历了相反的经历。我发现了字典中的功能，并一开始将其用作`^:(<'')`（可能是Kaprekar的）某种变体，直到在卡中找到它，并`a:`为此进行了了解。
JB

3

## c-519

（如果您相信我的框架，则为137 ...）

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char*(*O)(char*);
char*b(char*s){long long int v=0,i,l=0;char*t=0;l=strlen(s);t=malloc(l+2);
for(i=0;i<l;i++)v+=s[i]-'0';snprintf(t,l+2,"%lld",v);return t;}
int a(char**s,O o){int r;char*n;n=o(*s);r=!strcmp(*s,n);free(*s);
*s=n;return r;}
int main(int c, char**v){size_t l, m=0;char *d,*n=0;O o=b;FILE*f=stdin;
while(((l=getline(&n,&m,f))>1)&&!feof(f)){int i=0;n=strsep(&n,"\n");
d=strdup(n);while(!a(&n,o))i++;printf("%s %d\n",d,i);free(d);free(n);n=0;m=0;}}``````

``````/* persistence.c
*
* A general framework for finding the "persistence" of input strings
* on opperations.
*
* Persistence is defined as the number of times we must apply
*
*    value_n+1 <-- Opperation(value_n)
*
* before we first reach a fixed point.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../getline.h"

/* A function pointer type for operations */
typedef char*(op_func)(char*);
typedef op_func* op_ptr;
/* Op functions must
* + Accept the signature above
* + return a point to a newly allocated buffer containing the updated str
*/

char* addop(char*s){
int i,l=0;
long long int v=0;
char *t=NULL;
/* protect against bad input */
if (NULL==s) return s;
/* allocate the new buffer */
l = strlen(s);
t = malloc(l+2);
if (NULL==t) return t;
/* walk the characters of the original adding as we go */
for (i=0; i<l; i++) v += s[i]-'0';
//fprintf(stderr,"   '%s' (%d) yields %lld\n",s,l,v);
snprintf(t,l+2,"%lld",v);
//fprintf(stderr,"   %lld is converted to '%s'\n",v,t);
return t;
}

/* Apply op(str), return true if the argument is a fixed point fo
* falsse otherwise,
*/
int apply(char**str, op_ptr op){
int r;
char*nstr;
/* protect against bad input */
if ( NULL==op ) exit(1);
if ( NULL==*str ) exit(4);
/* apply */
nstr = op(*str);
/* test for bad output */
if ( NULL==nstr ) exit(2);
r = !strcmp(*str,nstr);
/* free previous buffer, and reasign the new one */
free(*str);
*str = nstr;
return r;
}

int main(int argc, char**argv){
size_t len, llen=0;
char *c,*line=NULL;
op_ptr op=addop;
FILE *f=stdin;
if (argc > 1) f = fopen(argv[1],"r");
while( ((len=getline(&line,&llen,f))>1) && line!=NULL && !feof(f) ){
int i=0;
line=strsep(&line,"\n"); // Strip the ending newline
/* keep a copy for later */
c = strdup(line);
/* count necessary applications */
while(!apply(&line,op)) i++;
printf("%s %d\n",c,i);
/* memory management */
free(c);
free(line);
line=NULL;
llen=0;
}
}``````

2

## J，74个字符

``````i=:<;._2(1!:1)3
i&((],' ',":@(0 i.~9<[:".([:":[:+/"."0)^:(i.9)))@>@{~)i.#i
``````

### 编辑

• （86→83） 的一些帽`[:`到ATS`@`
• （83→79） 不需要的括号
• （79→75） 更改`0".``".`简化形式
• （75→74） 更好的切割

### 例如

``````i=:<;._2(1!:1)3
74621
39
2677889
0
i&((],' ',":@(0 i.~9<[:".([:":[:+/"."0)^:(i.9)))@>@{~)i.#i
74621 2
39 2
2677889 3
0 0
``````

Jesse Millikan

@杰西：我没看错。你能写一个例子吗？
Eelvex

1

``````f=->(n){n.sum-n.size*48}
\$<.each{|l|i=0;i+=1 while(i+=1;n=f[(n||l.chop!).to_s])>10
puts "#{l} #{i}"}
``````

Alex Bartlow 2011年

1

PARI / GP 101字符

``````s(n)=r=0;while(n>0,r+=n%10;n\=10);r
f(n)=c=0;while(n>9,c++;n=s(n));c
while(n=input(),print(n," ",f(n)))
``````

Eelvex

@Eelvex，完成了。:)
st0le 2011年

1

Javascript-95

``````i=prompt();while(i>9){i=''+i;t=0;for(j=0;j<i.length;j++)t+=parseInt(i.charAt(j));i=t;}alert(t);
``````

1

1

# J，78

``````f=:[:+/"."0&":
r=:>:@\$:@f`0:@.(=f)
(4(1!:2)~LF,~[:":@([,r)".@,&'x');._2(1!:1)3
``````

1

## Perl-77个字符

``````sub'_{split//,shift;@_<2?0:1+_(eval join'+',@_)}chop,print\$_,\$",(_\$_),\$/for<>
``````

1

# JavaScript，57 47字节

-10个字节，感谢@ l4m2！

``f=(s,c=0)=>s>9?f(eval([...s+""].join`+`),++c):c``

`f=(s,c=0)=>s>9?f([...s+""].reduce((x,y)=>x*1+y*1),++c):c`
l4m2

`f=(s,c=0)=>s>9?f([...s+""].reduce((x,y)=>x- -y),++c):c`
l4m2 '18年

1
`f=(s,c=0)=>s>9?f(eval([...s+""].join`+`)),++c):c`
l4m2 '18年

@ l4m2谢谢！`s>9``eval`是伟大的想法。我认为您那里有一个多余的括号，使它总共节省了10个字节：-)

Shaggy

1

# 05AB1E，13 个字节

``````ε.µΔSO¼}¾}<ø»
``````

``````ε     # Map each integer in the (implicit) input to:
.µ   #  Reset the counter variable to 0
Δ    #  Loop until the integer no longer changes:
S   #   Convert it to a list of digits
O  #   And take the sum of those
¼   #   Increase the counter variable by 1
}¾   #  After the inner loop: Push the counter variable
}<    # After the map: decrease each value by 1
ø   # Zip/transpose it with the (implicit) input to create a paired list
»  # Join each pair by a space, and then each string by newlines
# (after which the result is output implicitly)``````

1

# MathGolf，11个字节

``````hÅ_Σ]▀£(k ?
``````

## 说明（使用`n = 6234`）

``````h             push length of number without popping (6234, 4)
Å            loop 4 times using next 2 operators
_           duplicate TOS
Σ          get the digit sum
]         wrap stack in array
this gives the array [6234, 15, 6, 6, 6]
▀        unique elements of string/list ([6234, 15, 6])
£       length of array/string with pop (3)
(      decrement (2)
k ?   push input, space, and rotate top 3 elements to produce output (6234 2)
``````

1

# K（ngn / k），16字节

``````{x,#1_(+/10\)\x}
``````

``````{x,#1_(+/10\)\x} / the solution
{              } / lambda taking implicit x
(     )\x  / iterate until convergence
10\     / split into base-10 (123 => 1 2 3)
+/        / sum
1_           / drop first result (iterate returns input as first result)
#             / count length of result
x,              / prepend x (original input)
``````

1

# Stax，8 11 字节

``````ªwæMε∞ö?îm⌐
``````

@Khuldraeseth +3个字节（第一个答案没有符合要求的输出）

1

Khuldraeseth na'Barya

0

### scala 173：

``````def s(n:BigInt):BigInt=if(n<=9)n else n%10+s(n/10)
def d(n:BigInt):Int=if(n<10)0 else 1+d(s(n))
Iterator.continually(readInt).takeWhile(_>0).foreach(i=>println(i+" "+d(i)))
``````

0

# Perl 5、65个字节

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

0

# Java（OpenJDK 8），79字节

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

ceilingcat

0

# Python 3，82字节

``````while 1:f=lambda n:n//10and 1+f(sum(map(int,str(n))));i=input();print(i,f(int(i)))
``````

0

# Tcl，95字节

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

3

fəˈnɛtɪk

0

# Japt，28个字节

``````Ë+S+(@D=X©A<D©ì x ªD 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     ªD         // if we can (handles unfoldable cases),
(@               D<AÃa   // until it can't be folded up any further.
Ã· // Then, join everything up with newlines.``````

0

# PHP，72 + 1字节

+1表示`-R`标志。

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

• 将PHP作为管道运行将对每个输入行执行一次代码
• 但是它不会在它们之间设置变量；因此`\$i`必须初始化。
（此外，如果`0`不进行初始化，它将不打印任何内容，而只打印单个数字。）

0

# Bash + coreutils，83个字节

``````[ \$1 -le 9 ]&&exit \$2
let x=\$2+1
for z in `fold -w1<<<\$1`
do let y+=\$z
done
a \$y \$x``````

TIO对脚本的操作有一些限制，因此有一些样板代码可以在标头中运行。

`stderr`对于大于bash整数可以处理的输入，将错误输出到，但是由于实际计算是使用字符串完成的，因此无论如何仍会给出正确的结果。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.