# Fibonacci Exponents

For this challenge, you are to output the result of the sum of some numbers. What are these numbers? Well, you are given input, (a, b), which are integers (positive, negative, or zero) , a != b, and a < b , and each integer within a and b (including them) will have exponents according to the Fibonacci numbers. That's confusing so here's an example:

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


Given that the first Fibonacci number is represented by f(0), the formula is:

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


Input, Processing, Output

To clarify the above, here are some test cases, the processing of the input, and the expected outputs:

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


Rules

• No standard loopholes allowed

• Exponents must be in order according to Fibonacci series

• Code must work for above test cases

• Only the output needs to be returned

Winning Criteria

Shortest code wins!

• So 0 is not included in the fibonacci numbers here? – FlipTack Dec 31 '16 at 14:58
• 0 is not a Fibonacci number but is a valid choice for input – Anthony Pham Dec 31 '16 at 14:59
• 33165 or 33156? – Neil Dec 31 '16 at 15:29
• @Neil I think you're right – Anthony Pham Dec 31 '16 at 18:47
• This above "af(0) + ... + bf(b-a+1) " it is wrong, for example for a=1 and b=2 it would be 1f(0)+2f(2). I think would be af(0) + ... + bf(b-a); here f(0)=0 not 1 – RosLuP Apr 17 '17 at 15:16

# 05AB1E, 9 bytes

ŸDg!ÅFsmO


Try it online!

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


Doesn't work on TIO for large discrepancies between a and b (E.G. [a..b].length() > 25).

But it does seem to work for bigger numbers than the average answer here.

Inefficient, because it calculates the fibonacci sequence up to n!, which is more than is needed to compute the answer, where n is the length of the sequence of a..b.

## Mathematica, 38 bytes37 bytes 31 bytes

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


This is just rahnema1's answer ported to Mathematica. Below is my original solution:

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


Explanation:

## represents the sequence of all the arguments, # represents the first argument, #2 represents the second argument. When called with two arguments a and b, Range[##] will give the list {a, a+1, ..., b} and Range[#2-#+1] will give the list of the same length {1, 2, ..., b-a+1}. Since Fibonacci is Listable, Fibonacci@Range[#2-#+1] will give list of the first b-a+1 Fibonacci numbers. Since Power is Listable, calling it on two lists of equal length will thread it over the lists. Then Tr takes the sum.

Edit: Saved 1 byte thanks to Martin Ender.

• You can use Range@##. – Martin Ender Dec 31 '16 at 19:16
• Not as relevant now, but the original approach can be improved 3 bytes to Tr[(r=Range@##)^Fibonacci[r-#+1]]&. – Greg Martin Jan 5 '17 at 18:40
• Using Range twice should have been a red flag. Thanks! – ngenisis Jan 5 '17 at 20:12

# Python, 49 bytes

A recursive lambda which takes a and b as separate arguments (you can also set the first two numbers of fibonacci, x and y, to whatever you want - not intentional, but a nice feature):

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


Try it online! (includes test suite)

Golfing suggestions welcome.

• Why -~a and not simply a+1? I think -~a is machine dependant. – Titus Jan 6 '17 at 1:19

# Perl 6, 32 30 bytes

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


$^a and $^b are the two arguments to the function; $^a..$^b is the range of numbers from $^a to $^b, which is zipped with exponentiation by Z** with the Fibonacci sequence, 1, &[+] ... *.

Thanks to Brad Gilbert for shaving off two bytes.

• (1,&[+]...*) is one byte shorter, and the space after Z** isn't needed. – Brad Gilbert b2gills Jan 1 '17 at 6:47
• @BradGilbertb2gills Cool, I had no idea the Fibonacci sequence could be expressed that way. – Sean Jan 1 '17 at 18:55
• Actually it works because &infix:<+> can accept 0,1 or 2 arguments. (&[+] is a short way of writing &infix:<+>). The WhateverCode * + * accepts exactly 2 arguments. (&[0]() == 0 so you have to have the 1 there to start off the sequence) – Brad Gilbert b2gills Jan 1 '17 at 19:19

# Maxima , 32 bytes

f(a,b):=sum(x^fib(x-a+1),x,a,b);


## Pyke, 11 bytes

h1:Foh.b^)s


Try it here!

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


## JavaScript (ES7), 42 bytes

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


Straightforward port of @FlipTack's excellent Python answer.

• Nice, turned out even shorter in JavaScript! :) – FlipTack Dec 31 '16 at 22:15

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


