斐波那契指数


11

对于此挑战,您将输出一些数字之和的结果。这些数字是多少?好吧,您将得到输入(ab),它们是整数(正,负或零)a != b,,和a < b,并且ab(包括它们)内的每个整数将根据斐波那契数具有指数。这很混乱,所以这里有个例子:

Input: (-2, 2)
Output: -2**1 + (-1**1) + 0**2 + 1**3 + 2**5 =
          -2  +    -1   +   0  +   1  +   32 = 30

假设第一个斐波那契数由表示f(0),则公式为:

a**f(0) + ... + b**f(b-a+1) 

输入,处理,输出

为了阐明上述内容,以下是一些测试用例,输入的处理以及预期的输出:

Input: (1, 2)
Processing: 1**1 + 2**1
Output: 3

Input: (4, 8)
Processing: 4**1 + 5**1 + 6**2 + 7**3 + 8**5
Output: 33156

Input: (-1, 2)
Processing: -1**1 + 0**1 + 1**2 + 2**3
Output: 8

Input: (-4, -1)
Processing: -4**1 + -3**1 + -2**2 + -1**3
Output: -4

规则

  • 不允许出现标准漏洞

  • 指数必须符合斐波那契数列的顺序

  • 代码必须适用于上述测试用例

  • 仅输出需要返回

获奖标准

最短的代码胜出!


那么0这里的斐波那契数不包括在内吗?
FlipTack

0不是斐波那契数,而是输入的有效选择
Anthony Pham

6
33165或33156?
尼尔

@Neil我认为您是对的
Anthony Pham

上面的“ a f(0)+ ... + b f(b-a + 1)”是错误的,例如,对于a = 1和b = 2,它将是1 f(0)+2 f(2 )。我认为应该是f(0)+ ... + b f(ba); 此处f(0)= 0不为1
RosLuP

Answers:


2

05AB1E,9个字节

ŸDg!ÅFsmO

在线尝试!

Ÿ         # Push [a, ..., b].
 Dg!      # Calculate ([a..b].length())! because factorial grows faster than fibbonacci...
    ÅF    # Get Fibonacci numbers up to FACTORIAL([a..b].length()).
      s   # Swap the arguments because the fibb numbers will be longer.
       m  # Vectorized exponentiation, dropping extra numbers of Fibonacci sequence.
        O # Sum.

ab(EG [a..b].length() > 25)之间的较大差异在TIO上不起作用。

但这似乎比上面的平均答案更大。

效率低下,因为它计算的斐波纳契数列最高为n!,比计算答案所需的数要多,其中n的序列长度为a..b


5

Mathematica,38个字节 37个字节 31个字节

