フィボナッチ数列Mod Kで不足している数値を見つける


20

このMath.SEの質問に触発されました。

バックグラウンド

フィボナッチ数列(と呼ばれるF)を開始シーケンスである0, 1各番号(ようにF(n)(最初の二つの後))がその前2つ(の和であるがF(n) = F(n-1) + F(n-2))。

フィボナッチ数列mod K(と呼ばれるM)は、フィボナッチ数列mod K(M(n) = F(n) % K)です。

各値は前のペアによって決定されるため、フィボナッチ数列mod KはすべてのKに対して循環的であり、非負整数の可能なペアはK 2のみであり、両方ともKより小さいことが示されます。は、最初に繰り返される項のペアの後に循環します。最初に繰り返される項のペアが表示される前にフィボナッチ数列mod Kに表示されない数値。

K = 4の場合

0 1 1 2 3 1 0 1 ...

K = 8の場合

0 1 1 2 3 5 0 5 5 2 7 1 0 1 ...

K = 8の場合、repeatの前に0 14と6は表示されないため、Fibonacci Sequence mod 8には4と6は表示されません。

チャレンジ

厳密に0より大きい整数Kが与えられた場合、フィボナッチ数列mod Kに現れない Kより小さいすべての非負整数を出力します。

ルール

  • デフォルトの抜け穴は禁止されています。

  • I / Oをデフォルト

  • プログラムまたは機能は許容されます。

  • Kは(理由の範囲内で)ネイティブ整数型に適合すると仮定できます。

  • フィボナッチ数列mod Kに表示されないK未満の非負の数がある場合、プログラム/関数はすべてのそのような数を合理的な方法で出力する必要があります。

  • フィボナッチ数列mod Kに表示されないK未満の負でない整数がない場合、プログラム/関数は空のリストを返す、何も印刷しない、エラーを生成するなどしてこれを示すことができます。

  • 順序は関係ありません。

  • これはであるため、各言語で最も短い回答が優先されます。

テストケース

オンラインでテストケースを生成します!

空でないテストケース

  8 [4, 6]
 11 [4, 6, 7, 9]
 12 [6]
 13 [4, 6, 7, 9]
 16 [4, 6, 10, 12, 14]
 17 [6, 7, 10, 11]
 18 [4, 6, 7, 9, 11, 12, 14]
 19 [4, 6, 7, 9, 10, 12, 14]
 21 [4, 6, 7, 9, 10, 11, 12, 14, 15, 16, 17, 19]
 22 [4, 6, 7, 9, 15, 17, 18, 20]
 23 [4, 7, 16, 19]
 24 [4, 6, 9, 11, 12, 14, 15, 18, 19, 20, 22]
 26 [4, 6, 7, 9, 17, 19, 20, 22]
 28 [10, 12, 14, 16, 18, 19, 23]
 29 [4, 6, 7, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 27]
 31 [4, 6, 9, 12, 14, 15, 17, 18, 19, 22, 25, 29]
 32 [4, 6, 10, 12, 14, 18, 20, 22, 26, 28, 30]
 33 [4, 6, 7, 9, 15, 17, 18, 20, 24, 26, 27, 28, 29, 31]
 34 [4, 6, 7, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 27, 28, 30]
 36 [4, 6, 7, 9, 10, 11, 12, 14, 16, 18, 20, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32]
 37 [9, 10, 14, 17, 20, 23, 27, 28]
 38 [4, 6, 7, 9, 10, 11, 12, 14, 15, 16, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 31, 32, 33, 36]
 39 [4, 6, 7, 9, 15, 17, 19, 20, 22, 24, 30, 32, 33, 35]
...
200 [4, 6, 12, 14, 20, 22, 28, 30, 36, 38, 44, 46, 52, 54, 60, 62, 68, 70, 76, 78, 84, 86, 92, 94, 100, 102, 108, 110, 116, 118, 124, 126, 132, 134, 140, 142, 148, 150, 156, 158, 164, 166, 172, 174, 180, 182, 188, 190, 196, 198]
...
300 [6, 18, 30, 42, 54, 66, 78, 90, 102, 114, 126, 138, 150, 162, 174, 186, 198, 210, 222, 234, 246, 258, 270, 282, 294]
...
400 [4, 6, 10, 12, 14, 20, 22, 26, 28, 30, 36, 38, 42, 44, 46, 52, 54, 58, 60, 62, 68, 70, 74, 76, 78, 84, 86, 90, 92, 94, 100, 102, 106, 108, 110, 116, 118, 122, 124, 126, 132, 134, 138, 140, 142, 148, 150, 154, 156, 158, 164, 166, 170, 172, 174, 180, 182, 186, 188, 190, 196, 198, 202, 204, 206, 212, 214, 218, 220, 222, 228, 230, 234, 236, 238, 244, 246, 250, 252, 254, 260, 262, 266, 268, 270, 276, 278, 282, 284, 286, 292, 294, 298, 300, 302, 308, 310, 314, 316, 318, 324, 326, 330, 332, 334, 340, 342, 346, 348, 350, 356, 358, 362, 364, 366, 372, 374, 378, 380, 382, 388, 390, 394, 396, 398]
...

