Fibonacci Exponents

11

1

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!

Anthony Pham

Posted 2016-12-31T14:48:59.253

Reputation: 1 911

So 0 is not included in the fibonacci numbers here? – FlipTack – 2016-12-31T14:58:55.233

0 is not a Fibonacci number but is a valid choice for input – Anthony Pham – 2016-12-31T14:59:16.330

633165 or 33156? – Neil – 2016-12-31T15:29:51.283

@Neil I think you're right – Anthony Pham – 2016-12-31T18:47:37.780

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 – 2017-04-17T15:16:26.073

Answers

2

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.

Magic Octopus Urn

Posted 2016-12-31T14:48:59.253

Reputation: 19 422

5

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.

FlipTack

Posted 2016-12-31T14:48:59.253

Reputation: 13 242

Why -~a and not simply a+1? I think -~a is machine dependant. – Titus – 2017-01-06T01:19:31.653

5

Mathematica, 38 bytes 37 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.

ngenisis

Posted 2016-12-31T14:48:59.253

Reputation: 4 600

3You can use Range@##. – Martin Ender – 2016-12-31T19:16:25.173

1Not as relevant now, but the original approach can be improved 3 bytes to Tr[(r=Range@##)^Fibonacci[r-#+1]]&. – Greg Martin – 2017-01-05T18:40:16.427

Using Range twice should have been a red flag. Thanks! – ngenisis – 2017-01-05T20:12:57.163

4

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.

Sean

Posted 2016-12-31T14:48:59.253

Reputation: 4 136

(1,&[+]...*) is one byte shorter, and the space after Z** isn't needed. – Brad Gilbert b2gills – 2017-01-01T06:47:51.610

@BradGilbertb2gills Cool, I had no idea the Fibonacci sequence could be expressed that way. – Sean – 2017-01-01T18:55:19.023

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 – 2017-01-01T19:19:43.260

3

Maxima , 32 bytes

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

rahnema1

Posted 2016-12-31T14:48:59.253

Reputation: 5 435

3

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

Blue

Posted 2016-12-31T14:48:59.253

Reputation: 26 661

3

Haskell, 35 bytes

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

Usage:

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

Roman Czyborra

Posted 2016-12-31T14:48:59.253

Reputation: 604

You can turn the function o into an infix operator, like a#b=sum.... – nimi – 2017-01-02T23:49:30.130

Had considered infix like a…b but read the requirement to accept unary (ℤ,ℤ)→ℕ – Roman Czyborra – 2017-01-04T11:11:41.043

Many other answers take two separate arguments, so I think it's fine. – nimi – 2017-01-04T20:29:12.910

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 – 2017-01-05T09:52:04.113

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 – 2017-01-06T01:13:22.790

You mean: 2 lists of numbers. I thought a#b went too far. – Roman Czyborra – 2017-01-06T09:06:34.813

More precisely: 2 lists of numbers. Substituting [a..b] for (a,b) and postfix ?f for prefix o seems as cognitively reasonable as substituting a#b or o a b for o(a,b) to me. – Roman Czyborra – 2017-01-06T09:12:26.717

3

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.

Neil

Posted 2016-12-31T14:48:59.253

Reputation: 95 035

Nice, turned out even shorter in JavaScript! :) – FlipTack – 2016-12-31T22:15:12.177

2

MATL, 23 bytes

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

Try it online! Or verify all test cases.

&:      % 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

Luis Mendo

Posted 2016-12-31T14:48:59.253

Reputation: 87 464

1

R, 51 bytes

An anonymous function.

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

rturnbull

Posted 2016-12-31T14:48:59.253

Reputation: 3 689

1

Jelly, 13 bytes

ạµ1+⁸С0
r*çS

Try it online!

Lynn

Posted 2016-12-31T14:48:59.253

Reputation: 55 648

Nice, the only other answer I've found where an input of f(1,25) works ;). +1 – Magic Octopus Urn – 2017-01-05T19:21:15.053

0

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.

G B

Posted 2016-12-31T14:48:59.253

Reputation: 11 099

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 http://codegolf.stackexchange.com/questions/363/tips-for-golfing-in-ruby yet?

– Roman Czyborra – 2017-01-05T10:14:27.640

0

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;
}

peech

Posted 2016-12-31T14:48:59.253

Reputation: 309

0

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

JAD

Posted 2016-12-31T14:48:59.253

Reputation: 2 898

Using an anonymous function rather than scan() saves 6 bytes; see my posted solution. – rturnbull – 2017-01-05T18:06:07.457

ah yeah, silly of me. – JAD – 2017-01-05T18:14:40.807

0

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

R. Kap

Posted 2016-12-31T14:48:59.253

Reputation: 4 730

0

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);}

Titus

Posted 2016-12-31T14:48:59.253

Reputation: 13 814

0

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)

RosLuP

Posted 2016-12-31T14:48:59.253

Reputation: 3 036

0

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

Sinusoid

Posted 2016-12-31T14:48:59.253

Reputation: 451