对前n个偶数斐波纳契数求和


19

似乎还没有比赛。

任务很简单。加上n斐波那契数列的第一个偶数,然后输出结果。

这是由OEIS A099919提供的,除了序列以fib(1) = 0代替移位了一个fib(1) = 1

这是代码高尔夫。最低字节数获胜。

例子

n sum
1 0
2 2
3 10
4 44
5 188
6 798
7 3382
8 14328
9 60696

有关



@EasterlyIrk测试用例暗示了后者,但应明确说明。
Mego

@Mego是的,我想了很多。
Rɪᴋᴇʀ

9
请不要这么快就接受答案。仅仅一个小时,高尔夫球手的答案就可以出现。编辑:我看到现在已经有一个较短的答案尚未被接受。
Rɪᴋᴇʀ

6
通常要等待至少一周才能接受答案,因为许多人将其解释为挑战不再活跃的标志。
Zgarb

Answers:


8

绿洲8 7 5字节

@ETHProductions节省了1个字节,@ Adnan节省了2个字节!

zc»+U

在线尝试!

说明:

这使用与我的MATL答案相同的重复公式。


1
Oasis的info.txt说U在代码中被替换为00,可以为您节省一个字节吗?
ETHproductions

@ETHproductions谢谢!我忘了
Luis Mendo

1
真好!您可以替换4*使用z,并2+»:)
阿德南

@Adnan谢谢!我真的应该阅读文档:-)
Luis Mendo

17

Python,33个字节

c=2+5**.5
lambda n:(7-c)*c**n//20

在线尝试

魔术配方!


3
天啊。我花了很长时间,才意识到为什么要在第二行中“注释” 20:P
Theo

@xnor,对此魔术公式有任何参考吗?
TheChetan

@TheChetan:可能a(n) = (-10 + (5-3*sqrt(5))*(2-sqrt(5))^n + (2+sqrt(5))^n*(5+3*sqrt(5)))/20(Colin Barker,2016年11月26日)来自OEIS页面
Titus,


7

实际上,6个字节

r3*♂FΣ

在线尝试!

说明:

每第三个斐波纳契数(从开始F_0 = 0)为偶数。因此,第一n连斐波那契数是F_{i*3}用于i[0, n)

r3*♂FΣ
r       [0, n)
 3*     multiply each element by 3
   ♂F   retrieve the corresponding element in the Fibonacci sequence
     Σ  sum

7

JavaScript(ES6),27个字节

f=x=>x>1&&4*f(x-1)+f(x-2)+2

递归救援!这使用OEIS页面上的公式之一:

f(n <1)= 0,f(n)= 4 * a(n + 1)+ a(n)+2

(但因挑战而改变了一位)



4

Perl 6的 38个35  32字节

{[+] grep(*%%2,(1,&[+]...*))[^($_-1)]}

尝试一下

{[+] grep(*%%2,(0,1,*+*...*))[^$_]}

尝试一下

{[+] (0,1,*+*...*)[3,6...^$_*3]}

尝试一下

展开:

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

  [+]                       # reduce with 「&infix:<+>」

    ( 0, 1, * + * ... * )\  # fibonacci sequence with leading 0

    [ 3, 6 ...^ $_ * 3 ]    # every 3rd value up to
                            # and excluding the value indexed by
                            # the input times 3

}

3

八度36 35 33字节

@(n)filter(2,'FAD'-69,(1:n)>1)(n)

在线尝试!

说明

这个匿名函数将差分方程实现a(n) = 4*a(n-1)+a(n-2)+2递归过滤器

Y = filter(B,A,X)使用矢量X描述的过滤器过滤矢量中的数据,AB创建过滤后的数据Y。过滤器是标准差方程的“直接形式II换位”实现:

a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na)

在我们的情况下A = [1 -4 -1]B = 2和,输入x应该是1的向量,其结果将显示为输出的最后一个条目y。但是,我们将0输入设置为第一个值,以便0根据需要在输出中出现一个首字母。

'FAD'-69是产生系数向量的一种较短的方法A = [1 -4 -1]; 并(1:n)>1产生输入向量x = [0 1 1 ... 1]


3

dc25 22字节

9k5v1+2/3?*1-^5v/0k2/p

在线尝试!