空のテストケース(出力なし、エラー、空のリストなどは許容可能な出力です)

1, 2, 3, 4, 5, 6, 7, 9, 10, 14, 15, 20, 25, 27, 30, 35 ... 100 ...

関連:

フィボナッチ軌道を数える

ピサノ時代を見つける


サンドボックス(削除済み)。
pizzapants184

回答:



6

Haskell、70バイト

Esolanging Fruitのおかげでいくらかのバイトが節約されました

Laikoniのおかげで8バイト節約

a=1:scanl(+)1a
f x=[u|u<-[2..x-1],and[mod b x/=u|(_,b)<-zip[1..x^2]a]]

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


@EsolangingFruitああありがとう!私自身も同様の結論に至っていました。
小麦ウィザード

read$showfromIntegerこの場合の代わりに機能し、2バイトを節約します。
ライコニ

zip[1..x^2]切り捨てに使用すると、さらにバイトが節約されます:オンラインで試してください!
ライコニ

@Laikoniしばらく時間がかかりましたが、変更を加えました。おかげで、それは良いアイデアです。
小麦ウィザード

5

Perl 6の 43の42 39  32バイト

{^$_ (-)(1,1,(*+*)%$_...->\a,\b{!a&&b==1})}

試して

{^$_∖(1,1,(*+*)%$_...->\a,\b{!a&&b==1})}

試して

{^$_∖(1,1,(*+*)%$_...{!$^a&&$^b==1})}

試して

{^$_∖(1,1,(*+*)%$_...!*&*==1)}

試して

拡張:

{  # bare block lambda with implicit parameter 「$_」

  ^$_               # Range upto and excluding the input

                   # set minus (U+2216)

  (                 # generate the Fibonacci sequence mod k

    1, 1,           # seed the sequece (can't be 0,1)

    ( * + * ) % $_  # add two values and modulus the input (lambda)

    ...             # keep doing that until

                    # it matches 0,1
    !*              #   negate the first param (1 when 0)
    &               #   and Junction
    *               #   second param
    == 1            #   both match 1

  )
}

3

> <>、48バイト

01\
?!\:&+{:}%:1$0p&$:
v0\~:1=?
>?!;1-::0g?!nao:

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

-vフラグを介して入力を受け取ります。

余分な改行を大量に印刷しますが、仕事は完了です。これは基本的に最初の行を使用して、シーケンス内でこれまでに出現した一連の数値を格納します。

使い方:

01\    Input is already on the stack
...... Initialises the sequence with 1 and 0
...... Goes to the second line
......

......
..\:&+{:}% Gets the next number in the modded Fibonacci sequence while preserving the previous number
......
......

......
..........:1$0p&$: Puts a 1 at that cell number on the first line
.......
.......

......             If the number is a 0 go to the third line
?!\..............: Check if the next number is a 1, meaning we've reached the end of the sequence
v0\~:1=?           Go to the fourth line if so
>.....             Re-add the 0 and go back to the second line if not

......           While input:
......             Get the cell from the first line
......             If not 0: print the number
>?!;1-::0g?!nao:   Finally, print a newline and decrement the input




2

ハスク13 12 10バイト

-2バイトをありがとう@Zgarb!

-U2m%⁰İfŀ⁰

すべての整数が表示される場合に空のリストを印刷します。 します。オンライン試してください!

説明

