最後の2つの先行モデルと互いに素であり、まだ出現していない最小の正の整数。a(1)= 1、a(2)= 2


10

定義

  • 2つの整数は、以外の正の公約数を共有しない場合、互いに素です1
  • a(1) = 1
  • a(2) = 2
  • a(n)integerに対して、a(n-1)と互いに素であり、a(n-2)まだ出現していない最小の正の整数n >= 3です。

仕事

  • 正の整数を指定するとn、出力/印刷されますa(n)

  • a(11) = 6これ6は、最後の2つの先行(つまり、11および13)と互いに素であり、6以前に出現したことがないためです。

ノート

  • シーケンスは昇順ではないことに注意してください。つまり、要素は前のものよりも小さくなる場合があります。

スペック

  • 1インデックスを使用する必要あります。

テストケース

n      a(n)
1      1
2      2
3      3
4      5
5      4
6      7
7      9
8      8
9      11
10     13
11     6
12     17
13     19
14     10
15     21
16     23
17     16
18     15
19     29
20     14
100    139
1000   1355
10000  13387
100000 133361

得点

  • coprimeは、2つの数値が1つの除数(1)のみを共有することを意味し、1小さい数値であるため、コードはバイト数の観点からできるだけ小さくする必要があります。

参考文献


4
短いコードの「理由」...
Luis Mendo

1
なぜこれが反対票だったのかしら。恐ろしい根拠が原因ではないでしょうか?
Conor O'Brien

@Conor私じゃない。実際私は賛成した。私は人々が理論的根拠と私のコメントの両方をジョークとして見てくれることを願っています
Luis Mendo 2016

3
コードゴルフのこれらの「おかしい」正当化の問題は、これが標準のコードゴルフであることを知るためだけに、4行にわたる悪いジョークを読む必要があることです。それは、正当な理由もなく、チャレンジのルールを単純に覆い隠しているだけです。
マーティンエンダー

1
@ ConorO'Brienすべてのブラウザが常にタイトルを表示するわけではありません(モバイルアプリもあります)。タグを使用するだけでなく、一般的に投稿のスコアリングについて説明します。タグだけでは新しい人には何の意味もないからですサイトへ。私はにもかかわらず午前私たちのチャレンジタイプのタグに精通し、私はチャレンジが得点された方法を見つけ出すことが、挑戦本体にそれを見つけることを試みるためにそれらを読んだことがありません。タグは、タグウィキ内の分類、検索機能、およびチャレンジタイプ固有の情報用です。
マーティンエンダー

回答:


5

パイソン3.5、160の 141 126 124 121 109バイト

これは、シーケンスの定義の単純な実装です。ゴルフの提案を歓迎します。

編集: Leaky Nunのおかげで-17バイト。ピーター・テイラーのおかげで-9バイト。Sp3000とPython 3.5への切り替えにより、-6バイト。

import math;f=lambda n,r=[2,1],c=3:n<2and r[1]or(c in r)+math.gcd(c,r[0]*r[1])<2and f(n-1,[c]+r)or f(n,r,c+1)

Ungolfing:

import math
def f(n, r=[2,1], c=3):
    if n<2:
        return r[1]
    elif (c in r) + math.gcd(c,r[0]*r[1]) < 2:
        return f(n-1, [c]+r)
    else:
        return f(n, r, c+1)

Python 3.5以降のimport math場合g=math.gcd、独自のを定義するよりも短くする必要がありますg。3.5前の場合は、あなたが行うことができますfrom fractions import*のためにgcd
Sp3000

c=3ループ内で初期化する場合は、一度だけ実行する必要があります。私のカウントでは、あなたは3文字を節約します。
Peter Taylor

また、逆の方法で配列を構築することで2文字節約できます。r=[c]+rではなくを使用する必要がありますが+=、3つの負のインデックスが正になります。そして、ラムダとして書き直すことでさらに2文字節約できますが、それはかなり大幅な変更です。完全なプログラムではなくなったfrom fractions import*;F=lambda n,r=[2,1],c=3:n<2and r[1]or(c in r)+gcd(r[0]*r[1],c)<2and F(n-1,[c]+r)or F(n,r,c+1)ため、aは必要ありprintません。
Peter Taylor

2

MATL28 27バイト

2:i:"`@ym1MTF_)Zdqa+}@h]]G)

コードは遅いですが、正しい結果が得られます。

オンラインでお試しください!または、最初の10ケースを確認します。

コードを少し変更すると、シーケンスのプロットが作成されます。

2:i:"`@ym1MTF_)Zdqa+}@h]]G:)XG

それをASCIIアートとして、またはオフラインコンパイラのグラフィック出力で確認してください。

ここに画像の説明を入力してください

ここに画像の説明を入力してください

説明