或将程序保存在文件中并通过键入以下内容运行

dc -f *filename*

程序在stdin上接受非负整数n,并在stdout上输出前n个偶数Fibonacci数的和。(按照OP的示例,斐波那契数列从0开始。)


该程序使用公式(F(3n-1)-1)/ 2作为前n个偶数斐波那契数的总和,其中F是通常的斐波那契函数,由F(0)= 0,F(1)= 1,当n> = 2时,F(n)= F(n-2)+ F(n-1)


dc是基于堆栈的计算器。以下是详细说明:

9k  # Sets the precision to 9 decimal places (which is more than sufficient).

5v  # Push the square root of 5

1+  # Add 1 to the number at the top of the stack.

2/  # Divide the number at the top of the stack by 2.

此时,数字(1 + sqrt(5))/ 2在堆栈的顶部。

3   # Push 3 on top of the stack.

?   # Read a number from stdin, and push it.

\*  # Pop two numbers from the stack, multiply them, and push the product

1-  # Subtract 1 from the number at the top of the stack.

此时,3n-1在堆栈的顶部(其中n是输入),而(1 + sqrt(5))/ 2在堆栈的顶部。

^   # Pop two numbers from the stack (x, then y), compute the power y^x, and push that back on the stack.

5v/ # Divide the top of the stack by sqrt(5).

此时,堆栈顶部的数字为((((1 + sqrt(5))/ 2)^(3n-1))/ sqrt(5)。与此数字最接近的整数是F(3n-1)。请注意,F(3n-1)始终是奇数。

0k # Change precision to 0 decimal places.

2/ # Divide the top of the stack by 2, truncating to an integer.

p # Print the top of the stack on stdout.

3

Mathematica, 27个 21字节

感谢xnor指出替代公式,alephalpha纠正起始索引

Fibonacci[3#-1]/2-.5&

1
(Fibonacci(3*n+2)-1)/2公式可能会更短吗?
xnor

2

MATL15 14字节

OOi:"t4*b+2+]x

在线尝试!

说明

这使用了OEIS的重复公式之一:

a(n)= 4 * a(n-1)+ a(n-2)+2

对于输入N,代码将迭代N次,这比所需次数多2倍。这是通过设置补偿00(而不是02)作为初始值,并且通过删除最后获得的值和显示前一个。

OO      % Push two zeros as initial values of a(n-2), a(n-1)
i       % Input N
:"      % Do this N times
  t     %   Duplicate a(n-1)
  4*    %   Multiply by 4
  b+    %   Bubble up a(n-2) and add to 4*a(n-1)
  2+    %   Add 2. Now we have 4*a(n-1)+a(n-2)+2 as a(n), on top of a(n-1)
]       % End
x       % Delete last value, a(n). Implicitly display the remaining value, a(n-1)

2

批处理,80字节

@set/at=x=0,y=1
@for /l %%i in (2,1,%1)do @set/az=x+y,y=z+x,t+=x=y+z
@echo %t%

利用每个第三个斐波那契数都是偶数的事实,并一次计算三个(实际上一次计算多个会更容易,因为您不必交换值)。我尝试了该(Fibonacci(3*n+2)-1)/2公式,但实际上要长几个字节(t+=事实证明,这在代码大小方面非常有效)。


2

C,82 38 36字节

@BrainSteel节省了2个字节

OEIS页面上的公式使它变得更短:

a(n){return--n<1?0:4*a(n)+a(n-1)+2;}

在线尝试!

82个字节:

x,s,p,n,c;f(N){s=0;p=n=1;c=2;while(n<N){if(~c&1)s+=c,n++;x=p+c;p=c;c=x;}return s;}

第一个版本为75个字节,但该函数不可重用,除非您始终f以大于N上一个调用的方式进行调用:-)

x,s,p=1,n=1,c=2;f(N){while(n<N){if(~c&1)s+=c,n++;x=p+c;p=c;c=x;}return s;}

我的第一个答案在这里。没有检查任何其他答案或OEIS。我想有一些技巧可以使它更短:-)


1
您可以通过将内容a(n){return--n<1?0:4*a(n)+a(n-1)+2;}
拖曳

1

Haskell(32 31字节)

感谢@ChristianSievers,节省了一个字节。