Sum[x^Fibonacci[x-#+1],{x,##}]&

这只是rahnema1移植到Mathematica的答案。以下是我的原始解决方案:

Tr[Range@##^Fibonacci@Range[#2-#+1]]&

说明:

##代表所有参数的序列,#代表第一个参数,#2代表第二个参数。当使用两个参数aand 调用时bRange[##]将给出列表,{a, a+1, ..., b}并且Range[#2-#+1]将给出相同长度的列表{1, 2, ..., b-a+1}。由于FibonacciListableFibonacci@Range[#2-#+1]将给出第一个b-a+1斐波那契数字的列表。由于Poweris Listable,在两个相等长度的列表上调用它将在列表上穿线。然后Tr取总和。

编辑:感谢Martin Ender,保存了1个字节。


3
您可以使用Range@##
马丁·恩德

1
现在不那么相关,但是原始方法可以提高3个字节为Tr[(r=Range@##)^Fibonacci[r-#+1]]&
格雷格·马丁

使用Range两次应该是一个危险信号。谢谢!
ngenisis'1

5

Python,49个字节

一个以ab为单独参数的递归lambda (您还可以将fibonacci x和的前两个数字设置为y您想要的任何值-不是有意的,而是一个不错的功能):

f=lambda a,b,x=1,y=1:a<=b and a**x+f(a+1,b,y,x+y)

在线尝试!(包括测试套件)

欢迎打高尔夫球。


为什么-~a而不是简单地a+1?我认为-~a这取决于机器。
泰特斯(Titus),

4

Perl 6的32 30个字节

{sum $^a..$^b Z**(1,&[+]...*)}

$^a$^b是函数的两个参数;$^a..$^b是从$^a到的数字范围,该范围以Fibonacci序列$^b乘幂压缩。Z**1, &[+] ... *

感谢Brad Gilbert削减了两个字节。


(1,&[+]...*)是短一个字节,Z**不需要空格。
布拉德·吉尔伯特b2gills

@ BradGilbertb2gills很酷,我不知道斐波那契数列可以这样表达。
肖恩

实际上它有效,因为&infix:<+>可以接受0,1或2个参数。(&[+]是一种简短的写作方式&infix:<+>)。WhateverCode * + *恰好接受2个参数。(&[0]() == 0因此您必须从1那里开始该序列)
Brad Gilbert b2gills


3

Pyke,11个字节

h1:Foh.b^)s

在这里尝试!

h1:         -   range(low, high+1)
   F     )  -  for i in ^:
    oh      -     (o++)+1
      .b    -    nth_fib(^)
        ^   -   i ** ^
          s - sum(^)

3

JavaScript(ES7),42个字节

f=(a,b,x=1,y=1)=>a<=b&&a**x+f(a+1,b,y,x+y)

@FlipTack出色的Python答案的直接端口。


不错,JavaScript竟然更短!:)
FlipTack

3

Haskell,35个字节

f=scanl(+)1(0:f);(?)=sum.zipWith(^)

用法:

$ ghc fibexps.hs -e '[4..8]?f'
33156

您可以将函数o转换为中缀运算符,例如a#b=sum...
nimi

像a ... b一样考虑了中缀,但阅读了接受一元(ℤ,ℤ)→ℕ的要求
Roman Czyborra

许多其他答案有两个不同的论点,所以我认为很好。
nimi

好吧,这已经使我们达到ECMAscript7 lambda的水平。但是,如果我们被允许进料(a,b)a?b那么为什么不能让我们把它作为直接准备[a..b]?f(?)=sum.zipWith(^)
Roman Czyborra

我认为这太过分了。输入的是两个数字(不一定是一对,两个单独的参数都可以),但是您要向主函数提供一个数字列表和一个函数。
nimi

2

MATL,23字节

&:ll&Gw-XJq:"yy+]JQ$h^s

在线尝试!验证所有测试用例

&:      % Binary range between the two implicit inputs: [a a+1 ... b] 
ll      % Push 1, 1. These are the first two Fibonacci numbers
&G      % Push a, b again
w-      % Swap, subtract: gives b-a
XJ      % Copy to cilipboard J
q:      % Array [1 2 ... b-a-1]
"       % For each (repeat b-a-1 times)
  yy    %    Duplicate the top two numbers in the stack
  +     %    Add
]       % End
J       % Push b-a
Q       % Add 1: gives b-a+1
$       % Specify that the next function takes b-a+1 inputs
h       % Concatenate that many elements (Fibonacci numbers) into a row vector
^       % Power, element-wise: each entry in [a a+1 ... b] is raised to the
        % corresponding Fibonacci number
s       % Sum of array. Implicitly display

1

R,51个字节

匿名函数。

function(a,b)sum((a:b)^numbers::fibonacci(b-a+1,T))


0

Ruby,46个字节

->a,b{n=s=0;m=1;a.upto(b){|x|s+=x**n=m+m=n};s}

这里没有什么特别聪明或原创的东西。抱歉。


对于我来说ℤ.upto(ℤ)不是Ruby的使用者,该方法很好地提醒了Ruby的全对象行为美。进一步打高尔夫球的代码留给母语为Ruby的人练习。您扫描了codegolf.stackexchange.com/questions/363/…吗?
Roman Czyborra

0

Java 7,96字节

打高尔夫球:

int n(int a, int b){int x=1,y=1,z=0,s=0;while(a<=b){s+=Math.pow(a++,x);z=x+y;x=y;y=z;}return s;}

取消高尔夫:

int n(int a, int b)
{
    int x = 1, y = 1, z = 0, s = 0;
    while (a <= b)
    {
        s += Math.pow(a++, x);
        z = x + y;
        x = y;
        y = z;
    }

    return s;
}

0

R,57个字节

x=scan();sum((x[1]:x[2])^numbers::fibonacci(diff(x)+1,T))

非常简单。gmp::fibnum是一个较短的内置函数,但不支持将整个序列返回到n,这需要numbers::fibonacci通过添加参数来实现T

首先,我有一个更棘手的解决方案,gmp::fibnum该解决方案比该解决方案长2个字节。

x=scan();for(i in x[1]:x[2])F=F+i^gmp::fibnum((T<-T+1)-1);F

使用匿名函数而不是scan()节省6个字节;查看我发布的解决方案。
rturnbull

是的,我很傻。
JAD

0

dc,56字节

?sf?sa0dsbsg1sc[lblcdlfrdsb^lg+sg+sclf1+dsfla!<d]dsdxlgp

[1,30]51秒内完成输入。一次执行两个独立行上的两个输入,负号前加一个下划线_)而不是破折号(即-4输入_4)。


0

PHP,77 75字节

for($b=$argv[$$x=1];$b<=$argv[2];${$x=!$x}=${""}+${1})$s+=$b++**$$x;echo$s;

从命令行参数获取边界。用运行-nr。再次
展示PHP的变量变量(以及从中发现的内容)

分解

for($b=$argv[$$x=0}=1]; # $"" to 1st Fibonacci and base to 1st argument
    $b<=$argv[2];           # loop $b up to argument2 inclusive
    ${$x=!$x}                   # 5. toggle $x,             6. store to $1/$""
        =${""}+${1}             # 4. compute next Fibonacci number
)
    $s+=$b++**                  # 2. add exponential to sum,    3. post-increment base
        $$x;                    # 1. take current Fibonacci from $""/$1 as exponent
echo$s;                     # print result

FlipTack移植到PHP的答案有70个字节:

function f($a,$b,$x=1,$y=1){return$a>$b?0:$a**$x+f($a+1,$b,$y,$x+$y);}

0

公理,65字节

f(a,b)==reduce(+,[i^fibonacci(j)for i in a..b for j in 1..b-a+1])

测试代码和结果

(74) -> f(1,2)
   (74)  3
                                                   Type: Fraction Integer
(75) -> f(4,8)
   (75)  33156
                                                   Type: Fraction Integer
(76) -> f(-1,2)
   (76)  8
                                                   Type: Fraction Integer
(77) -> f(-4,-1)
   (77)  - 4
                                                   Type: Fraction Integer
(78) -> f(3,1)
   >> Error detected within library code:
   reducing over an empty list needs the 3 argument form
    protected-symbol-warn called with (NIL)

0

PowerShell,67字节

$e=1;$args[0]..$args[1]|%{$s+=("$_*"*$e+1|iex);$e,$f=($e+$f),$e};$s

在线尝试!

找到了一种更好的方法来执行序列,但是powershell不能与其他语言相比:)

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.