数字の製品シーケンス


22

ブルームズバーグ大学の数学者、ポール・ルーミスによって発見された興味深いシーケンスは次のとおりです。このシーケンスに関する彼のページから:

定義する
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=3yields で始まる
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

ルール

  • 該当する場合、入力/出力が言語のネイティブ整数型に適合すると想定できます。
  • 入力と出力は、任意の便利な方法で指定できます。
  • 完全なプログラムまたは機能のいずれかが受け入れられます。関数の場合、出力する代わりに出力を返すことができます。
  • 標準的な抜け穴は禁止されています。
  • これはので、通常のゴルフルールがすべて適用され、最短のコード(バイト単位)が勝ちます。

回答:


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

、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




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

TIO

以前のバージョン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))

TIO

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.