Recamánのシーケンスを生成する


20

Recamánのシーケンス(A005132)は、次のように定義される数学的シーケンスです。

A(0) = 0
A(n) = A(n-1) - n if A(n-1) - n > 0 and is new, else
A(n) = A(n-1) + n

上記のかわいいLaTexバージョン(読みやすいかもしれません):

A(n)={0if n=0A(n1)nif A(n1)n is positive and not already in the sequenceA(n1)+notherwise

最初のいくつかの用語は 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11

明確にis newするために、番号が既にシーケンスにあるかどうかを意味します。

n関数引数またはSTDINを介して整数を指定するnと、Recamánシーケンスの最初の項を返します。


これはコードとゴルフの挑戦なので、最短のコードが勝ちます。


「新しい」とはどういう意味ですか?
ベータ崩壊14

番号が新しい場合、それはまだシーケンスにないことを意味します。間違ったシーケンスを入力したことに気付いたので、それを修正するために少し時間をください。
ジェームズウィリアムズ14

シーケンスを修正しました。
ジェームズウィリアムズ14

1
シーケンスの最初の値を追加できますか?
誇りに思ってhaskeller 14

最初のいくつかの数字を追加しました!(およびOEISページへのリンク)
ジェームズウィリアムズ14

回答:


9

CJam、34 33バイト

0ali{_W=_I-__0<4$@#)|@I+@?+}fI1>`

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

実行例

$ cjam <(echo '0ali{_W=_I-__0<4$@#)|@I+@?+}fI1>`') <<< 33
[0 1 3 6 2 7 13 20 12 21 11 22 10 23 9 24 8 25 43 62 42 63 41 18 42 17 43 16 44 15 45 14 46]

使い方

0ali                               " Push S := [ 0 ] and read an integer N from STDIN.    ";
    {                      }fI     " For each I in [ 0 ... (N - 1) ]:                     ";
     _W=                           "   X := S[-1].                                        ";
        _I-                        "   Y := X - I                                         ";
            _0<                    "   A := (Y < 0)                                       ";
           _   4$@#)               "   B := (Y ∊ S)                                       ";
                     @I+           "   Z := X + I                                         ";
                    |   @?         "   C := (A || B) ? Z : Y                              ";
                          +        "   S += [C]                                           ";
                              1>`  " Push str(S[1:]).                                     ";

どのような変更を加えましたか?
ソーハムチョードリー

私の最初のアプローチはシーケンスに負の数を追加したので、を明示的にチェックする必要はありませんでしたA(i) - i > 0。ただし、の値が小さい場合は十分な数を追加しませんでしたn。今、私はちょうど仕様が言うことを正確にします。
デニス14

33対45。とても近いが、これまでのところ。:)
IngoBürk14年

うわー、e#チャムにコメントなしで…おいしいチェリー。
クロム

8

ハスケル、74

l=0:0#1
a§v|a<0||a`elem`r v=v|1<2=0-v
a#b=a+(a-bb:l!!b#(b+1)
r=(`take`l)

使用例:

λ> r 20
[0,1,3,6,2,7,13,20,12,21,11,22,10,23,9,24,8,25,43,62]


5

パイソン2、78 75 73の 69バイト

xnorとflornquakeへの称賛
最初の回答よりもほぼ10バイト短くなりました

m=p,=0,
exec"p+=1;k=m[-1]-p;m+=k+2*p*(k*(k>0)in m),;"*input()
print m

あなたは短縮することができます[k,k+2*p][bool]k+2*p*(bool)
XNOR

@xnorありがとう、3バイト節約しました。
マルクス14

また、k in m or k<0可能k*(k>=0)in mならば以降k<0の製品があり、0中にあります、m
XNOR

@xnorブリリアント!再びありがとう
マルクス14

-1代わりに書くことができますp-1。編集:mタプルを作成してm=0,とを書くこともできm+=k+2*p*(k*(k>0)in m),ます。
flornquake

4

Golfscript(41 45

こちらからオンラインでお試しください:

(,1,\{:~1$=~)-:^1<\.^?)!!@|^\{~)2*+}*+}/

説明

これは元の45バイトのソリューション用ですが、それでもほぼ同じです。

(,              # push array [0 .. n-1]
[0]\            # push sequence elements as [0] and reverse stack
{               # foreach element in [0 .. n-1] do:
  :m;           # store current element in m and discard
  .m=           # get the previous sequence element
  m)-:^         # subtract the current index from it and store in ^
  0>            # is that number greater than 0?
  \.^?)!        # is that number new to our sequence?
  @&            # logically and both checks
  {^}           # if true, push ^
  {^m)2*+}      # otherwise, add the index twice and push
  if
  +             # add new element to our sequence
}/
`               # make output pretty

編集#1: 4バイトを削り取ってくれたDennisに感謝します。


4

dc、46バイト

sn[z+z+d0r:a]sF0[pz-d1>Fd;a0<Fddd:azln!<M]dsMx

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

このプログラムは、空のスタックから入力を受け取り、stdout(改行区切り)に出力します。

私はこれを本当に誇りに思っています-それは専用のゴルフ言語ではないすべてを打ち負かし、私のお気に入りのDCゴルフトリックの3つを紹介しています:

  • インデックス変数として使用されるスタックサイズ
  • 「if A then B else C」を「無条件にC、if A then D」にリファクタリングし、CとDを組み合わせてBにする
  • 一意性制約を解決するための、ほとんど使用されないランダムアクセス配列機能

説明

sn             Stores the input in register n
[z+z+0r:a]sF   Defines the macro F, which: 
    z+z+         adds twice the stack size/index variable
    0r:a         resets the "uniqueness" flag to 0 in the array a
               In context, F is the "D" in my description above, 
               changing A(z-1)-z to A(z-1)+z
0              The main loop starts with the previous sequence member on top of 
               the stack and total stack depth equal to the next index. 
               Pushing a zero accomplishes both of these things.
[              Start of the main loop M
  p               Print the previous sequence member, with newline (no pop)
  z-             Calculate A(z-1)-z
  d1>F           If that's nonpositive, (F)ix it to be A(z-1)+z
  d;a            a is my array of flags to see if we've hit this value before
  0<F            If we have, (F)ix it! (nonzero = flag, since ;a is zero by
                 default, and also zero if we just (F)ixed and therefore 
                 don't care about uniqueness right now)
  ddd            Make one copy to keep and two to eat
  :a             Flag this entry as "used" in the uniqueness array a
  zln!<M         If our "index variable" is n or less, repeat!
]dsMx          End of main loop - store it and execute

それはワイルドで、DCが存在することすら知らなかった
明るい

3

JavaScript- 81 80 79 70

9バイトの節約を支援してくれたedc65への称賛

f=n=>{for(a=[x=i=0];++i<n;)a[i]=x+=x>i&a.indexOf(x-i)<0?-i:i;return a}

-9:g = n => {for(a = [x = i = 0]; ++ i <n;)a [i] = x + = x> i&a.indexOf(xi)<0?-i:i ;リターンa}
edc65 14

@ edc65 Grazie mille :)
ウィリアム

3

JavaScriptを、ES6、74の 69文字

最新のFirefoxのWebコンソールで以下のコードを実行します。

G=n=>(i=>{for(r=[t=0];++i<n;)r[i]=t+=i>t|~r.indexOf(t-i)?i:-i})(0)||r

後でもっとゴルフしようとします。

使用例:

G(11) -> 0,1,3,6,2,7,13,20,12,21,11

3

MATLAB、83 78バイト

以下をf.m(73バイト)として保存します

A=0;for i=1:n-1 b=A(i)-i;A(i+1)=b+2*i;if b>0&&~any(A==b) A(i+1)=b;end;end

コマンドウィンドウから実行(5バイト)

n=9;f

上記が正当でない場合、90バイトが必要です。

function A=f(n) 
A=0;for i=1:n-1 b=A(i)-i;A(i+1)=b+2*i;if b>0&&~any(A==b) A(i+1)=b;end;end

3

R:96文字

ゴルフ:

A=function(s,n,m,i){if(m==n){return(s)}else{t=i-m;if(t%in%s||t<0){t=i+m};s=c(s,t);A(s,n,m+1,t)}}

ゴルフをしていない:

A = function(s,n,m,i) {
    if(m==n){return(s)}
    else{
        t=i-m
        if(t%in%s||t<0){t=i+m}
        s=c(s,t)
        A(s,n,m+1,t)
    }
}

サンプル実行:

> An(0,34,1)
[1]   0   1   3   6   2   7  13  20  12  21  11  22  10  23   9  24   8
[18]  25  43  62  42  63  41  18  42  17  43  16  44  15  45  14  46  79



3

05AB1E、19バイト

¾ˆG¯¤N-DŠD0›*åN·*+ˆ

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

説明

¾ˆ                    # Initialize the global list with 0
  G                   # for N in [1, input-1] do:
   ¯                  # push the global list
    ¤N-               # subtract N from the last item in the list
       D              # duplicate
        Š             # move the copy down 2 spots on the stack
         D            # duplicate again
          0›          # check if it is positive
            *         # multiply, turning negative results to zero
             å        # is the result already present in the list?
              N·*     # multiply by N*2
                 +    # add to the result
                  ˆ   # add this to the list

これはどのように作動しますか?
リルトシアスト

@lirtosiast:このチャレンジをしてからしばらく経ちましたので、これは私がすぐにできる最善の説明です。それで十分だと思います。
エミグナ

3

K(oK)、53バ​​イト

溶液:

{$[y>c:#x;o[x,(r;*|x+c)(r in x)|0>r:*|x-c;y];x]}[,0;]

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

説明:

再帰的なソリューション。

{$[y>c:#x;o[x,(r;*|x+c)(r in x)|0>r:*|x-c;y];x]}[,0;] / the solution
{                                              }[,0;] / lambda with first arg set as list containing 0
 $[      ;                                  ; ]       / if[condition;true;false]
       #x                                             / length of x
     c:                                               / save as c
   y>                                                 / y greater than? (ie have we produced enough results?)
                                             x        / return x if we are done
          o[                             ;y]          / recurse with new x and existing y
                                      x-c             / subtract c from x
                                    *|                / reverse first, aka last
                                  r:                  / save result as r
                                0>                    / 0 greater than?
                               |                      / or
                       (      )                       / do together
                        r in x                        / r in x?
              ( ;     )                               / use result to index into this 2-item list
                   x+c                                / add c to x
                 *|                                   / reverse first, aka last 
               r                                      / result
            x,                                        / append to x

2

Java、144

int[]f(int n){int[]a=new int[n];a[0]=0;int i,j,k,m;for(i=0;i<n-1;){k=a[i++]-i;m=0;for(j=0;j<i;)if(k==a[j++])m=1;a[i]=m<1&k>0?k:k+2*i;}return a;}

2

Luaの- 141 135 139 135

function s(n)a,b={1},{[0]=0}for i=1,n do k=b[i-1]-i c=k+i+i if(k>0)and(a[k]==nil)then b[i],a[k]=k,1 else b[i],a[c]=c,1 end end return b end

読み取り可能なバージョン:

function s(n)
a,b={1},{[0]=0}
for i=1,n do 
   k=b[i-1]-i 
   c=k+i+i
   if (k>0) and (a[k]==nil) then 
      b[i],a[k]=k,1 
   else 
      b[i],a[c]=c,1
   end 
end 
return b 
end

私は2つのテーブルを使用し、最初のものが呼び出され、それはそう構築されている[I] = 1のときに限りiが既にシーケンスに現れたゼロ二番目の表は、実際に配列を保持しつつ、それ以外の場合


あなたの順序はかかわらず、0で始まる必要があります
ウィリアム・バルボサ

1
あなたは正しいです、私は質問をあまり注意深く見ておらず、mathworldで同じ定義(1から始まる)があると仮定しました、それはそれ以上の文字を必要としないと思います、私は後でテストして修正します、今、携帯電話から書いています!

2

Python、73

def f(x,t=0):
 if x:t=f(x-1);t+=2*x*(t*(t>0)in map(f,range(x)))
 return t

編集1:他のPythonの答えに関する@xnorのヒントに感謝します!(私は両方が非常によく似ていることに気付きました。)

編集2:再びありがとう、@ xnor。


これにより、無限ループが発生します。f(x)常にすぐに呼び出されるとは限らないため、何らかの制御フローが必要f(x-1)です。
xnor 14

@xnorはコードを修正しました。
ソハムチョードリー14

1
これは、最初のn個の項ではなく、n番目の項を返すようです。
デニス14

いくつかのマイナーなが保存されます:t=0にオプションのパラメータとして行くことができるf、とt=t+することができt+=
XNOR

2

グルービー:122 118 111文字

ゴルフ:

m=args[0] as int
a=[0]
(1..m-1).each{n->b=a[n-1];x=b-n;(x>0&!(x in a))?a[n]=x:(a[n]=b+n)}
a.each{print "$it "}

ゴルフをしていない:

m = args[0] as int
a = [0]
(1..m-1).each { n->
    b = a[n-1]
    x = b-n
    ( x>0 & !(x in a) ) ? a[n] = x : (a[n] = b+n) 
}
a.each{print "$it "}

サンプル実行:

bash$ groovy Rec.groovy 14
0 1 3 6 2 7 13 20 12 21 11 22 10 23

2

Clojure:174文字

ゴルフ:

(defn f[m a](let[n(count a)b(last a)x(- b n)y(if(and(> x 0)(not(.contains a x)))x(+ b n))](if(= m n)a(f m(conj a y)))))(println(f(read-string(first *command-line-args*))[0]))

ゴルフをしていない:

(defn f[m a]
  (let [n (count a) 
        b (last a) 
        x (- b n) 
        y (if (and (> x 0) (not (.contains a x))) x (+ b n)) ]
    (if (= m n) a (f m (conj a y))) ) )

(println (f (read-string (first *command-line-args*)) [0]) )

サンプル実行:

bash$ java -jar clojure-1.6.0.jar rec.clj 14 
[0 1 3 6 2 7 13 20 12 21 11 22 10 23]

1
STDINから読み取るのではなく、関数に整数の引数を使用することをお勧めします:)またyletフォームで定義してもメリットが得られない場合は、値が必要な場所で式を直接使用できます。
ニコニール

2

Mathcad、54「バイト」

enter image description here


ユーザーの観点から見ると、Mathcadは事実上2Dホワイトボードであり、式は左から右、上から下に評価されます。Mathcadは従来の「テキスト」入力をサポートしていませんが、代わりにテキストと特別なキー/ツールバー/メニュー項目の組み合わせを使用して、式、テキスト、プロットまたはコンポーネントを挿入します。たとえば、「:」と入力して定義演算子(画面に「:=」と表示)を入力するか、「ctl-shft-#」と入力してforループ演算子(反復変数、反復値、および1つの本文のプレースホルダーを含む)式)。上の画像に表示されているのは、ユーザーインターフェイスに表示され、「入力」されたとおりです。

ゴルフを目的とする場合、「バイト」カウントは、式を入力するために必要なキーボード操作と同等の数です。


それがすべてですうまく良いが、何している実際のキーストロークは?
ジョーキング


2

スタックス、19 バイト

É╖C8½ΔL▄░▬L+≡ΩSa⌂¼╧

実行してデバッグする

開梱されていない、コメントされていない、これはこのように見える これまでのシーケンスをスタックに保持A(n - 1)し、Xレジスタに記憶します。反復インデックスはに使用されnます。1回目は0ですが、その反復では特別なケースなしで0を生成するため、オフ1インデックスを調整する必要はありません。

0X      push 0 to main stack and store it in X register, which will store A(n - 1)
z       push an empty array that will be used to store the sequence
,D      pop input from input stack, execute the rest of the program that many times
  xi-Y  push (x-register - iteration-index) and store it in the Y register
        this is (A(n - 1) - n)
  0>    test if (A(n - 1) - n) is greater than 0 (a)
  ny#   count number of times (A(n - 1) - n) occurs in the sequence so far (b)
  >     test if (a) > (b)
    y   (A(n - 1) - n)
    xi+ A(n - 1) + n
  ?     if/else;  choose between the two values based on the condition
  X     store the result in the X register
  Q     print without popping
  +     append to sequence array

これを実行してデバッグする


面白い。これはどのように作動しますか?
明るくしない

1
@donbright:注釈と説明を追加しました。
再帰


2

Pyth、24バイト

tu+G-eG_W|g0J-eGH}JGHQ]0

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

tu+G-eG_W|g0J-eGH}JGHQ]0   Implicit: Q=eval(input())
 u                   Q     Reduce [0-Q)...
                      ]0   ... with initial value G=[0], next value as H:
              eG             Last value of G (sequence so far)
             -  H            Take H from the above
            J                Store in J
          g0J                0 >= J
                 }JG         Is J in G?
         |                   Logical OR of two previous results
       _W           H        If the above is true, negate H, otherwise leave as positive
    -eG                      Subtract the above from last value in G
  +G                         Append the above to G
                           The result of the reduction is the sequence with an extra leading 0
