MU番号を計算する


19

最初の2つのMU番号は2と3です。他のすべてのMU番号は、まだ現れていない最小の番号であり、2つの以前の別個のMU番号の積として正確に1つの方法で表現できます。

ここに最初の10があります

2, 3, 6, 12, 18, 24, 48, 54, 96, 162

仕事

正の数が与えられたら、n番目のMU 数を計算して出力します。

これは競争なので、ソースコードをできるだけ小さくすることを目指してください。

OEIS A007335


1
0インデックスまたは1インデックス?
ハイパーニュートリノ

1
@HyperNeutrinoどちらでも結構です。
小麦ウィザード

2
これらがMU番号と呼ばれる理由は何ですか?(ワイルド推測:乗算ユニーク?)

回答:


5

Pyth、22 21バイト

@u+Gfq2l@GcLTGheGQhB2

オンラインでお試しください。 テストスイート。

0インデックス付き。

説明

@u+Gfq2l@GcLTGheGQhB2Q    Implicitly append Q and read+eval input to it.
                  hB2     Take the list [2, 2 + 1].
 u               Q        Put the list in G and apply this Q times:
               eG           Get last number in G.
              h             Add one.
    f                       Starting from that, find the first T such that:
          cLTG                Divide T by each of the numbers in G.
        @G                    Find the quotients that are also in G.
       l                      Get the number of such quotients.
     q2                       Check that it equals 2.
  +G                        Append that T to G.
@                    Q    Get the Q'th number in G.

@最後の行の記号の位置がずれています。2文字の変更なので、提案された編集を行うことはできません。
user2357112は、モニカをサポートします

@ user2357112修正済み。
PurkkaKoodari

4

Haskell、80 77バイト

