ピギーバックシーケンス


14

私は最近自分のシーケンスを作成し(ピギーバックシーケンスと呼ばれます)、次のように動作します。

P(1)P(2)およびP(3)= 1

すべてのP(n)where n>3について、シーケンスは次のように機能します。

P(n) = P(n-3) + P(n-2)/P(n-1)

したがって、シーケンスを続行します。

P(4)= 1 + 1/1=2

P(5)= 1 + 1/2= 3/2 =1.5

P(6)= 1 + 2/(3/2)= 7/3 =2.33333...

P(7)= 2 + (3/2)/(7/3)= 37/14=2.6428571428...

P(8)= 3/2 + (7/3)/(37/14)= 529/222 =2.3828828828...

あなたのタスクは、与えられたとき、浮動小数点数または(im)適切な分数としてn計算しP(n)ます。

これはなので、バイト単位の最短コードが優先されます。

誰かがシーケンスの名前を見つけられる場合は、それに応じて投稿を編集してください。

現在のリーダー:MATLおよびJelly(両方とも15バイト)。


インデックス0から始められますか? P(0)=1...
nimi

3
このシーケンスに付けた名前の背後にある理論的根拠をお願いできますか?
ジョンドヴォルザーク

@JanDvorak数字がお互いに「便乗」しているようです。
clismique

@nimiはい、許可されています。
clismique

回答:


6

パイソン2、40 39バイト。

f=lambda x:x<4or.0+f(x-3)+f(x-2)/f(x-1)

与えTrue、これは我々が42バイトのためにこれを持つことができます許可されていない場合、代わりに1:

f=lambda x:.0+(x<4or f(x-3)+f(x-2)/f(x-1))

動作方法は非常に簡単で、唯一のトリックは.0+結果をフロートにキャストすることです。


x<4との間のスペースを削除することで1バイトを節約できますor
acrolith

Python 2では、f(x-1.)フロートへのキャストに使用できます。Python 3では、まったくキャストする必要はありません。
デニス

5

Haskel、32バイト

