14
\$\begingroup\$

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!

\$\endgroup\$
5
  • \$\begingroup\$ So 0 is not included in the fibonacci numbers here? \$\endgroup\$
    – FlipTack
    Dec 31, 2016 at 14:58
  • \$\begingroup\$ 0 is not a Fibonacci number but is a valid choice for input \$\endgroup\$ Dec 31, 2016 at 14:59
  • 6
    \$\begingroup\$ 33165 or 33156? \$\endgroup\$
    – Neil
    Dec 31, 2016 at 15:29
  • \$\begingroup\$ @Neil I think you're right \$\endgroup\$ Dec 31, 2016 at 18:47
  • \$\begingroup\$ 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 \$\endgroup\$
    – user58988
    Apr 17, 2017 at 15:16

25 Answers 25

5
\$\begingroup\$

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.

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

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.

\$\endgroup\$
1
  • \$\begingroup\$ Why -~a and not simply a+1? I think -~a is machine dependant. \$\endgroup\$
    – Titus
    Jan 6, 2017 at 1:19
4
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ (1,&[+]...*) is one byte shorter, and the space after Z** isn't needed. \$\endgroup\$ Jan 1, 2017 at 6:47
  • \$\begingroup\$ @BradGilbertb2gills Cool, I had no idea the Fibonacci sequence could be expressed that way. \$\endgroup\$
    – Sean
    Jan 1, 2017 at 18:55
  • \$\begingroup\$ 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) \$\endgroup\$ Jan 1, 2017 at 19:19
3
\$\begingroup\$

Maxima , 32 bytes

f(a,b):=sum(x^fib(x-a+1),x,a,b);
\$\endgroup\$
3
\$\begingroup\$

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(^)
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Nice, turned out even shorter in JavaScript! :) \$\endgroup\$
    – FlipTack
    Dec 31, 2016 at 22:15
3
\$\begingroup\$

Haskell, 35 bytes

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

Usage:

$ ghc fibexps.hs -e '[4..8]?f'
33156
\$\endgroup\$
7
  • \$\begingroup\$ You can turn the function o into an infix operator, like a#b=sum.... \$\endgroup\$
    – nimi
    Jan 2, 2017 at 23:49
  • \$\begingroup\$ Had considered infix like a…b but read the requirement to accept unary (ℤ,ℤ)→ℕ \$\endgroup\$ Jan 4, 2017 at 11:11
  • \$\begingroup\$ Many other answers take two separate arguments, so I think it's fine. \$\endgroup\$
    – nimi
    Jan 4, 2017 at 20:29
  • \$\begingroup\$ 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(^)? \$\endgroup\$ Jan 5, 2017 at 9:52
  • \$\begingroup\$ 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. \$\endgroup\$
    – nimi
    Jan 6, 2017 at 1:13
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ I'm confused as to why this is the accepted answer, it doesn't output the correct result for any of the given testcases. The exponents should be the fibonacci numbers, not the integers in the range. I'm not sure what features were available back then, but ŸDgLÅfmO or just ŸāÅfmO would work instead. \$\endgroup\$
    – ovs
    Nov 13, 2020 at 16:48
  • \$\begingroup\$ I was wondering that too. I came up with a 9-byte answer that works, but your alternatives are much better. \$\endgroup\$ Nov 13, 2020 at 16:57
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Vyxal s, 6 bytes

ṡ:ẏ∆fe

Try it online, or see a seven byte flagless solution. Takes input with b first, then a.

Explanation:

ṡ       # Range from a to b
 :      # Duplicate
  ẏ     # Range from 0 to length
   ∆f   # Nth Fibonacci number
     e  # To the power of
        # Sum with the s flag
\$\endgroup\$
1
\$\begingroup\$

R, 51 bytes

An anonymous function.

function(a,b)sum((a:b)^numbers::fibonacci(b-a+1,T))
\$\endgroup\$
0
1
\$\begingroup\$

Jelly, 13 bytes

ạµ1+⁸С0
r*çS

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Nice, the only other answer I've found where an input of f(1,25) works ;). +1 \$\endgroup\$ Jan 5, 2017 at 19:21
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
0
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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? \$\endgroup\$ Jan 5, 2017 at 10:14
0
\$\begingroup\$

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;
}
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Using an anonymous function rather than scan() saves 6 bytes; see my posted solution. \$\endgroup\$
    – rturnbull
    Jan 5, 2017 at 18:06
  • \$\begingroup\$ ah yeah, silly of me. \$\endgroup\$
    – JAD
    Jan 5, 2017 at 18:14
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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);}
\$\endgroup\$
0
\$\begingroup\$

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)
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

Japt -x, 9 bytes

òV ËpMgEÄ

Try it

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.