使用OEIS中给出的公式:a(n) = 4*a(n-1)+a(n-2)+2, n>1Gary Detlefs

a n|n>1=4*a(n-1)+a(n-2)+2|n<2=0


n<=1整数的高尔夫球手说法是n<2。同样,第二个条件也不必是第一个条件的否定(惯用语otherwise就是True),因此通常在打高尔夫球时1<2会使用类似的东西。
Christian Sievers

@ChristianSievers确实n <2是一个明显的改进,谢谢。第二个也可以,尽管在这种情况下并不能为我节省任何费用。我仍在学习Haskell,却没有意识到我可以拥有这样的后卫。谢谢!
Dylan Meeus

1

Mathematica,32 27字节

Fibonacci[3Input[]-1]/2-1/2

感谢XNOR。感谢郑焕敏节省了5个字节。


当然Mathematica具有斐波那契,并且做一个(Fibonacci(3*n+2) - 1)/2或写一个sumi 的时间更短吗?
xnor

@JungHwanMin这不是窃;它提到了OEIS页面。另外,这不是社区Wiki的候选人。请参阅应如何使用社区Wiki?
丹尼斯,

@devRichter很抱歉取消删除您的帖子,但有必要进行对话。如果您希望将其删除,请告诉我,我会将对话内容移到聊天室。
丹尼斯,

@Dennis仍然,我认为应该明确地赞扬Vincenzo Librandi-(不小心删除了我的最后评论...可以不删除吗?)对于社区帖子的建议,我表示更正。
JungHwan Min

我的意思是在帖子中提及他的名字...(或在帖子中包括Mathematica评论(* Vincenzo Librandi, Mar 15 2014 *),因为它涉及OEIS。)
JungHwan Min

1

R,42个字节

非递归解决方案,与@rtrunbull 这里的早期解决方案形成对比。

for(i in 1:scan())F=F+gmp::fibnum(3*i-3);F

使用Fibonacci序列的每个第三值都是偶数的属性。还滥用F默认情况下定义为的事实FALSE=0,从而使其成为将值添加到的基础。


1

R,42 41字节

sum(DescTools::Fibonacci(3*(scan():2-1)))

scan()n取自stdin。

scan():2-1:从n到生成整数2,减到1,产生n-11

3*(scan():2-1) :乘以3,因为第三个斐波纳契数为偶数。

DescTools::Fibonacci(3*(scan():2-1)):返回这些斐波那契数字(即3通过(n-1)*3)。

sum(DescTools::Fibonacci(3*(scan():2-1))) :求和结果。

以前,我使用OEIS的公式之一获得了这种无趣的解决方案:

a=function(n)`if`(n<2,0,4*a(n-1)+a(n-2)+2)

我成功地匹配了您的字节数,而没有递归:)
JAD

@JarkoDubbeldam不错!我也放弃了递归,并做了一个字节的改进:)
rturnbull

好的,desctools::fibonaccinumbers::fibonacci不能做什么呢?因为那雾要短一些。
JAD

哦,没关系,找到了。亲爱的,我发现的其他实现不支持一次要求多个数字。
JAD

1
@JarkoDubbeldam是的,“ gmp :: fibnum”返回type的对象bigz,由于某种原因*apply,函数的类将其转换为type raw...
rturnbull


1

PHP,73 70字节

for(${0}=1;$i++<$argv[1];$$x=${0}+${1})${$x^=1}&1?$i--:$s+=$$x;echo$s;

展示 变量变量。上)。用运行-nr

分解

for(${0}=1;         # init first two fibonaccis (${1}=NULL evaluates to 0 in addition)
                    # the loop will switch between $0 and $1 as target.
    $i++<$argv[1];  # loop until $i reaches input
    $$x=${0}+${1}       # 3. generate next Fibonacci
)
    ${$x^=1}            # 1. toggle index (NULL => 1 => 0 => 1 ...)
    &1?$i--             # 2. if current Fibonacci is odd, undo increment
    :$s+=$$x;           #    else add Fibonacci to sum
echo$s;             # print result

在PHP中,数字是完全有效的变量名。
但是,对于文字,它们需要大括号;即${0}不是$0

36字节,O(1)

<?=(7-$c=2+5**.5)*$c**$argv[1]/20|0;

xnor的答案端口


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.