搭载顺序


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...

给定任务后,您可以将其n计算P(n)为浮点数或适当的分数。

这是,因此以字节为单位的最短代码获胜。

如果有人可以找到序列的名称,请相应地编辑帖子。

现任领导人:MATL和Jelly(均为15字节)。


我们可以从索引0开始吗? P(0)=1...
nimi

3
请问您为该序列指定的名称背后的原因是什么?
约翰·德沃夏克

@JanDvorak似乎数字在互相“ pi带”。
clismique

@nimi是的,您被允许。
clismique

Answers:


6

Python 2中,40 39个字节。

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

给定True而不是1,如果不允许这样做,我们可以使用42个字节:

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

它的工作方式非常简单,唯一的技巧是使用.0+将结果转换为浮点数。


您可以通过删除之间的空间节省一个字节x<4or
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.642857142857143。我0, 1, 1以修复!!基于0的索引开始序列。

编辑:@xnor找到了一种从基于0的索引切换到基于1的索引的方法,而无需更改字节数。


1
击败直接递归定义的好方法。我认为您可以通过初始化转到1索引(0,1,1)
xnor

4

Ruby,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
  )[ $_ ]
}

这将从输入3开始返回RatRational),直到结果开始的分母大于可以容纳64位整数的分母,然后开始返回Num s(浮点数)。它会返回
的最后一个老鼠P(11) == 8832072277617 / 2586200337022

如果您希望它返回有理数而不是浮点数,则可以将其交换为以下内容,这将返回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

Cheddar,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)
Ismael Miguel

@IsmaelMiguel谢谢。坦率地说,我不知道不同Javascript之间的区别:D
Beta Decay

为了您的利益,在大多数挑战中,您只需要了解“大箭头符号”即可使用它,无需使用关键字即可创建函数function。该位P=n=>[...]正在创建一个带有1个参数(n)的匿名函数。同样,在ES6上,返回值是隐式的。因此,P=n=>5是一个始终返回的函数5{}如果您有多个声明(例如:),则只需要将主体括起来P=n=>{alert(1);console.log(1)}。由于您只有1个(大)语句(三元运算符),因此您可以忘记{}
伊斯梅尔·米格尔

@IsmaelMiguel谢谢,这将很有用:D
Beta Decay

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

在线尝试!

Luis Mendo节省了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 '16

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.