TransWikia.com

Fibonacci Exponents

Code Golf Asked by Anthony Pham on December 20, 2020

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!

24 Answers

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.

Correct answer by Magic Octopus Urn on December 20, 2020

05AB1E, 10 bytes

ŸvyN>Åfm}O

Try it online! Takes two lines of input, the first line being b and the second line being a.

 v          # for y in...
Ÿ           # [a, ..., b] (implicit input)
       m    # push...
  y         # current item in list
       m    # to the power of...
     Åf     # the...
   N>       # current index in list...
     Åf     # th Fibonacci number
        }   # end loop
         O  # push sum(stack)
            # implicit output

Answered by Makonede on December 20, 2020

Japt -x, 9 bytes

òV ËpMgEÄ

Try it

Answered by Shaggy on December 20, 2020

Arn -x, 11 8 bytes

gŸ▀<Çúì═

Try it!

Explained

Unpacked: .=z^[1 1{+

   .=       Rangify input (inclusive)
z^          Zipped with exponentiation to
   [1 1{+   The fibonacci sequence, closing }] implied
          Then take the sum

Answered by ZippyMagician on December 20, 2020

Jelly, 9 8 bytes

rJÆḞ*@rS

Try it online!

-1 byte thanks to ZippyMagician

How it works

rJÆḞ*@rS - Main link. Takes a on the left and b on the right
r        - Range from a to b
 J       - Length range of that
  ÆḞ     - n'th Fibonacci number of each
      r  - Range from a to b
    *@   - Raise each to the power of the Fib numbers
       S - Sum

Answered by caird coinheringaahing on December 20, 2020

Husk, 6 bytes

Σz^İf…

Try it online!

Explanation

Σz^İf…
     … rangify the input
 z^    zip using the power function
   İf  using the fibonacci numbers
Σ      sum the result

Answered by Razetime on December 20, 2020

Whispers v3, 87 bytes

> Input
> Input
> fₙ
>> 1…2
>> #4
>> 3ᶠ5
>> L*R
>> Each 7 4 6
>> ∑8
>> Output 9

Unfortunately, Whispers v3 isn't on TryItOnline!, so there isn't an online interpreter to test this, aside from downloading the repository and running it locally.

Answered by caird coinheringaahing on December 20, 2020

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

Answered by Sinusoid on December 20, 2020

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)

Answered by user58988 on December 20, 2020

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

Answered by Titus on December 20, 2020

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

Answered by R. Kap on December 20, 2020

Jelly, 13 bytes

ạµ1+⁸С0
r*çS

Try it online!

Answered by Lynn on December 20, 2020

R, 51 bytes

An anonymous function.

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

Answered by rturnbull on December 20, 2020

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

Answered by JAD on December 20, 2020

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

Answered by peech on December 20, 2020

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.

Answered by G B on December 20, 2020

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.

Answered by Neil on December 20, 2020

Haskell, 35 bytes

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

Usage:

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

Answered by Roman Czyborra on December 20, 2020

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.

Answered by Sean on December 20, 2020

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

Answered by Blue on December 20, 2020

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.

Answered by ngenisis on December 20, 2020

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

Answered by Luis Mendo on December 20, 2020

Maxima , 32 bytes

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

Answered by rahnema1 on December 20, 2020

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.

Answered by FlipTack on December 20, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP