新しい注文#4:世界


17

はじめに(無視してもよい)

すべての正の数を規則正しい順序(1、2、3、...)で並べるのは少し退屈ですよね?そのため、すべての正数の順列(再編成)に関する一連の課題があります。これはこのシリーズの4番目の課題です(最初2番目3番目の課題へのリンク)。

この課題では、自然数の1つの順列ではなく、順列の全世界を調べます。

2000年、クラークキンバリングは、カナダ数学学会が発行する数学の科学ジャーナルであるCrux Mathematicorumの 26 号で問題を提起しました。問題は:

Sequence a={a1=1an=an12 if an12{0,a1,...,an1}an=3an1 otherwise

すべての正の整数は、このシーケンスで1回だけ発生しますか?

2004年には、はMateusz Kwasnickiは同じジャーナルに肯定的証拠を提供し、2008年に、彼は公表され、より一般的な証拠(元の質問に比べて)よりフォーマルなと。彼は、パラメーターpおよびqを使用してシーケンスを作成しました。

{a1=1an=an1q if an1q{0,a1,...,an1}an=pan1 otherwise

彼は、l o g pq が非合理的であるようなp,q>1について、シーケンスは自然数の順列であることを証明しました。これが当てはまるp値とq値の数は無限であるため、これは自然数の順列の世界全体です。我々は、元に固執しますP Q = 3 2 、及びこれらの偶然にするために、配列は次のように求めることができるA050000logp(q)pq(p,q)=(3,2)OEISで。最初の20要素は次のとおりです。

1, 3, 9, 4, 2, 6, 18, 54, 27, 13, 39, 19, 57, 28, 14, 7, 21, 10, 5, 15

これは「純粋なシーケンス」チャレンジであるため、タスクは入力として特定のnに対してa n を出力a(n)です。ここで、a n A050000です。na(n)

仕事

整数入力n与えられた場合、整数形式のa(n)出力ます。ここで、

{a(1)=1a(n)=a(n1)2 if a(n1)2{0,a1,...,a(n1)}a(n)=3a(n1) otherwise

a(0)=1;a(1)=3

テストケース

Input | Output
---------------
1     |  1
5     |  2
20    |  15
50    |  165
78    |  207
123   |  94
1234  |  3537
3000  |  2245
9999  |  4065
29890 |  149853

ルール

  • 入出力は整数です(プログラムは少なくとも1〜32767の範囲の入出力をサポートする必要があります)
  • 無効な入力(0、フロート、文字列、負の値など)は、予期しない出力、エラー、または(未)定義された動作につながる可能性があります。
  • デフォルトのI / Oルールが適用されます。
  • デフォルトの抜け穴は禁止されています。
  • これはなので、バイト単位の最短回答が勝ちます

0<N<1000

@Tau:仕様外(およびこの競合しない)ですが、あなたのソリューションに興味があります。投稿できるものはありますか?
agtoever

1
プログラムを削除しましたが、再作成できるはずです。やり直したら、非競合として投稿します。
タウ

@agtoever、「非競合」は無効なソリューションをカバーしていません。課題が投稿された後に作成された言語または言語機能を使用したソリューション用でした。
シャギー

これに関して、PP&CGメタは確かに非常に明確です。「非競合」のそのような厳密な解釈は授与されませんでした... @Tau:これらのルールではTI-BASICソリューションを投稿できないようです。ごめんなさい。
agtoever

回答:


3

Japt15 14バイト

1インデックス付き。

@[X*3Xz]kZ Ì}g

それを試してみてください

@[X*3Xz]kZ Ì}g     :Implicit input of integer U
             g     :Starting with the array [0,1] do the following U times, pushing the result to the array each time
@                  :  Pass the last element X in the array Z through the following function
 [                 :    Build an array containing
  X*3              :      X multiplied by 3
     Xz            :      X floor divided by 2
       ]           :    Close array
        kZ         :    Remove all elements contained in Z
           Ì       :    Get the last element
            }      :  End function
                   :Implicit output of the last element in the array

7

JavaScript(ES6)、 55 51  50バイト

保存された1バイト@EmbodimentofIgnoranceのおかげ
@tshに保存された1つのバイトのおかげで

n=>eval("for(o=[p=2];n--;)o[p=o[q=p>>1]?3*p:q]=p")

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



@EmbodimentofIgnorance評価されたコードはずっと遅いので、私は通常そのトリックを避けます。しかし、その違いはほとんど目立たないので、それでいいと思います。
アーナルド

2
しかし、これはコードのゴルフで、私たちがいる限り、それは仕事を取得するよう、スピードを気にしない
無知の実施の形態

n=>eval("for(o=[p=2];n--;)o[p=o[q=p>>1]?3*p:q]=p")
tsh

5

ゼリー、15 バイト

µ×3żHḞḢḟȯ1Ṫ;µ¡Ḣ

n結果を出力するSTDINからの整数(1ベース)を受け入れる完全なプログラム。

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

どうやって?

µ×3żHḞḢḟȯ1Ṫ;µ¡Ḣ - Main Link: no arguments (implicit left argument = 0)
µ           µ¡  - repeat this monadic chain STDIN times (starting with x=0)
                -                   e.g. x = ...  0      [1,0]            [9,3,1,0]
 ×3             -   multiply by 3                 0      [3,0]            [27,9,3,0]
    H           -   halve                         0      [1.5,0]          [4.5,1.5,0.5,0]
   ż            -   zip together                  [0,0]  [[3,1.5],[0,0]]  [[27,4.5],[9,1.5],[3,0.5],[0,0]]
     Ḟ          -   floor                         [0,0]  [[3,1],[0,0]]    [[27,4],[9,1],[3,0],[0,0]]
      Ḣ         -   head                          0      [3,1]            [27,4]
       ḟ        -   filter discard if in x        []     [3]              [27,4]
        ȯ1      -   logical OR with 1             1      [3]              [27,4]
          Ṫ     -   tail                          1      3                4
           ;    -   concatenate with x            [1,0]  [3,1,0]          [4,9,3,1,0]
              Ḣ - head                            1      3                4
                - implicit print

4

05AB1E16 15バイト

Kevin Cruijssenのおかげで1バイト節約されました
0インデックス。

¾ˆ$FDˆx3*‚;ï¯Kн

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

説明

n=1例として使用

¾ˆ                 # initialize global array as [0]
  $                # initialize stack with 1, input
   F               # input times do:
    Dˆ             # duplicate current item (initially 1) and add one copy to global array
                   # STACK: 1, GLOBAL_ARRAY: [0, 1]
      x            # push Top_of_stack*2
                   # STACK: 1, 2, GLOBAL_ARRAY: [0, 1]
       3*          # multiply by 3
                   # STACK: 1, 6, GLOBAL_ARRAY: [0, 1]
         ‚;ï       # pair and integer divide both by 2
                   # STACK: [0, 3], GLOBAL_ARRAY: [0, 1]
            ¯K     # remove any numbers already in the global array
                   # STACK: [3], GLOBAL_ARRAY: [0, 1]
              н    # and take the head
                   # STACK: 3


@KevinCruijssen:ありがとう!グローバル配列を使用することを考えましたが、それがスタック上のリストと同じ長さになると想定し、それを試したことはありませんでした:/
Emigna

4

Perl 6、49バイト

nwellnofのおかげで-2バイト

{(1,3,{(3*@_[*-1]Xdiv 6,1).max(*∉@_)}...*)[$_]}

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

シーケンス内の0からインデックス付けされた要素を返します。開始要素を次の0,1ように変更することにより、これを1インデックスに変更できます。1,3

説明:

{                                             }  # Anonymous code block
 (                                   ...*)[$_]   # Index into the infinite sequence
  1,3                                            # That starts with 1,3
     ,{                             }            # And each element is
       (                 ).max(    )             # The first of
          @_[*-1]X                               # The previous element
        3*        div 6                          # Halved and floored
        3*        div  ,1                        # Or tripled
                               *∉@_             # That hasn't appeared in the sequence yet

3

J47 40バイト

[:{:0 1(],<.@-:@{:@](e.{[,3*{:@])])^:[~]

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

食べない

[: {: 0 1 (] , <.@-:@{:@] (e. { [ , 3 * {:@]) ])^:[~ ]

定義をJに直接変換します。^:開始値から必要な回数反復することにより、ボトムアップでビルドされます。


3

Java 10、120 99バイト

n->{var L=" 1 0 ";int r=1,t;for(;n-->0;L+=r+" ")if(L.contains(" "+(r=(t=r)/2)+" "))r=t*3;return r;}

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

説明:

n->{                              // Method with integer as both parameter and return-type
  var L=" 1 0 ";                  //  Create a String that acts as 'List', starting at [1,0]
  int r=1,                        //  Result-integer, starting at 1
      t;                          //  Temp-integer, uninitialized
  for(;n-->0;                     //  Loop the input amount of times:
      L+=r+" "))                  //    After every iteration: add the result to the 'List'
                          t=r     //   Create a copy of the result in `t`
                       r=(...)/2  //   Then integer-divide the result by 2
    if(L.contains(" "+(...)+" ")) //   If the 'List' contains this result//2:
      r=t*3;                      //    Set the result to `t` multiplied by 3 instead
  return r;}                      //  Return the result




2

C ++(gcc)189180バイト

-9バイトから小さなゴルフまで

#import<vector>
#import<algorithm>
int a(int n){std::vector<int>s={1};for(int i=0;i<n;++i)s.push_back(i&&std::find(s.begin(),s.end(),s[i]/2)==s.end()?s[i]/2:3*s[i]);return s[n-1];}

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

までのシーケンスを計算しn、目的の要素を返します。大きなインデックスの場合は遅くなります。


@ceilingcat残念ながら、これは演算子の優先順位に影響を与え、関数の出力を変更します。
ニールA.

2

Python 2、66バイト

l=lambda n,p=1,s=[0]:p*(n<len(s))or l(n,3*p*(p/2in s)or p/2,[p]+s)

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

ゼロベースのインデックスを使用します。ラムダは、シーケンスを再帰的に構築し、必要なインデックスに達するとすぐに戻る以上のことは行いません。





1

Pythonの3105の 103 100 95 83バイト

agtoeverのおかげで-2バイトArBoのおかげで
-12バイト

def f(n):
 s=0,1
 while len(s)<=n:t=s[-1]//2;s+=(t in s)*3*s[-1]or t,
 return s[-1]

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


forループをwhile len(s)<=nで置き換え、i をで置き換え-1ます。これにより、2つの文字のいずれかが削除されます。
agtoever

@agtoeverそれはとても賢い-ありがとう!:)
Noodle9

リストの代わりにタプルを使用ifし、whileループから1行を削除できるようにループから削除することにより、83バイト
ArBo

@ArBoすごい!絶対に素晴らしい-ありがとう:)
Noodle9

1

ガイア22 20バイト

2…@⟨:):3פḥ⌋,;D)+⟩ₓ)

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

0から始まるインデックス。

アプローチに対するシャギーの功績

2…			| push [0 1]
  @⟨		 ⟩ₓ	| do the following n times:
    :):			| dup the list L, take the last element e, and dup that
       3פḥ⌋,		| push [3*e floor(e/2)]
	     ;D		| take the asymmetric set difference [3*e floor(e/2)] - L
	       )+	| take the last element of the difference and add it to the end of L (end of loop)
		   )	| finally, take the last element and output it

;D



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