(相关)
给定整数n > 1,
1)构造n, n-1, n-2, ... 3, 2, 1数的取值范围,计算和
2)取该数字的个别数字并计算乘积。
3)取该数字的个别数字并计算和。
4)重复步骤2和3,直到达到一位数为止。这个数字就是结果。
序列的前二十项如下:
3, 6, 0, 5, 2, 7, 9, 2, 7, 9, 1, 9, 0, 0, 9, 6, 7, 0, 0, 6注:此序列不在OEIS中。
n output
1234 9
3005 3
5007 5
9854 8
75849 8
100000 0发布于 2018-05-04 13:59:48
发布于 2018-05-04 13:18:24
f=(n,k=n*++n/2)=>k>9?f(!n,eval([...k+''].join('*+'[+!n]))):kf = ( // f = recursive function taking:
n, // n = original input
k = n * ++n / 2 // k = current value, initialized to sum(i=1..n)(i)
) => //
k > 9 ? // if k has more than 1 digit:
f( // recursive call to f() with:
!n, // a logical NOT applied to n
eval( // the result of the expression built by:
[...k + ''] // turning k into a list of digits
.join('*+'[+!n]) // joining with '*' on even iterations or '+' on odd ones
) // end of eval()
) // end of recursive call
: // else:
k // stop recursion and return the last value只适用于n< 236172的非递归版本。(它涵盖所请求的范围,但不限定为有效的泛型算法。)
n=>[...'*+*+'].map(o=>n=eval([...n+''].join(o)),n*=++n/2)|nhttps://codegolf.stackexchange.com/questions/164156
复制相似问题