(a#b)c=a:(b#c)(a+b/c)
((0#1)1!!)

使用例: ((0#1)1!!) 7-> 2.6428571428571430, 1, 1修正するためにシーケンスを開始します!!の0ベースのインデックス。

編集:@xnorは、バイトカウントを変更せずに、0ベースのインデックスから1ベースのインデックスに切り替える方法を見つけました。


1
直接再帰の定義を破るための素晴らしい方法。を初期化することで1-indexedに移行できると思います(0,1,1)
xnor

4

ルビー、34バイト

Rubyはデフォルトで整数除算を使用するため、分数を使用する方が短いことがわかります。ゴルフの提案を歓迎します。

f=->n{n<4?1r:f[n-3]+f[n-2]/f[n-1]}

4

Perl 6の 25の  23バイト

{(0,1,1,1,*+*/*...*)[$_]}

{(0,1,1,*+*/*...*)[$_]}

説明:

# bare block lambda with implicit parameter 「$_」
{
  (
    # initial set-up
    # the 「0」 is for P(0) which isn't defined
    0, 1, 1, 1,

    # Whatever lambda implementing the algorithm
    * + * / *
    # { $^a + $^b / $^c }

    # keep using the lambda to generate new values until
    ...

    # Whatever (Forever)
    *

   # get the value indexed by the argument
  )[ $_ ]
}

これは、結果が64ビット整数に収まるよりも大きい分母を持つようになるまで、3から始まる入力に対してRatRational)を返します。 Num(浮動小数点)をます。
最後のネズミそれが返すはP(11) == 8832072277617 / 2586200337022

Rationalを返す場合浮動小数点数ではなく数値、FatRatを返す次の要素に交換できます。

{(0.FatRat,1,1,*+*/*...*)[$_]}

テスト:

#! /usr/bin/env perl6
use v6.c;
use Test;

my &piggyback = {(0,1,1,*+*/*...*)[$_]}
# */ # stupid highlighter no Perl will ever have C/C++ comments

my @test = (
  1, 1, 1, 2,
  3/2, 7/3, 37/14,
  529 / 222,
  38242 / 11109,
  66065507 / 19809356,
  8832072277617 / 2586200337022,
);

plan +@test;

for 1..* Z @test -> ($input,$expected) {
  cmp-ok piggyback($input), &[==], $expected, $expected.perl;
}


3

MATL、15バイト

llli3-:"3$t/+]&

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

説明

lll       % Push 1, 1, 1
i         % Take input n
3-:       % Pop n and push range [1 2 ... n-3] (empty if n<4)
"         % For each
  3$t     %    Duplicate the top three numbers in the stack
  /       %    Pop the top two numbers and push their division
  +       %    Pop the top two numbers and push their addition
]         % End
&         % Specify that the next function, which is implicit display, will take
          % only one input. So the top of the stack is displayed

2

チェダー、31バイト

n P->n<4?1:P(n-3)+P(n-2)/P(n-1)

使用されていないバージョンは非常に明確なので、説明する必要はありません。

n P->
  n < 4 ? 1 : P(n-3) + P(n-2) / P(n-1)

基本的には、関数の引数の後に、関数自体に設定される使用する変数を指定できます。どうして?この関数は末尾呼び出しに最適化されるか、少なくともそうすべきだからです。


2

Javascript(ES6)、31バイト

P=n=>n<4?1:P(n-3)+P(n-2)/P(n-1)

シンプルな機能。

P=n=>n<4?1:P(n-3)+P(n-2)/P(n-1)

var out = '';

for (var i=1;i <= 20;i++) {
out +='<strong>'+i+':</strong> '+P(i)+'<br/>';
}

document.getElementById('text').innerHTML = out;
div {
font-family: Arial
}
<div id="text"></div>


なぜES6ではありませんか?メートル単位のバイトを節約します。
イスマエルミゲル

このように:P=n=>n<4?1:P(n-3)+P(n-2)/P(n-1)
イスマエルミゲル

@IsmaelMiguelありがとう。D:率直に言って、私は別のジャバスクリプトの違いについては考えていない
ベータ崩壊

ほとんどの場合、あなたにとって有利なのは、キーワードを使用せずに関数を作成できる「ビッグアロー表記法」を知っていることだけですfunction。ビットP=n=>[...]は、1つのパラメーター(n)を取る匿名関数を作成しています。また、ES6では、返品は暗黙的です。したがって、P=n=>5は常にを返す関数です5{}複数のステートメントがある場合にのみ、本文を囲む必要があります(例:)P=n=>{alert(1);console.log(1)}。1つの(大きな)ステートメント(三項演算子)しかないため、を忘れることがあり{}ます。
イスマエルミゲル

便利に来る@IsmaelMiguelおかげで、:D
ベータ崩壊

2

05AB1E18 17バイト

3Ld                # push list [1,1,1]
   ¹ÍG         }   # input-3 times do
      D3£          # duplicate list and take first 3 elements of the copy
         R`        # reverse and flatten
           /+      # divide then add
             ¸ì    # wrap in list and prepend to full list
                ¬  # get first element and implicitly print

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

ルイスメンドーのおかげで1バイト節約



1

ゼリー、15 バイト

ạ2,1,3߀÷2/SµḊ¡

オンラインでお試しください!またはすべてのテストケースを確認します

使い方

ạ2,1,3߀÷2/SµḊ¡  Main link. Argument: n (integer)

             Ḋ   Dequeue; yield [2, ..., n].
            µ ¡  If the range is non-empty (i.e., if n > 1), execute the chain to
                 the left. If n is 0 or 1, return n.
                 Note that P(3) = P(0) + P(2)/P(1) if we define P(0) := 0.
ạ2,1,3           Take the absolute difference of n and 2, 1, and 3.
                 This gives [0, 1, 1] if n = 2, and P(0) + P(1)/P(1) = 0 + 1/1 = 1.
      ߀         Recursively apply the main each to each difference.
        ÷2/      Perform pairwise division.
                 This maps [P(n-2), P(n-1), P(n-3)] to [P(n-2)/P(n-1), P(n-3)].
           S     Sum, yielding P(n-2)/P(n-1) + P(n-3).

1

R、53 47バイト

f=function(N)ifelse(N>3,f(N-3)+f(N-2)/f(N-1),1)

この答えはかなりきれいな機能を利用しましたifelseifelse(Condition, WhatToDoIfTrue, WhatToDoIfNot)


1
return()コード内のを取り除くことができるはずです。しかし、あなたはまた、仕事へのご再帰ためで関数の名前を指定する必要がある
user5957401

0

Mathematica、36バイト

P@n_:=If[n<4,1,P[n-3]+P[n-2]/P[n-1]]

最初のいくつかの用語は次のとおりです。

P /@ Range[10]
{1, 1, 1, 2, 3/2, 7/3, 37/14, 529/222, 38242/11109, 66065507/19809356}

0

Dyalog APL、25バイト

⊃{1↓⍵,⍎⍕' +÷',¨⍵}⍣⎕⊢0 1 1

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