Usage:

$ghc fibexps.hs -e '[4..8]?f' 33156  • You can turn the function o into an infix operator, like a#b=sum.... – nimi Jan 2 '17 at 23:49 • Had considered infix like a…b but read the requirement to accept unary (ℤ,ℤ)→ℕ – Roman Czyborra Jan 4 '17 at 11:11 • Many other answers take two separate arguments, so I think it's fine. – nimi Jan 4 '17 at 20:29 • Alrightie already, that brings us up to par with the ECMAscript7 lambda. But if we are allowed to feed (a,b) as a?b then why aren't we allowed to prepare it as immediate [a..b]?f onto (?)=sum.zipWith(^)? – Roman Czyborra Jan 5 '17 at 9:52 • I think this goes too far. The input are two numbers (not necessarily as a pair, two separate arguments will do), but you're feeding a list of numbers and a function to your main function. – nimi Jan 6 '17 at 1:13 # MATL, 23 bytes &: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
]       % 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  # R, 51 bytes An anonymous function. function(a,b)sum((a:b)^numbers::fibonacci(b-a+1,T))  # Jelly, 13 bytes ạµ1+⁸Ð¡0 r*çS  Try it online! • Nice, the only other answer I've found where an input of f(1,25) works ;). +1 – Magic Octopus Urn Jan 5 '17 at 19:21 ## Ruby, 46 bytes ->a,b{n=s=0;m=1;a.upto(b){|x|s+=x**n=m+m=n};s}  Nothing particularly clever or original to see here. Sorry. • For me nonspeaker of Ruby, the ℤ.upto(ℤ) method is a nice reminder of Ruby's all-object behavior beauty. Further golfing the code is left as an exercise to native Ruby speakers. Have you scanned codegolf.stackexchange.com/questions/363/… yet? – Roman Czyborra Jan 5 '17 at 10:14 # Java 7, 96 bytes Golfed: 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;}  Ungolfed: 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; }  # R, 57 bytes x=scan();sum((x[1]:x[2])^numbers::fibonacci(diff(x)+1,T))  Pretty straightforward. gmp::fibnum is a shorter built-in, but it doesn't support returning the entire sequence up to n, which numbers::fibonacci does by adding the argument T. First I had a more tricky solution with gmp::fibnum which ended up 2 bytes longer than this solution. x=scan();for(i in x[1]:x[2])F=F+i^gmp::fibnum((T<-T+1)-1);F  • Using an anonymous function rather than scan() saves 6 bytes; see my posted solution. – rturnbull Jan 5 '17 at 18:06 • ah yeah, silly of me. – JAD Jan 5 '17 at 18:14 # dc, 56 bytes ?sf?sa0dsbsg1sc[lblcdlfrdsb^lg+sg+sclf1+dsfla!<d]dsdxlgp  Finishes for input [1,30] in 51 seconds. Takes the two inputs on two separate lines once executed and negative numbers with a leading underscore (_) instead of a dash (i.e -4 would be input as _4). # PHP, 77 75 bytes for($b=$argv[$$x=1];b<=argv[2];{x=!x}={""}+{1})s+=b++**$$x;echo$s;


takes boundaries from command line arguments. Run with -nr.
showcasing PHP´s variable variables again (and what I´ve found out about them.

breakdown

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´s answer ported to PHP has 70 bytes: function f($a,$b,$x=1,$y=1){return$a>$b?0:$a**$x+f($a+1,$b,$y,$x+$y);}


# Axiom, 65 bytes

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


test code and results

(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)


# PowerShell, 67 bytes

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


Try it online!

Found a slightly better way to do the sequence, but powershell doesn't compare to other languages for this one :)