数字产品序列


22

这是彭博大学数学家保罗·洛米斯(Paul Loomis)发现的有趣序列。从关于此序列的页面中


f(n) = f(n-1) + (the product of the nonzero digits of f(n-1))
f(0) = x使用x以10为基数的正整数定义。

因此,从开始f(0)=1,您将获得以下序列
1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, ...

到目前为止,如此标准。当您将任何其他整数作为起点时,有趣的属性将起作用,最终该序列会收敛到上述x=1序列中的某个点。例如,从x=3收益率开始
3, 6, 12, 14, 18, 26, 38, 62, 74, 102, ...

这是另外一些序列,每个序列仅显示到到达为止102

5, 10, 11, 12, 14, 18, 26, 38, 62, 74, 102, ...
7, 14, 18, 26, 38, 62, 74, 102, ...
9, 18, 26, 38, 62, 74, 102, ...
13, 16, 22, 26, 38, 62, 74, 102, ...
15, 20, 22, 26, 38, 62, 74, 102, ...
17, 24, 32, 38, 62, 74, 102, ...
19, 28, 44, 60, 66, 102, ...

他推测并凭经验证明x=1,000,000,该属性(即所有输入数字收敛到相同的序列)成立。

挑战

给定正输入整数0 < x < 1,000,000,输出f(x)序列收敛到f(1)序列的数字。例如,对于x=5,这将是26,因为这是两个序列共有的第一个数字。

 x output
 1 1
 5 26
19 102
63 150056

规则

  • 如果适用,您可以假定输入/输出将适合您语言的本机Integer类型。
  • 输入和输出可以通过任何方便的方法给出。
  • 完整的程序或功能都是可以接受的。如果是函数,则可以返回输出而不是打印输出。
  • 禁止出现标准漏洞
  • 这是因此所有常用的高尔夫规则都适用,并且最短的代码(以字节为单位)获胜。

Answers:


5

JavaScript(ES6),81 67字节

@ l4m2节省了1个字节

f=(n,x=1)=>x<n?f(x,n):x>n?f(+[...n+''].reduce((p,i)=>p*i||p)+n,x):n

在线尝试!

已评论

f = (n,                   // n = current value for the 1st sequence, initialized to input
        x = 1) =>         // x = current value for the 2nd sequence, initialized to 1
  x < n ?                 // if x is less than n:
    f(x, n)               //   swap the sequences by doing a recursive call to f(x, n)
  :                       // else:
    x > n ?               //   if x is greater than n:
      f(                  //     do a recursive call with the next term of the 1st sequence:
        +[...n + '']      //       coerce n to a string and split it
        .reduce((p, i) => //       for each digit i in n:
          p * i || p      //         multiply p by i, or let p unchanged if i is zero
        ) + n,            //       end of reduce(); add n to the result
        x                 //       let x unchanged
      )                   //     end of recursive call
    :                     //   else:
      n                   //     return n

````f =(n,x = 1)=> x <n?f(x,n):x> n?f(+ [... n +'']。reduce((p,i)= > p * i || p)+ n,x):
n````– l4m2

4

果冻18 14字节

ḊḢDo1P+Ʋ;µQƑ¿Ḣ

输入是一个单例数组。

在线尝试!

怎么运行的

ḊḢDo1P+Ʋ;µQƑ¿Ḣ  Main link. Argument: [n]

            ¿   While...
          QƑ      all elements of the return value are unique...
         µ          execute the chain to the left.
Ḋ                     Dequeue; remove the first item.
 Ḣ                    Head; extract the first item.
                      This yields the second item of the return value if it has
                      at least two elements, 0 otherwise.
       Ʋ              Combine the links to the left into a chain.
  D                     Take the decimal digits of the second item.
   o1                   Perform logical OR with 1, replacing 0's with 1's.
     P                  Take the product.
      +                 Add the product with the second item.
        ;             Prepend the result to the previous return value.
             Ḣ  Head; extract the first item.



2

Python 2,78个字节

f=lambda a,b=1:a*(a==b)or f(*sorted([a+eval('*'.join(`a`.replace(*'01'))),b]))

在线尝试!


我当时正在使用lambda解决方案,但是在短循环中停留了几分钟,干得好!
死负鼠

2

外壳,13个字节

→UΞm¡S+ȯΠf±dΘ

将输入作为单例列表。

在线尝试!

说明

                 Implicit input, e.g 5
            Θ    Prepend a zero to get  [0,5]
   m             Map the following over [0,5]
    ¡              Iteratatively apply the following function, collecting the return values in a list
           d         Convert to a list of digits
         f±          keep only the truthy ones
       ȯΠ            then take the product
     S+              add that to the original number
                After this map, we have [[0,1,2,4,8,16,22,26,38,62...],[5,10,11,12,14,18,26,38,62,74...]]
  Ξ             Merge the sorted lists:  [0,1,2,4,5,8,10,11,12,14,16,18,22,26,26,38,38,62,62,74...]
 U              Take the longest unique prefix: [0,1,2,4,5,8,10,11,12,14,16,18,22,26]
→               Get the last element and implicitely output: 26

1

Python 3中126个是125字节

m=[1]
n=[int(input())]
while not{*m}&{*n}:
 for l in m,n:l+=l[-1]+eval('*'.join(str(l[-1]).replace(*'01'))),
print({*m}&{*n})

在线尝试!

将输入作为字符串




0

J,50个字节

隐式样式功能定义

[:{.@(e.~#])/[:(+[:*/@(*#])(#~10)&#:)^:(<453)"0,&1

如果将参数(例如63)粘贴到REPL表达式中,则可能为45,例如

{.(e.~#])/(+[:*/@(*#])(#~10)&#:)^:(<453)"0]1,63
  • ,&1 追加1以生成搜索序列以及参数序列
  • ^:(<453)"0 反复迭代直到按1的顺序达到1mio
  • + [: */@(*#]) (#~10)&#: 叉添加到钩上,它执行数字的乘积
  • (e.~ # ])/ 如果存在重复项,则使用重复项
  • {. 仅返回第一个共同值

在线尝试!


0

R110 86字节

o=c(1,1:9);o=o%o%o%o%o;o=c(o%o%o)
x=c(1,n);while((x=sort(x))<x[2])x[1]=(x+o[x+1])[1]
x

蒂奥

先前版本110:

f=function(x){if((x[1]=x[1]+(c((y=(y=c(1,1:9))%o%y%o%y)%o%y))[x[1]+1])==x[2]){x[1]}else{f(sort(x))}}
f(c(1,n))

蒂奥

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.