2:         % Push [1 2] to initiallize the sequence
i:         % Input n. Push [1 2 ... n]
"          % For loop: repeat n times
  `        %   Do while loop
    @      %     Push iteration index, starting at 1. This is the candidate number
           %     to extend the sequence
    y      %     Duplicate vector containing the sequence so far
    m      %     Is member? Gives true if the candidate is in the sequence
    1M     %     Push candidate and vector again
    TF_)   %     Get last two elements of the vector
    Zd     %     GCD between the candidate and those two elements. Produces a
           %     two-element vector
    qa     %     True if any of the two results exceeds 1, meaning
           %     the candidate is not coprime with the latest two sequence values
    +      %     Add. This corresponds to logical "or" of the two conditions, namely
           %     whether the candidate is member of the sequence so far, and
           %     whether it is not coprime with the latest two. In either case
           %     the do...while must continue with a next iteration, to try a new
           %     candidate. Else the loop is exited, and the current candidate
           %     is the new value of the sequence
  }        %   Finally (execute when the loop is exited)
    @h     %     Push current candidate and concatenate to the sequence vector
  ]        %   End do...while
]          % End for
G)         % Get n-th value of the sequence. Implicitly display

1

C、185バイト

G(a,b){return a%b?G(b,a%b):b;}
i,j,k;f(n){int a[n+2];for(i=0;i++<n;){a[i]=i<3?i:0;for(j=2;!a[i];++j){for(k=i;--k;){if(a[k]==j)++j,k=i;}a[G(a[i-1],j)*G(a[i-2],j)<2?i:0]=j;}}return a[n];}

1

実際には38 37 35 33 31 30バイト

これは、関数定義の単純な実装です。ゴルフの提案を歓迎します。オンラインでお試しください!

編集: Leaky Nunのおかげで-3バイト。

2R#╗,;`1";2±╜tπg@╜í+Y"£╓╖`nD╜E

Ungolfing:

2R#╗    Push [1,2] and store it in register 0
,;      Take input and duplicate
`1      Start function, push 1
  "       Start string
  ;       Duplicate i
  2±╜t    Push (list in register 0)[-2:]
  πg      gcd(i, product of list[-2:])
  @╜í     Rotate the gcd and bring up i, check for i in list (0-based, -1 if not found)
  +Y      Add the gcd and the index, negate (1 if coprime and not found in list, else 0)
  "£      End string, turn into a function
╓       Push first (1) values where f(x) is truthy, starting with f(0)
╖`      Append result to the list in register 0, end function
n       Run function (input) times
D╜E     Return (final list)[n-1]

1
スタック操作が多い
Leaky Nun

0

Haskell、81 73バイト

c l@(m:n:_)=m:c([x|x<-[1..],gcd(m*n)x<2,all(/=x)l]!!0:l)
((0:1:c[2,1])!!)

使用例:((0:1:c[2,1])!!) 12-> 17

すべてのリストを作成します。1からa(n)始まる0インデックスを修正することから始め1、次にを作成しc[2,1]ます。cその引数リストの先頭を受け取り、lその後に再帰呼び出しを行い、次に適合する(共素数、これまでにない)次の数をの前に追加しlます。nこのリストのth番目の要素を選択します。


0

R、141バイト

 f=Vectorize(function(n)ifelse(n>3,{c=3;a=f(n-1);b=f(n-2);d=f(4:n-3);while(!c%%which(!a%%1:a)[-1]||!c%%which(!b%%1:b)[-1]||c%in%d)c=c+1;c},n))

ない

f=Vectorize( function(n)     #build a recursive function. Vectorize allows
    if(n>3) {                #the function to be called on vectors.
        c=3                  #Tests size. Builds some frequent variables.
        a=f(n-1)
        b=f(n-2)
        d=f(4:n-3)           #Should really golf this out, but its horribly slow.
        while(!c%%which(!a%%1:a)[-1]||!c%%which(!b%%1:b)[-1]||c%in%d)
              c=c+1          #If we are coprime and not already seen. add.
        c
     } else n)               #First three are 1,2,3.

0

Mathematica、97 90バイト

a@1=1;a@2=2;a@n_:=SelectFirst[Range[2n],GCD[a[n-1]a[n-2],#]<2&&!MemberQ[a/@Range[n-1],#]&]

基づいて、私の推測その a(n) < 2nすべてについてn

より高速に実行するa@n=には、元の値の後に追加し:=、関数が以前の値を再計算する必要がないようにします

Sherlock9おかげバイト保存7(IF gcd(a,b)=1その後gcd(ab,m) = gcd(a,m)*gcd(b,m)


OEISのページに " ABS(a(n)-n) < n"
Leaky Nun

@LeakyNunありがとう。OEISのページは数分前までダウンしており、大きなの反例の可能性について心配していましたn

0

Pyth、23バイト

eu+Gf&-TGq1iT*F>2G1tQ]1

テストスイート

かなり単純な実装ですが、いくつかの素晴らしいゴルフのトリックがあります。

eu+Gf&-TGq1iT*F>2G1tQ]1
 u                 tQ]1    Apply the following function input - 1 times,
                           where G is the current state (List of values so far)
  +G                       Add to G
    f             1        The first number, counting up from 1
      -TG                  That has not been seen so far
     &                     And where
               >2G         The most recent two numbers
             *F            Multiplied together
           iT              Gcd with the current number being checked
         q1                Equals 1
e                          Output the final element of the list.
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.