-U2m%⁰İfŀ⁰  -- named argument ⁰, example with: 8
-           -- difference of
        ŀ⁰  -- | lowered range: [0,1,2,3,4,5,6,7]
            -- and
      İf    -- | Fibonacci sequence: [1,1,2,3,5,8,13,21,34,55,89,144,233,377…
   m%⁰      -- | map (modulo ⁰): [1,1,2,3,5,0,5,5,2,7,1,0,1,1…
 U2         -- | keep longest prefix until 2 adjacent elements repeats: [1,1,2,3,5,0,5,5,2,7,1,0,1]
            -- : [4,6]

を使用U2して、隣接するペアが繰り返されない最長のプレフィックスを取得できます。
ズガルブ


2

R、92 86バイト

6バイトを節約してくれた@Giuseppeに感謝します!

function(k,n=!!0:2){while(any((z=tail(n,2))-n[1:2]))n=c(n,sum(z)%%k);setdiff(1:k-1,n)}

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

かなり簡単な実装(以前のバージョンですが、同じコンセプト):

function(k,
         K=1:k-1,      #Uses default arguments to preset variables for legibility 
         n=c(0,1,1)){  #(wouldn't change byte-count to put them in the body of the function)
    while(any((z=tail(n,2))!=n[1:2])) #Do as long as first 2 elements are not identical to last 2 elements
        n=c(n,sum(z)%%k) #Built the fibonacci mod k sequence
    K[!K%in%n] #Outputs integers < k if not in sequence.
}


@ジュゼッペああsetdiff、良いアイデア!
プランナパス

1:k^2誰もが使用するアプローチを移植する70バイト
ジュゼッペ

2

Python 3、173 152 143 131バイト

f=lambda n,m,a=0,b=1:a%m if n<=0else f(n-1,m,b,a+b)
p=lambda n,i=2,y={0}:y^{*range(n)}if f(i,n)==1>f(i-1,n)else p(n,i+1,y|{f(i,n)})

@ovsに感謝します。

オンラインで試す

仕組みは?

最初の関数は2つのパラメーターmとnを取り、n番目のフィボナッチ数mod mを返します。2番目の関数は、フィボナッチ数mod kをループし、0と1が繰り返されるかどうかをチェックします。番号をリストに保存し、番号1〜nを含むリストと比較します。重複する番号は削除され、残りの番号が返されます。


これはヘッダーの一部であり、コードに含めることは必須ではありません。
マニッシュクン

わかった @ovs教えてくれてありがとう、私はそれを知らなかった。
マニッシュクン

1
131バイトset()。代わりに中括弧を使用してセットを作成し、比較を連鎖させます。
OVS


2

ルビー、47バイト

->n{a=b=1;[*1...n]-(1..n*n).map{a,b=b,a+b;a%n}}

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

同じロジックを使用していますが、これはGBのAnswerに基づいていません 。

説明:

->n{
  a=b=1;   # start sequence with 1,1
  [*1...n] # all the numbers from 1 to n-1 as an array
           # 0 is excluded as it should never be in the final answer 
  -  # set operation; get all items in the first set and not in the second
  (1..n*n).map{ # n squared times
    a,b=b,a+b;  # assign next fibonacci numbers 
    a%n         # return a fibonacci number mod n
  }    # Map to an array
}



1

Elixir148 144バイト

 fn x->Enum.to_list(1..x-1)--List.flatten Enum.take_while Stream.chunk(Stream.unfold({1,1},fn{p,n}->{rem(p,x),{n,p+n}}end),2),&Enum.sum(&1)!=1end

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

特に競争力のある答えではありませんが、ゴルフは本当に楽しかったです!Elixirは非常に読みやすい言語ですが、中央の文字の混乱についての説明が続きます。


この説明は、mod-fibonacciとその操作の2つのセクションに分かれています

Mod-fib:

Stream.unfold({1,1},fn{p,n}->{rem(p,x),{n,p+n}}end)

これは、フィボナッチmodの無限ストリームを返しますx。accumulator {1,1}で始まり、次の操作を無限に適用します:accumulatorを与え、ストリーム{p,n}に出力p mod xします。次に、アキュムレータをに設定し{n,p+n}ます。

残り:

fn x->                              Define a fxn f(x) that returns
  Enum.to_list(1..x-1)--            The numbers from 1..x-1 that are not in
  List.flatten                      The flattened list constructed by
    Enum.take_while                 Taking from mod-fib until
      Stream.chunk(                 A 2-size chunk
        Stream.unfold({1,1},fn{p,n}->{rem(p,x),{n,p+n}}end) (of mod fib)
        ,2)
      ,&Enum.sum(&1)!=1             sums to 1, representing [0,1] or [1,0]
end



1

JavaScript(ES6)、84バイト

f=(n,a=0,b=1,q=[...Array(n).keys()])=>a*b+a-1?f(n,b,(a+b)%n,q,q[b]=0):q.filter(x=>x)

1

Python 3、76バイト

def t(n,r=[1]):
 while n*n>len(r):r+=[sum(r[-2:])%n]
 return{*range(n)}-{*r}

これは、フィボナッチ数の可能な限り長いサイクル(n ^ 2)を単純に調べ、その時間内に発生するすべての数のリストを作成します。ロジックを簡素化するために、数値はnを法として格納されます。

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