似乎还没有比赛。
任务很简单。加上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
似乎还没有比赛。
任务很简单。加上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
Answers:
U
在代码中被替换为00
,可以为您节省一个字节吗?
4*
使用z
,并2+
用»
:)
{[+] 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
}
@(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
描述的过滤器过滤矢量中的数据,A
并B
创建过滤后的数据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]
。
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.
OOi:"t4*b+2+]x
这使用了OEIS的重复公式之一:
a(n)= 4 * a(n-1)+ a(n-2)+2
对于输入N,代码将迭代N次,这比所需次数多2倍。这是通过设置补偿0
,0
(而不是0
,2
)作为初始值,并且通过删除最后获得的值和显示前一个。
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)
@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。我想有一些技巧可以使它更短:-)
a(n){return--n<1?0:4*a(n)+a(n-1)+2;}
感谢@ChristianSievers,节省了一个字节。
使用OEIS中给出的公式:a(n) = 4*a(n-1)+a(n-2)+2, n>1
Gary Detlefs
a n|n>1=4*a(n-1)+a(n-2)+2|n<2=0
n<=1
整数的高尔夫球手说法是n<2
。同样,第二个条件也不必是第一个条件的否定(惯用语otherwise
就是True
),因此通常在打高尔夫球时1<2
会使用类似的东西。
Fibonacci[3Input[]-1]/2-1/2
感谢XNOR。感谢郑焕敏节省了5个字节。
(Fibonacci(3*n+2) - 1)/2
或写一个sumi 的时间更短吗?
(* Vincenzo Librandi, Mar 15 2014 *)
,因为它涉及OEIS。)
sum(DescTools::Fibonacci(3*(scan():2-1)))
scan()
:n
取自stdin。
scan():2-1
:从n
到生成整数2
,减到1
,产生n-1
到1
。
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)
desctools::fibonacci
那numbers::fibonacci
不能做什么呢?因为那雾要短一些。
bigz
,由于某种原因*apply
,函数的类将其转换为type raw
...
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的答案端口
n->fibonacci(3*n-1)\2
\
是整数商。