l#(a:b)|[x]<-[a|i<-l,j<-l,i<j,i*j==a]=a:(a:l)#b|1<2=l#b
((2:3:[2,3]#[4..])!!)

オンラインでお試しください!

使い方

2:3:             -- start the list with 2 and 3 and append a call to # with
    [2,3]        -- the list so far and
         #[4..]  -- list of candidate elements

l # (a:b)        -- l -> list so far, a -> next candidate element, b -> rest c.el.
  | [x]<-[...]   -- if the list [...] is a singleton list
    =a:(a:l#b) -- the result is a followed by a recursive call with l extended
                    by a and b
  | 1<2=l#b      -- if it's not a singleton list, drop a and retry with b

                 -- the [...] list is
 [ i<-l,j<-l,    -- loop i through l and j through l and whenever   
       i<j,      -- i<j and
       i*j==a]   -- i*j==a
  a|             -- add a to the list              

3

ゼリー、22 バイト

ŒcP€ḟ⁸ṢŒgLÞḢḢṭ
2,3Ç¡ị@

1インデックス付きの単項リンク。

オンラインでお試しください!

どうやって?

ŒcP€ḟ⁸ṢŒgLÞḢḢṭ - Link 1, add the next number: list, a  e.g. [2,3,6,12,18,24]
Œc             - unordered pairs                            [[2,3],[2,6],[2,12],[2,18],[2,24],[3,6],[3,12],[3,18],[3,24],[6,12],[6,18],[6,24],[12,18],[12,24],[18,24]]
  P€           - product of €ach                            [6,12,24,36,48,18,36,54,72,72,108,144,216,288,432]
     ⁸         - chain's left argument, a                   [2,3,6,12,18,24]
    ḟ          - filter discard                             [36,48,36,54,72,72,108,144,216,288,432]
      Ṣ        - sort                                       [36,36,48,54,72,72,108,144,216,288,432]
       Œg      - group runs of equal elements               [[36,36],[48],[54],[72,72],[108],[144],[216],[288],[432]]
          Þ    - sort by:
         L     -   length                                   [[48],[54],[108],[144],[216],[288],[432],[36,36],[72,72]]
           Ḣ   - head                                       [48]
            Ḣ  - head                                       48
             ṭ - tack to a                                  [2,3,6,12,18,24,48]

2,3Ç¡ị@ - Link: number, i                              e.g. 7
2,3     - literal [2,3]                                     [2,3]
    ¡   - repeat i times:
   Ç    -   call last link (1) as a monad                   [2,3,6,12,18,24,48,54,96]
     ị@ - index into with swapped @rguments (with i)        48

3

R127 118 111 108 105 100 98 90バイト

ジュゼッペのおかげで8バイト。

r=3:2;for(i in 1:scan())r=c(min((g=(r%o%r)[i:-1<i])[colSums(g%o%g==g*g)+g%in%r<3]),r);r[3]

オンラインでお試しください!


それはそれが実現するために永遠に連れて行ってくれた<よりも低い優先順位がある+、私はいったいに何を見つけ出すことができなかったので+g%in%r<3やっていた、と私はそれをやっていた一方で、あなたは私がお勧めするつもりだった二つの部分を下にgolfed ... +1を
ジュゼッペ

@ジュゼッペ私は今日Rを学び始めたばかりです...まともなRゴルファーに会えてうれしいです。
リーキー修道女

私はあなたに同じことを言うつもりだった.............-
ジュゼッペ

もう1つn=scan()、関数定義の代わりにstdinから読み取ることができます。それはあなたを100未満にするでしょう
ジュゼッペ

入力に失敗します0
リフト

2

CJam(32バイト)

4,{_2m*{~>},::*1$-$e`$0=|}qi*-2=

0インデックス付きのオンラインデモ

私は確かに一つの例外を除いてスペックの些細な翻訳を超えて行われるべき多くがありますないよ:のリストを開始することにより、[0 1 2 3](代わりに[2, 3])私が行うことができるということで初期化し、他の2ですぐに1つのバイトを保存する0=|(だけ追加します新しい要素その周波数があるので1、リストに既にある)が、すべてのためにあるため、虚偽の要素を導入していないxリストに0*xし、1*xリストに既にあります。



1

Mathematica、154バイト

oeisリンクにあるコードの簡単な変更

(s={2,3};Do[n=Select[Split@Sort@Flatten@Table[s[[j]]s[[k]],{j,Length@s},{k,j+1,Length@s}],#[[1]]>s[[-1]]&&Length@#==1&][[1,1]];AppendTo[s,n],{#}];s[[#]])&

1

PHP、130バイト

0インデックス付き

for($r=[2,3];!$r[$argn];$r[]=$l=min($m)/2){$m=[];foreach($r as$x)foreach($r as$y)($p=$x*$y)<=$l|$y==$x?:$m[$p]+=$p;}echo$r[$argn];

オンラインでお試しください!

拡大

for($r=[2,3];!$r[$argn]; #set the first to items and loop till search item exists
$r[]=$l=min($m)/2){ # add the half of the minimum of found values to the result array
  $m=[]; # start with empty array
  foreach($r as$x) # loop through result array
    foreach($r as$y) # loop through result array
      ($p=$x*$y)<=$l|$y==$x? # if product is greater as last value and we do multiple two distinct values
        :$m[$p]+=$p; # add 2 times or more the product to array so we drop 36 cause it will be 144  
}
echo$r[$argn]; # Output 

PHP、159バイト

0インデックス付き

for($r=[2,3];!$r[$argn];$r[]=$l=min(array_diff_key($m,$d))){$d=$m=[];foreach($r as$x)foreach($r as$y)$x<$y?${dm[$m[$p=$x*$y]<1&$p>$l]}[$p]=$p:0;}echo$r[$argn];

オンラインでお試しください!

PHP、161バイト

0インデックス付き

for($r=[2,3];!$r[$argn];$r[]=$l=min(array_diff($m,$d))){$d=$m=[];foreach($r as$x)foreach($r as$y)$x<$y?${dm[!in_array($p=$x*$y,$m)&$p>$l]}[]=$p:0;}echo$r[$argn];

オンラインでお試しください!


1

Mathematica、140バイト

(t=1;s={2,3};While[t<#,s=AppendTo[s,Sort[Select[First/@Select[Tally[Times@@@Permutations[s,{2}]],#[[2]]==2&],#>Last@s&]][[1]]];t++];s[[#]])&

1

MATL、25バイト

3:i:"t&*9B#u2=)yX-X<h]2_)

オンラインでお試しください!

説明

3:     % Push [1 2 3]. Initial array of MU numbers, to be extended with more numbers
i:     % Input n. Push [1 2 ... n]
"      % Do this n times
  t    %   Duplicate array of MU numbers so far
  &*   %   Matrix of pair-wise products
  9B   %   Push 9 in binary, that is, [1 0 0 1]
  #    %   Specify that next function will produce its first and fourth ouputs
  u    %   Unique: pushes unique entries (first output) and their counts (fourth)
  2=   %   True for counts that equal 2
  )    %   Keep only unique entries with count 2
  y    %   Duplicate (from below) array of MU numbers so far
  X-   %   Set difference
  X<   %   Minimum. This is the new MU number
  h    %   Concatenate vertically horizontally to extend the array
]      % End
2_     % Push 2 negated, that is, -2
)      % Get entry at position -2, that is, third-last. Implicitly display

1

Perl 6、96バイト

{(2,3,{first *∉@_,@_.combinations(2).classify({[*]
$_}).grep(*.value==1)».key.sort}...*)[$_]}

オンラインでお試しください!

  • 2, 3, { ... } ... *は、3番目から始まる各要素が波括弧で区切られたコードブロックによって計算される無限シーケンスです。コードブロックは、slurpy @_配列を介して引数を取るため、その配列内の現在のシーケンス全体を受け取ります。
  • @_.combinations(2)は、のすべての2要素の組み合わせのシーケンスです@_
  • .classify({ [*] $_ }) 製品ごとに各2タプルを分類し、製品がキーで値がその製品を持つ2タプルのリストであるハッシュを生成します。
  • .grep(*.value == 1) 値(つまり、製品としてそのキーを持つペアのリスト)のサイズが1であるハッシュからキーと値のペアを選択します。
  • ».key各ペアのキーのみを選択します。これは、現在のシーケンスの要因の1つの組み合わせのみから生じる製品のリストです。
  • .sort 製品を数値順に並べ替えます。
  • first * ∉ @_, ... シーケンスにまだ登場していない最初の製品を見つけます。

1

JavaScriptの(ES6)、119の 118 117バイト

0から始まるインデックスを取得する再帰関数。

f=(n,a=[2,m=3])=>a[n]||a.map(c=>a.map(d=>c<d&(d*=c)>m?b[d]=b[d]/0||d:0),b=[])|f(n,a.push(m=b.sort((a,b)=>a-b)[0])&&a)

どうやって?

f()の各反復で、シーケンスの最後の項mと最初は空の配列bを使用して、次の項を識別します。以前の2つの異なるMU番号の各製品d> mに対して、次のことを行います。

b[d] = b[d] / 0 || d

そして、bの最小値を保持します。

上記の式は次のように評価されます。

b[d]               | b[d] / 0  | b[d] / 0 || d
-------------------+-----------+--------------
undefined          | NaN       | d
already equal to d | +Infinity | +Infinity
+Infinity          | +Infinity | +Infinity

これにより、複数の方法で表現できる製品が選択されないことが保証されます。

書式設定およびコメント化

f = (n, a = [2, m = 3]) =>           // given: n = input, a[] = MU array, m = last term
  a[n] ||                            // if a[n] is defined, return it
  a.map(c =>                         // else for each value c in a[]:
    a.map(d =>                       //   and for each value d in a[]:
      c < d &                        //     if c is less than d and
      (d *= c) > m ?                 //     d = d * c is greater than m:
        b[d] = b[d] / 0 || d         //       b[d] = either d or +Infinity (see 'How?')
      :                              //     else:
        0                            //       do nothing
    ),                               //   end of inner map()
    b = []                           //   initialization of b[]
  ) |                                // end of outer map()
  f(                                 // do a recursive call:
    n,                               //   - with n
    a.push(                          //   - push in a[]:
      m = b.sort((a, b) => a - b)[0] //     m = minimum value of b[]
    ) && a                           //     and use a[] as the 2nd parameter
  )                                  // end of recursive call

デモ



0

Pythonの3 2167 139 136 133 123 121の 120 118バイト

a=[2,3];exec'p=[x*y for x in a for y in a if x-y];a+=min(q for q in p if p.count(q)+(q in a)<3),;'*input();print a[-2]

オンラインでお試しください!


@ Mr.Xcoderと@LeakyNunの改善に感謝します!


159バイト。不要なスペースと角かっこを削除するだけです。
ミスターXcoder

@ Mr.Xcoder改善してくれてありがとう。私は確かに変化はないよp.count(q)==1p.count(q)>0それは挑戦の「正確に一つの方法で、」条件を確保したコードだから、有効です。
チェイスヴォーゲリ

p.count(q)-~(q in a)<=3等価であるp.count(q)+(q in a)<3
漏れ修道女

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