t                          Remove a leading 0, implicit print

1

パワーシェル(103)

$n=Read-Host;$a=@(0);$n-=1;1..$n|%{$x=$a[-1]-$_;if($x-gt0-and!($a-like$x)){$a+=$x}else{$a+=$x+2*$_}};$a

別の「単語ごとの」実装もここにあります。PowerShellでも驚くほど読みやすい。

シーケンスは配列$ aに保存され、1行に1つの用語が出力されます。

$ n = 20の場合、ステートメント$a-join","を実行すると

0,1,3,6,2,7,13,20,12,21,11,22,10,23,9,24,8,25,43,62

1

C#:140文字

int i,w,t,y;int[]F(int n){var r=new int[n--];for(;i<n;y=0){w=r[i++]-i;for(t=0;y<i&&t<1;)t=w==r[y++]?1:0;r[i]=w>0&&t<1?w:r[i-1]+i;}return r;}

1

C ++:180文字(cinおよびcoutステートメントなしの158)

int a[5000000][2]={0},i,k,l;a[0][0]=0;a[0][1]=1;cin>>k;for(i=1;i<=k;i++){l=a[i-1][0];if(l-i>0&&a[l-i][1]!=1){ a[i][0]=l-i;a[l-i][1]=1;}else{ a[i][0]=l+i;a[l+i][1]=1;}cout<<a[i][0]<<endl;

プログラミングパズルとCode Golf Stack Exchangeへようこそ!他の回答にあるように、ソリューションの文字/バイト数をヘッダーに編集してください。また、できるだけコードをゴルフしてください(例:空白を削除して文字数を減らしてください)。ありがとう!
ドアノブ

確かに、私はそうします。
アブヘイジャイン

1

Mathematica-81バイト

Fold[#~Append~(#[[-1]]+If[#[[-1]]>#2&&FreeQ[#,#[[-1]]-#2],-#2,#2])&,{0},Range@#]&

使用法

Fold[#~Append~(#[[-1]]+If[#[[-1]]>#2&&FreeQ[#,#[[-1]]-#2],-#2,#2])&,{0},Range@#]&[30]
{0,1,3,6,2,7,13,20,12,21,11,22,10,23,9,24,8,25,43,62,42,63,41,18,42,17,43,16,44,15,45}

1

PHP、89バイト

$f=function($n){for(;$i<$n;$s[$r[$i++]=$p=$m]=1)if($s[$m=$p-$i]|0>$m)$m=$p+$i;return$r;};

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

ゴルフをしていない:

$f = function ($n) {
    for (; $i < $n; $s[$r[$i++] = $p = $m] = 1) {
        if ($s[$m = $p - $i] | 0 > $m) {
            $m = $p + $i;
        }
    }

    return $r;
};
  • $r 私の結果のために
  • $s 見られたものを追跡するため
  • $p 前の値
  • $m m ext値

1

共通LISP(139バイト)

(defun r(n)(do*(s(i 0(1+ i))(a 0(car s))(b 0(- a i)))((> i n)(nreverse s))(push(cond((= 0 i)0)((and(> b 0)(not(find b s)))b)(t(+ a i)))s)))

ゴルフをしていない:

(defun recaman (n)
  (do*
   (series               ; starts as empty list
    (i 0 (1+ i))         ; index variable
    (last 0 (car s))     ; last number in the series
    (low 0 (- last i)))

   ((> i n)              ; exit condition
    (nreverse series))   ; return value

    (push                ; loop body
     (cond
       ((= 0 i) 0)       ; first pass
       ((and
         (> low 0) (not (find low s)))
        low)
       (t (+ last i)))
     series)))
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.