44
\$\begingroup\$

Definition

Fibonacci sequence F(n), on the positive integers, are defined as such:

1. F(1) = 1
2. F(2) = 1
3. F(n) = F(n-1) + F(n-2), where n is an integer and n > 2

The Fibonacci-orial of a positive integer is the product of [F(1), F(2), ..., F(n)].

Task

Given positive integer n, find the Fibonacci-orial of n.

Specs

The fibonacci-orial of 100 must compute in under 5 seconds on a reasonable computer.

Testcases

n   Fibonacci-orial of n
1   1
2   1
3   2
4   6
5   30
6   240
7   3120
8   65520
9   2227680
10  122522400
11  10904493600
12  1570247078400
13  365867569267200
14  137932073613734400
15  84138564904377984000
16  83044763560621070208000
17  132622487406311849122176000
18  342696507457909818131702784000
19  1432814097681520949608649339904000
20  9692987370815489224102512784450560000
100 3371601853146468125386964065447576689828006172937411310662486977801540671138589868616500834190029067583665182291701553172011082574587431382310099030394306877775647395167143332483560925112960024644459715300507481235056111434293619038347456390454209587101225261757371666449068625033999573552165524529725467628060170886602001077137613803027158648329335507728698605769992818756765633305318529965186184043999696650407246193257877568825245646129366994079739720698147440310773871269639752334356493678913424390564535389212240038895626811627949132978086070255082668392290037141141291484839596694182152062726390364094447642643912371532491388089634845995941928089653751672688740718152064107169357399466473375804972260594768969952507346694189050233823596316467570584434128052398891223730335019092974935617029638919358286124350711360361279157416837428904150054292406756317837582840596331363581207781793070936765786629772999832857257349696094416616259974304208756997835360702840912518532683324936435856348020736000000000000000000000000

References

\$\endgroup\$
13
  • \$\begingroup\$ Related. \$\endgroup\$
    – Leaky Nun
    Commented Jul 30, 2016 at 3:19
  • 2
    \$\begingroup\$ @LuisMendo The sum of fibonacci is... you've guessed it, fibonacci. Well, minus one. \$\endgroup\$
    – Leaky Nun
    Commented Jul 30, 2016 at 9:23
  • 2
    \$\begingroup\$ @LeakyNun Currently the JavaScript answer only completes the test cases up to 15 because JavaScript cannot correctly compare (or manipulate) numbers beyond 2^53 - 1. This is most likely similar for a lot of the submissions here, because most languages don't support numbers that big \$\endgroup\$
    – bren
    Commented Jul 30, 2016 at 13:54
  • 1
    \$\begingroup\$ What do you mean by "reasonable computer"? \$\endgroup\$ Commented Jul 31, 2016 at 16:26
  • 3
    \$\begingroup\$ -1 because this seems like several challenges tacked together (range, fibonacci of each, factorial) with no particularly interesting shortcuts. \$\endgroup\$ Commented Dec 28, 2017 at 2:25

69 Answers 69

71
\$\begingroup\$

Mathematica, 10 bytes

Fibonorial

Another Mathematica built-in soundly beaten by a golfing language without the built-in.

\$\endgroup\$
6
  • 52
    \$\begingroup\$ I… w-what… why, Mathematica?! \$\endgroup\$
    – lynn
    Commented Jul 30, 2016 at 10:41
  • 3
    \$\begingroup\$ Forgot that function even existed! \$\endgroup\$ Commented Jul 30, 2016 at 13:42
  • 3
    \$\begingroup\$ @Lynn Rule 35: If it exists, there's a Mathematica function of it ;) \$\endgroup\$
    – Beta Decay
    Commented Aug 3, 2016 at 15:27
  • 10
    \$\begingroup\$ @BetaDecay I thought we had established that it's Rule 110. \$\endgroup\$ Commented Aug 3, 2016 at 15:29
  • 1
    \$\begingroup\$ No, Rule 110 is something very different. Though I'm sure Mathematica has a built-in for that, too. \$\endgroup\$ Commented Aug 5, 2016 at 19:05
28
\$\begingroup\$

Jelly, 6 bytes

+С1ḊP

Input 100 finishes in 500 ms locally. Try it online!

How it works

+С1ḊP  Niladic link. No input.
        Since the link doesn't start with a nilad, the argument 0 is used.

   1    Yield 1.
+       Add the left and right argument.
 С     Read a number n from STDIN.
        Repeatedly call the dyadic link +, updating the right argument with
        the value of the left one, and the left one with the return value.
        Collect all values of the left argument into an array.
    Ḋ   Dequeue; discard the first Fibonacci number (0).
     P  Product; multiply the remaining ones.
\$\endgroup\$
3
  • \$\begingroup\$ So is +¡1 a non-built in nth fibonacci and +С1 is the first n Fibonacci numbers? \$\endgroup\$ Commented Oct 11, 2017 at 22:02
  • \$\begingroup\$ @cairdcoinheringaahing Pretty much. \$\endgroup\$
    – Dennis
    Commented Oct 11, 2017 at 22:03
  • \$\begingroup\$ I thought there was a built-in fibonacci function? \$\endgroup\$
    – MilkyWay90
    Commented Mar 17, 2019 at 17:20
21
\$\begingroup\$

Actually, 4 bytes

Runs the input 100 within 0.2 seconds. Code:

R♂Fπ

Explanation:

R     # Get the range [1, ..., input].
 ♂F   # Map over the array with the fibonacci command.
   π  # Take the product.

Uses the CP-437 encoding. Try it online!.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This is the right tool for the job :) Thanks to some improvements to the Fibonacci function that Leaky Nun made a while back, the 100 test case runs in 0.1 seconds on TIO. \$\endgroup\$
    – user45941
    Commented Aug 1, 2016 at 2:27
21
\$\begingroup\$

Brainfuck, 1198 1067 817 770 741 657 611 603

,[>++++++[-<-------->]<<[->++++++++++<]>>,]>+>+>>+>+<<<<<<[->>>[[-]
<[->+<]>>[-<+<+>>]<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->
[-]>>>>>>+>+<<<<<<<<[->+<]]]]]]]]]]]+>>>>>>>>]<<<<<<<<[->[-<+<<+>>>
]<[->+<]>>>[<<<<<[->>>>>>>>+<<<<<<<<]>>>>>>>>>>>>>]<<<<<<<<[->>>[-<
<<<<<<<+>>>>[->+>>+<<<]>[-<+>]>>>]<<[->>>>>>>+>+<<<<<<<<]<+<<<<<<<<
]>>>[-]>>>>>[[-]>[->+<]>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<
+>[->>>>>>+>+<<<<<<<<[-]]]]]]]]]]>]<<[<]+>>>>>>>>]<<<<<<<<[<<<<<<<<
]>>>>>[>>>>>>>>]+<<<<<<<<]>>>>>>>>>>>[<[-]>>[-<<+>>]>>>>>>>]<<<<<<<
<[<<<<<<<<]>>]>>>>>>[>>>>>>>>]<<<<<<<<[+++++[-<++++++++>]<.<<<<<<<]

Uncompressed, with comments:

# parse input (max 255)
,[>++++++[-<-------->]<<[->++++++++++<]>>,]
>+>+>>+>+<<<<<<
[->>>
  # compute next fibonacci number
  [[-]<
    [->+<]>>
    [-<+<+>>]<
    # perform carries
    [->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<
      [->[-]>>>>>>+>+<<<<<<<<[->+<]]
    ]]]]]]]]]+>>>>>>>>
  ]<<<<<<<<
  # multiplication
  [->
    # extract next digit of F_n (most significant first)
    [-<+<<+>>>]<
    [->+<]>>>
    # move back to the end
    [<<<<<
      [->>>>>>>>+<<<<<<<<]>>>>>>>>>>>>>
    ]<<<<<<<<
    # digit wise multiplication (shifting current digit)
    [->>>
      [-<<<<<<<<+>>>>
        [->+>>+<<<]>
        [-<+>]>>>
      ]<<
      # shift previous total over one gap (in effect multiplying by 10)
      [->>>>>>>+>+<<<<<<<<]<+<<<<<<<<
    ]>>>[-]>>>>>
    # add product to total
    [[-]>
      [->+<]>
      # perform carries
      [-<+>
        [-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>
          [->>>>>>+>+<<<<<<<<[-]]
        ]]]]]]]]>
      ]<<[<]+>>>>>>>>
    ]<<<<<<<<
    [<<<<<<<<]>>>>>
    [>>>>>>>>]+<<<<<<<<
  ]>>>>>>>>>>>
  # overwrite previous product
  [<[-]>>
    [-<<+>>]>>>>>>>
  ]<<<<<<<<
  [<<<<<<<<]>>
]>>>>>>
# output product
[>>>>>>>>]<<<<<<<<
[+++++
  [-<++++++++>]<.<<<<<<<
]

Try it online!

Runtime for n = 100 is less than 1 second with the online interpreter (about 0.2s locally using my own interpreter). Maximum input is 255, but would require the interpreter to support ~54000 cells (the online interpreter seems to wrap on 64k).


Change Log

Saved around 130 bytes with better extraction of the current digit to multiply through by, and by merging add and carry into a single pass. It also seems to be a bit faster.

Saved another 250 bytes. I managed to reduce my multiplication scratch pad by two cells, which saves bytes just about everywhere by not having to shift so far between digits. I also dropped the carry after multiplying through by a digit, and instead perform a full carry whilst adding to the running total.

Chopped another 50, again with better extraction of the current digit to multiply through by, simply by not moving it forward the first iteration, and working from where it is. A few micro-optimization further down account for around ~10 bytes.

30 more gone. Marking digits that have already been taken with a 0 rather than a 1 makes them easier to locate. It also makes the check if the multiplication loop has finished somewhat simpler.

I reduced the scratch pad by another cell, for 80 more bytes. I did this by merging the marker for the previous product and the current running total, which reduces the shifts between gaps, and makes bookkeeping a bit easier.

Saved another 50, by eliminating yet another cell, reusing the marker for fibonacci digits to mark the last digit taken as well. I was also able to merge the loop to shift the previous totals with the digit-wise multiplication loop.

Saved 8 bytes on input parsing. Oops.

\$\endgroup\$
15
\$\begingroup\$

Python, 45 Bytes

a=b=o=1
exec"o*=a;a,b=b,a+b;"*input()
print o

Input is taken from stdin. Output for n = 100 finishes too quickly to accurately time. n = 1000 takes approximately 1s.

Sample Usage

$ echo 10 | python fib-orial.py
122522400

$ echo 100 | python fib-orial.py
3371601853146468125386964065447576689828006172937411310662486977801540671138589868616500834190029067583665182291701553172011082574587431382310099030394306877775647395167143332483560925112960024644459715300507481235056111434293619038347456390454209587101225261757371666449068625033999573552165524529725467628060170886602001077137613803027158648329335507728698605769992818756765633305318529965186184043999696650407246193257877568825245646129366994079739720698147440310773871269639752334356493678913424390564535389212240038895626811627949132978086070255082668392290037141141291484839596694182152062726390364094447642643912371532491388089634845995941928089653751672688740718152064107169357399466473375804972260594768969952507346694189050233823596316467570584434128052398891223730335019092974935617029638919358286124350711360361279157416837428904150054292406756317837582840596331363581207781793070936765786629772999832857257349696094416616259974304208756997835360702840912518532683324936435856348020736000000000000000000000000
\$\endgroup\$
0
13
\$\begingroup\$

Haskell 41 29 Bytes

1+11 bytes saved by @Laikoni's remarks.

f=1:scanl(+)1f
(scanl(*)1f!!)

1,f and !! are separate tokens. The first lines defines the fibonacci sequence, the second is a function that computes the sequence of fibonacci-orials and returns the n-th for a given n. It starts printing digits almost immediately even for n=1000.

\$\endgroup\$
10
  • 1
    \$\begingroup\$ You can get rid of the space (scanl(*)1f!!). \$\endgroup\$
    – Laikoni
    Commented Jul 30, 2016 at 8:47
  • 2
    \$\begingroup\$ And there is a shorter fibonacci generator here: f=1:scanl(+)1f \$\endgroup\$
    – Laikoni
    Commented Jul 30, 2016 at 8:49
  • \$\begingroup\$ @Laikoni That's amazing, thanks! \$\endgroup\$ Commented Jul 30, 2016 at 9:15
  • 2
    \$\begingroup\$ @WillNess I think I am not only justified by what other users do, but also by meta.codegolf.stackexchange.com/questions/2419/… and meta.codegolf.stackexchange.com/questions/9031/… (but there is a lot more and I haven't read all) \$\endgroup\$ Commented Aug 1, 2016 at 11:07
  • 1
    \$\begingroup\$ @flawr Would you accept 42+ as a function that adds 42? You shouldn't, because it is just an unfinished expression. But in Haskell we can add parentheses and get the section (42+), a way to write the function \n->42+n. Here it is the same, only with !! (the binary infix operator for indexing) instead of + and a more complicated first operand. \$\endgroup\$ Commented Aug 5, 2016 at 23:28
11
\$\begingroup\$

Python 2, 39 bytes

f=lambda n,a=1,b=1:n<1or a*f(n-1,b,a+b)

Test it on Ideone.

\$\endgroup\$
2
  • \$\begingroup\$ You may want to specify that it returns True in some cases. \$\endgroup\$
    – Leaky Nun
    Commented Jul 30, 2016 at 17:19
  • 5
    \$\begingroup\$ This would only return True for input 0, which isn't positive. \$\endgroup\$
    – Dennis
    Commented Jul 30, 2016 at 17:22
6
\$\begingroup\$

J, 17 16 bytes

1 byte is golfed with even better solution by miles.

[:*/+/@(!|.)\@i.

The idea is the same as the original but instead of forming the matrix to operate on minor diagonals we form the diagonals on the fly.


Original

To get the first n fibonomials:

*/\(#{.+//.)!/~i.

Reading right to left...
Create the array of consecutive integers (i.) up to specified one, from that array create the table (/~) of binomial coefficients (!) calculated from every pair in the array, this table is Pascal's triangle top of whlocated at the end of the first row and all elements under the main diagonal are 0, thankfully to implementation of !. If you sum (+/) all minor diagonals (/.), you get Fibonacci numbers, but you need to take ({.) as much of first elements from the resulting array as the length (#) of the table itself. Then the product (*/) applied to consecutive prefixes (\) of the array results into desired sequence of fibonorials. If you wish you can take only the last one using 2 more bytes ({:) but I thought that displaying all of them is not a sin :).
NB. the previous code block is not a J function.

   {:*/\(#{.+//.)!/~i. 10
122522400

For big numbers in J you use x at the end:

   {:*/\(#{.+//.)!/~i. 100x
3371601853146468125386964065447576689828006172937411310662486977801540671138589868616500834190029067583665182291701553172011082574587431382310099030394306877775647395167143332483560925112960024644459715300507481235056111434293619038347456390454209587101225...

The program runs on avarage 0.11s.

   100 timex '{:*/\(#{.+//.)!/~i.100x'
0.112124
\$\endgroup\$
1
  • 1
    \$\begingroup\$ An alternative that is a function is [:*/+/@(!|.)\@i. using 16 bytes. It forms that same binomial coefficients along the table you generate using !/~ except that it forms that by taking prefixes of i.. \$\endgroup\$
    – miles
    Commented Jul 30, 2016 at 23:06
4
\$\begingroup\$

Pyth, 13 bytes

u*Gs=[sZhZ)Q1

Demonstration

This employs a clever, non-typesafe trick. Five of the characters (u*G ... Q1) say that the output is the product of the input many numbers. The rest of the code generates the numbers.

=[sZhZ) updates the variable Z to the list [s(Z), h(Z)]. Then s sums that list, to be multiplied.

Z is initially 0. s, on ints, is the identity function. h, on its, is the + 1 function. So on the first iteration, Z becomes [0, 1]. s on lists is the sum function, as mentioned above. h is the head function. So the second iteration is [1, 0].

Here's a list:

Iter  Z          Sum
0     0          Not used
1     [0, 1]     1
2     [1, 0]     1
3     [1, 1]     2
4     [2, 1]     3
5     [3, 2]     5

These sums are multiplied up to give the result.

\$\endgroup\$
4
\$\begingroup\$

Mathematica 25 24 bytes

With thanks to Martin Ender.

1##&@@Fibonacci@Range@#&

1##&@@Fibonacci@Range@#&@100

Timing: 63 microseconds.

{0.000063, 
3371601853146468125386964065447576689828006172937411310662486977801540
6711385898686165008341900290675836651822917015531720110825745874313823
1009903039430687777564739516714333248356092511296002464445971530050748
1235056111434293619038347456390454209587101225261757371666449068625033
9995735521655245297254676280601708866020010771376138030271586483293355
0772869860576999281875676563330531852996518618404399969665040724619325
7877568825245646129366994079739720698147440310773871269639752334356493
6789134243905645353892122400388956268116279491329780860702550826683922
9003714114129148483959669418215206272639036409444764264391237153249138
8089634845995941928089653751672688740718152064107169357399466473375804
9722605947689699525073466941890502338235963164675705844341280523988912
2373033501909297493561702963891935828612435071136036127915741683742890
4150054292406756317837582840596331363581207781793070936765786629772999
8328572573496960944166162599743042087569978353607028409125185326833249
36435856348020736000000000000000000000000}
\$\endgroup\$
1
  • \$\begingroup\$ Alternately, with same byte count: 1##&@@Fibonacci~Array~#& \$\endgroup\$ Commented Jan 5, 2017 at 19:18
4
\$\begingroup\$

Jelly, 8 bytes

RḶUc$S€P

My first submission in Jelly. It's not as short as @Dennis' answer, but its only 2 bytes longer with a different method.

Locally, it requires about 400ms compared to 380ms with @Dennis' version for n = 100.

Try it online!

Explanation

RḶUc$S€P  Input: n
R         Generate the range [1, 2, ..., n]
          For each value x in that range
 Ḷ          Create another range [0, 1, ..., x-1]
  U         Reverse that list
   c        Compute the binomial coefficients between each pair of values
    $       Bind the last two links (Uc) as a monad
     S€   Sum each list of binomial coefficients
          This will give the first n Fibonacci numbers
       P  Take the product of those to compute the nth Fibonacci-orial
\$\endgroup\$
4
\$\begingroup\$

R, 99 96 78 76 66 bytes

This answer is uses Binet's Formula, as well as the prod(x)function. Since R doesn't have a build-in Phi value, I defined it myself :

p=(1+sqrt(5))/2;x=1;for(n in 1:scan())x=x*(p^n-(-1/p)^n)/sqrt(5);x

It works under 5 seconds, but R tends to give Inf as an answer for those big numbers...

Ungolfed :

r=sqrt(5)
p=(1+r)/2 
x=1
for(n in 1:scan())        
    x=x*(p^n-(-1/p)^n)/r    
x

-2 bytes thanks to @Cyoce !
Oh, do I love this site ! -10 bytes thanks to @user5957401

\$\endgroup\$
3
  • \$\begingroup\$ Might be able to save a bit by storing sqrt(5) to a variable \$\endgroup\$
    – Cyoce
    Commented Aug 7, 2016 at 6:20
  • \$\begingroup\$ since you only use N once, you can just call scan inside the 1:N bit. i.e. for(n in 1:scan()). You can also save a few characters by just using * instead of the prod() function in your for loop. Your for loop is only one line, so you don't need the curly braces either. \$\endgroup\$ Commented Aug 10, 2016 at 19:31
  • \$\begingroup\$ Nice idea to use Binet's formula. In your spirit but only 53 bytes is function(n,N=1:n,p=5^.5)prod(((1+p)^N-(1-p)^N)/2^N/p) \$\endgroup\$
    – Michael M
    Commented Feb 19, 2018 at 9:19
4
\$\begingroup\$

R, 82, 53, 49 bytes (48 bytes w/ different input style)

b=d=1;a=0;for(i in 1:scan()){d=d*b;b=a+b;a=b-a};d

If we can just precede the code with the input number, we get the 48 byte

->n;b=d=1;a=0;for(i in 1:n){d=d*b;b=a+b;a=b-a};d

EDIT: New code. Original is below:

a=function(n)ifelse(n<3,1,{v=c(1,1,3:n);for(i in 3:n)v[i]=v[i-1]+v[i-2];prod(v)})

Won't return anything other than Inf for a(100) though. And it won't work for anything but non-negative integers.

Ungolfed:

a=function(n){
   if(n<3) return(1)
   v=c(1,1,3:n)
   for(i in 3:n)
       v[i]=v[i-1]+v[i-2]
   prod(v)
}
\$\endgroup\$
3
\$\begingroup\$

PARI/GP, 29 bytes

f=n->prod(i=1,n,fibonacci(i))

Or alternatively:

f=n->prod(i=a=!b=0,n,b=a+a=b)
\$\endgroup\$
3
\$\begingroup\$

Brachylog, 31 bytes

,[1:0]:?:{hH,?bh:H+g:?c.}irbb*.

Try it online!

\$\endgroup\$
3
+100
\$\begingroup\$

APL (Dyalog Unicode), 18 bytes

×/(+.!∘⌽⍨∘⍳¨1+∘⍳⊢)

Explanation:

×/(+.!∘⌽⍨∘⍳¨1+∘⍳⊢)
            1+∘⍳⊢   ⍝ generate numbers 2-(⍵+1)
           ¨        ⍝ ... map to each
   +.!∘⌽⍨∘⍳         ⍝ n-th fibonacci number
×/                  ⍝ product of all

Fibonacci formula explanation:

+.!∘⌽⍨∘⍳
       ⍳    ⍝ numbers 1 to ⍵
     ⍨      ⍝ apply to both sides of +.!∘⌽
    ⌽       ⍝ reverse the ⍵ vector.
+.!         ⍝ inner product: sum and binomial

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ This template may be helpful for future programs. \$\endgroup\$
    – Razetime
    Commented Dec 20, 2020 at 13:44
  • \$\begingroup\$ @Razetime I've seen it, but it didn't work for me - I accidentally put my footer code in the footer. I'll use it next time I need to do something \$\endgroup\$ Commented Dec 20, 2020 at 13:47
  • \$\begingroup\$ Since you're only using the argument once () and you hand two consecutive atops, a full program would save 3 bytes: ×/+.!∘⌽⍨∘⍳¨1+⍳⎕ \$\endgroup\$
    – Adám
    Commented Mar 2, 2021 at 15:46
2
\$\begingroup\$

Ruby, 39 bytes

->n{f=i=b=1;n.times{f,i,b=i,f+i,b*f};b}
\$\endgroup\$
1
  • \$\begingroup\$ 36: ->n{f=i=b=1;n.times{b*=f;i=f+f=i};b} \$\endgroup\$
    – G B
    Commented Feb 23, 2018 at 9:19
2
\$\begingroup\$

Forth, 55 bytes

Uses an iterative approach, built upon my Fibonacci answer in Forth. The results overflow arithmetically for n > 10. The answer is case-insensitive.

: f >r 1 1 r@ 0 DO 2dup + LOOP 2drop 1 r> 0 DO * LOOP ;

Try it online

\$\endgroup\$
2
\$\begingroup\$

Java, 165 bytes

Golfed:

BigInteger f(int n){BigInteger[]a={BigInteger.ZERO,BigInteger.ONE,BigInteger.ONE};for(int i=0;i<n;){a[++i%2]=a[0].add(a[1]);a[2]=a[2].multiply(a[i%2]);}return a[2];}

This is yet another case where BigInteger being required due to large numbers. However, I was able to keep the text BigInteger to a minimum, keeping the size down. I also compared with static imports, and it made the total length longer.

This program works by tracking three numbers in an array. The first two are the previous two Fibonacci numbers. The third is the accumulated value. The loop starts out by calculating the next value and storing it in alternating (0, 1, 0, 1, ...) array indices. This avoids needing to shift values with costly (in terms of source size) assignment operations. Then grab that new value and multiply it into the accumulator.

By avoiding temporary objects and limiting the loop to two assignment operators, I was able to squeeze out quite a few bytes.

Ungolfed:

import java.math.BigInteger;

public class Fibonacci_orial {

  public static void main(String[] args) {
    // @formatter:off
    String[][] testData = new String[][] {
      { "1", "1" },
      { "2", "1" },
      { "3", "2" },
      { "4", "6" },
      { "5", "30" },
      { "6", "240" },
      { "7", "3120" },
      { "8", "65520" },
      { "9", "2227680" },
      { "10", "122522400" },
      { "11", "10904493600" },
      { "12", "1570247078400" },
      { "13", "365867569267200" },
      { "14", "137932073613734400" },
      { "15", "84138564904377984000" },
      { "16", "83044763560621070208000" },
      { "17", "132622487406311849122176000" },
      { "18", "342696507457909818131702784000" },
      { "19", "1432814097681520949608649339904000" },
      { "20", "9692987370815489224102512784450560000" },
      { "100", "3371601853146468125386964065447576689828006172937411310662486977801540671138589868616500834190029067583665182291701553172011082574587431382310099030394306877775647395167143332483560925112960024644459715300507481235056111434293619038347456390454209587101225261757371666449068625033999573552165524529725467628060170886602001077137613803027158648329335507728698605769992818756765633305318529965186184043999696650407246193257877568825245646129366994079739720698147440310773871269639752334356493678913424390564535389212240038895626811627949132978086070255082668392290037141141291484839596694182152062726390364094447642643912371532491388089634845995941928089653751672688740718152064107169357399466473375804972260594768969952507346694189050233823596316467570584434128052398891223730335019092974935617029638919358286124350711360361279157416837428904150054292406756317837582840596331363581207781793070936765786629772999832857257349696094416616259974304208756997835360702840912518532683324936435856348020736000000000000000000000000" }
    };
    // @formatter:on

    for (String[] data : testData) {
      System.out.println("Input: " + data[0]);
      System.out.println("Expected: " + data[1]);
      System.out.print("Actual:   ");
      System.out.println(new Fibonacci_orial().f(Integer.parseInt(data[0])));
      System.out.println();
    }
  }

  // Begin golf
  BigInteger f(int n) {
    BigInteger[] a = { BigInteger.ZERO, BigInteger.ONE, BigInteger.ONE };
    for (int i = 0; i < n;) {
      a[++i % 2] = a[0].add(a[1]);
      a[2] = a[2].multiply(a[i % 2]);
    }
    return a[2];
  }
  // End golf

}

Program output:

Input: 1
Expected: 1
Actual:   1

Input: 2
Expected: 1
Actual:   1

Input: 3
Expected: 2
Actual:   2

Input: 4
Expected: 6
Actual:   6

Input: 5
Expected: 30
Actual:   30

Input: 6
Expected: 240
Actual:   240

Input: 7
Expected: 3120
Actual:   3120

Input: 8
Expected: 65520
Actual:   65520

Input: 9
Expected: 2227680
Actual:   2227680

Input: 10
Expected: 122522400
Actual:   122522400

Input: 11
Expected: 10904493600
Actual:   10904493600

Input: 12
Expected: 1570247078400
Actual:   1570247078400

Input: 13
Expected: 365867569267200
Actual:   365867569267200

Input: 14
Expected: 137932073613734400
Actual:   137932073613734400

Input: 15
Expected: 84138564904377984000
Actual:   84138564904377984000

Input: 16
Expected: 83044763560621070208000
Actual:   83044763560621070208000

Input: 17
Expected: 132622487406311849122176000
Actual:   132622487406311849122176000

Input: 18
Expected: 342696507457909818131702784000
Actual:   342696507457909818131702784000

Input: 19
Expected: 1432814097681520949608649339904000
Actual:   1432814097681520949608649339904000

Input: 20
Expected: 9692987370815489224102512784450560000
Actual:   9692987370815489224102512784450560000

Input: 100
Expected: 3371601853146468125386964065447576689828006172937411310662486977801540671138589868616500834190029067583665182291701553172011082574587431382310099030394306877775647395167143332483560925112960024644459715300507481235056111434293619038347456390454209587101225261757371666449068625033999573552165524529725467628060170886602001077137613803027158648329335507728698605769992818756765633305318529965186184043999696650407246193257877568825245646129366994079739720698147440310773871269639752334356493678913424390564535389212240038895626811627949132978086070255082668392290037141141291484839596694182152062726390364094447642643912371532491388089634845995941928089653751672688740718152064107169357399466473375804972260594768969952507346694189050233823596316467570584434128052398891223730335019092974935617029638919358286124350711360361279157416837428904150054292406756317837582840596331363581207781793070936765786629772999832857257349696094416616259974304208756997835360702840912518532683324936435856348020736000000000000000000000000
Actual:   3371601853146468125386964065447576689828006172937411310662486977801540671138589868616500834190029067583665182291701553172011082574587431382310099030394306877775647395167143332483560925112960024644459715300507481235056111434293619038347456390454209587101225261757371666449068625033999573552165524529725467628060170886602001077137613803027158648329335507728698605769992818756765633305318529965186184043999696650407246193257877568825245646129366994079739720698147440310773871269639752334356493678913424390564535389212240038895626811627949132978086070255082668392290037141141291484839596694182152062726390364094447642643912371532491388089634845995941928089653751672688740718152064107169357399466473375804972260594768969952507346694189050233823596316467570584434128052398891223730335019092974935617029638919358286124350711360361279157416837428904150054292406756317837582840596331363581207781793070936765786629772999832857257349696094416616259974304208756997835360702840912518532683324936435856348020736000000000000000000000000
\$\endgroup\$
2
\$\begingroup\$

Javascript (ES6), 51 39 bytes

Recursive implementation (39 bytes)

f=(n,a=p=i=b=1)=>++i<n?f(n,b,p*=b+=a):p

Original implementation (51 bytes)

n=>{for(i=a=b=p=1;++i<n;){c=a;a=b;p*=b+=c}return p}

Note: Starts rounding errors for the Fibonacci-orial of 16, 100 is just Infinity, runs in what appears to be <1 second.

\$\endgroup\$
6
  • \$\begingroup\$ I made an alternate 53-byte version n=>[...Array(n)].reduce(p=>(c=a,a=b,p*=b+=c),a=1,b=0) only to discover that you'd counted the f= which isn't required saving you 2 bytes. \$\endgroup\$
    – Neil
    Commented Jul 30, 2016 at 9:00
  • \$\begingroup\$ Fair point. My rationale was so it was callable and reusable rather than just callable. \$\endgroup\$
    – Pandacoder
    Commented Jul 30, 2016 at 13:38
  • \$\begingroup\$ Sure, but if someone does want to reuse it then they still have the option of naming it. \$\endgroup\$
    – Neil
    Commented Jul 30, 2016 at 20:08
  • \$\begingroup\$ @Neil Well now I went and re-implemented it and now the f= is mandatory or else it can't actually execute. :D \$\endgroup\$
    – Pandacoder
    Commented Aug 15, 2016 at 22:18
  • \$\begingroup\$ Well at least you fixed the byte count for the original implementation. \$\endgroup\$
    – Neil
    Commented Aug 15, 2016 at 22:53
2
\$\begingroup\$

DC (GNU or OpenBSD flavour), 36 bytes

File A003266-v2.dc:

0sA1sB1?[1-d0<LrlBdlA+sBdsA*r]dsLxrp

(no trailing newline)

...now the result is held on the stack instead of using a named register (is Y in version 1). The r command is not available in the original dc (see RosettaCode's Dc page).

Run:

$ time dc -f A003266-v2.dc <<< 100
337160185314646812538696406544757668982800617293741131066248697780154\
067113858986861650083419002906758366518229170155317201108257458743138\
231009903039430687777564739516714333248356092511296002464445971530050\
748123505611143429361903834745639045420958710122526175737166644906862\
503399957355216552452972546762806017088660200107713761380302715864832\
933550772869860576999281875676563330531852996518618404399969665040724\
619325787756882524564612936699407973972069814744031077387126963975233\
435649367891342439056453538921224003889562681162794913297808607025508\
266839229003714114129148483959669418215206272639036409444764264391237\
153249138808963484599594192808965375167268874071815206410716935739946\
647337580497226059476896995250734669418905023382359631646757058443412\
805239889122373033501909297493561702963891935828612435071136036127915\
741683742890415005429240675631783758284059633136358120778179307093676\
578662977299983285725734969609441661625997430420875699783536070284091\
2518532683324936435856348020736000000000000000000000000

real    0m0.005s
user    0m0.004s
sys     0m0.000s

Trying to explain:

tos is the contents of the top of the stack without removing it.
nos is the element below tos.

0 sA # push(0) ; A=pop()
1 sB # push(1) ; B=pop()
1    # push(1)
     # this stack position will incrementally hold the product
?    # push( get user input and evaluate it )
[    # start string
 1-  # push(pop()-1)
 d   # push(tos)
 0   # push(0)
 <L  # if pop() < pop() then evaluate contents of register L
 r   # exchange tos and nos
     # now nos is the loop counter
     # and tos is the bucket for the product of the fibonacci numbers
 lB  # push(B)
 d   # push(tos)
 lA  # push(A)
 +   # push(pop()+pop())
     # now tos is A+B, nos still is B 
 sB  # B=pop()
     # completes B=A+B
 d   # push(tos)
 sA  # A=pop()
     # completes B=A
     # tos (duplicated new A from above) is the next fibonacci number
 *   # push(nos*tos)
     # tos now is the product of all fibonacci numbers calculated so far
 r   # exchange tos and nos
     # now tos is the loop counter, nos is the product bucket
]    # end string and push it
d    # push(tos)
sL   # L=pop()
x    # evaluate(pop())
r    # exchange tos and nos
     # nos now is the former loop counter
     # tos now is the complete product. \o/
p    # print(tos)
     # this does not pop() but who cares? :-P

DC, 41 bytes

...straight forward, no tricks:

File A003266-v1.dc:

0sA1sB1sY?[1-d0<LlBdlA+sBdsAlY*sY]dsLxlYp

(no trailing newline)

Run:

$ for i in $(seq 4) ; do dc -f A003266-v1.dc <<< $i ; done
1
1
2
6
$ time dc -f A003266-v1.dc <<< 100
337160185314646812538696406544757668982800617293741131066248697780154\
067113858986861650083419002906758366518229170155317201108257458743138\
231009903039430687777564739516714333248356092511296002464445971530050\
748123505611143429361903834745639045420958710122526175737166644906862\
503399957355216552452972546762806017088660200107713761380302715864832\
933550772869860576999281875676563330531852996518618404399969665040724\
619325787756882524564612936699407973972069814744031077387126963975233\
435649367891342439056453538921224003889562681162794913297808607025508\
266839229003714114129148483959669418215206272639036409444764264391237\
153249138808963484599594192808965375167268874071815206410716935739946\
647337580497226059476896995250734669418905023382359631646757058443412\
805239889122373033501909297493561702963891935828612435071136036127915\
741683742890415005429240675631783758284059633136358120778179307093676\
578662977299983285725734969609441661625997430420875699783536070284091\
2518532683324936435856348020736000000000000000000000000

real    0m0.006s
user    0m0.004s
sys     0m0.004s
\$\endgroup\$
8
  • 1
    \$\begingroup\$ *sigh!* ... writing dc code definitely is easier than explaining it... \$\endgroup\$
    – user19214
    Commented Aug 1, 2016 at 12:05
  • 1
    \$\begingroup\$ Yeah, we really need an IDE with some kind of crazy multiple-stack visualisation gadget... that would be sweet. \$\endgroup\$
    – juh
    Commented Aug 1, 2016 at 15:13
  • 1
    \$\begingroup\$ I have several ideas what to add as new commands, but the idea with the highest impact seems to be: Add a "change default stack" command. ;-) \$\endgroup\$
    – user19214
    Commented Aug 1, 2016 at 17:23
  • 2
    \$\begingroup\$ ...or swap default stack with a named register. That would make more knots in users' brains but would not need the default stack to have a name... ]:-) \$\endgroup\$
    – user19214
    Commented Aug 1, 2016 at 17:29
  • 1
    \$\begingroup\$ Yes, that would definitely be handy! I'd also like to clear a single item, rotate items that aren't at the top of the stack, and maybe shift the top n items to another stack at once. For now, though, I still don't know how to compile dc from source. :/ \$\endgroup\$
    – juh
    Commented Aug 6, 2016 at 5:21
2
\$\begingroup\$

C# 110 109 107 103 101 94 Bytes

using i=System.Numerics.BigInteger;i f(i n){i a=0,b=1,x=1;for(;n-->0;)x*=a=(b+=a)-a;return x;}

Explanation

//Aliasing BigInteger saves a few bytes
using i=System.Numerics.BigInteger;

//Since BigInteger has an implicit from int we can also change the input
//and save two more bytes.
i f(i n)
{
    //using an alternative iterative algorithm (link to source below) to cut out the temp variable
    //b is next iteration, a is current iteration, and x is the running product
    i a = 0, b = 1, x = 1; 

    //post decrement n down to zero instead of creating a loop variable
    for (; n-- > 0;)

        //The bracket portion sets the next iteration             
        //get the current iteration and update our running product
        x *= a = (b += a) - a;

    return x;
}

Iterative Fib algorithm

\$\endgroup\$
6
  • \$\begingroup\$ Given this performed so much better than I had expected I wanted to find the max N that would return in under 5 seconds, I came out with 1540 which gives a number that is 247441 digits long. \$\endgroup\$
    – user19547
    Commented Aug 11, 2016 at 23:36
  • \$\begingroup\$ Impressive. How long does 1541 take to calculate, out of curiosity? \$\endgroup\$
    – Pandacoder
    Commented Aug 15, 2016 at 22:25
  • \$\begingroup\$ @Pandacoder So with the recent change to the algorithm it has gotten significantly faster. 1541 in 755 ms so i'm going to find the new sub 5 max now. \$\endgroup\$
    – user19547
    Commented Aug 15, 2016 at 22:29
  • \$\begingroup\$ @Pandacoder so the run time is varying by a fair bit ~100ms but 2565 seems to average about 5 seconds \$\endgroup\$
    – user19547
    Commented Aug 15, 2016 at 22:44
  • \$\begingroup\$ How long is the number for that? \$\endgroup\$
    – Pandacoder
    Commented Aug 15, 2016 at 22:45
2
\$\begingroup\$

Perl 6, 23 bytes

{[*] (1,&[+]...*)[^$_]}

Try it online!

  • 1, &[+] ... * is the infinite Fibonacci sequence.
  • [^$_] takes the first $_ elements of the sequence, where $_ is the argument to the function.
  • [*] reduces that subsequence with multiplication.
\$\endgroup\$
2
\$\begingroup\$

Brain-Flak, 54 bytes

([{}]<(())>){(({})()<{({}()<([({})]({}{}))>)}{}{}>)}{}

Try it online!

Multiplication in Brain-Flak takes a long time for large inputs. Just multiplying F100 by F99 with a generic multiplication algorithm would take billions of years.

Fortunately, there's a faster way. A generalized Fibonacci sequence starting with (k, 0) will generate the same terms as the usual sequence, multiplied by k. Using this observation, Brain-Flak can multiply by a Fibonacci number just as easily as it can generate Fibonacci numbers.

If the stack consists of -n followed by two numbers, {({}()<([({})]({}{}))>)}{}{} will computen iterations of the generalized Fibonacci sequence and discard all by the last. The rest of the program just sets up the initial 1 and loops through this for all numbers in the range n...1.

Here's the same algorithm in the other languages provided by this interpreter:

Brain-Flak Classic, 52 bytes

({}<(())>){(({})[]<{({}[]<(({}<>)<>{}<>)>)}{}{}>)}{}

Try it online!

Brain-Flueue, 58 bytes

<>(<(())>)<>{(({}<>)<{({}({}))({}[()])}{}>[()]<>)}<>({}){}

Try it online!

Mini-Flak, 62 bytes

([{}()](())){(({})()({({}()([({})]({}{}))[({})])}{}{})[{}])}{}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 6 4 bytes

LÅfP

Try it online!

-1 from Robbie0630, -1 more from an improvement on his suggestion.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ @robbie0630 LÅfP for 4, haven't looked here in a long time ;), you have to actually be careful with it doesn't work on 2-byte commands. Also, with L, many things vectorize automatically (even list commands) so you don't need an iterator :). \$\endgroup\$ Commented Feb 20, 2018 at 17:14
  • \$\begingroup\$ Beat me to it :P \$\endgroup\$
    – Makonede
    Commented Dec 20, 2020 at 19:15
2
\$\begingroup\$

tinylisp, 83 81 bytes

(load library
(d F(q((N)(i(l N 3)1(a(F(s N 1))(F(s N 2
(d P(q((N)(product(map F(0to N

Try it online!

-2 bytes thanks to DLosc for reminding me to remove parentheses.

tinylisp, 108 bytes

(d F(q((N)(i(l N 3)1(a(F(s N 1))(F(s N 2
(d M(q((A B)(i A(a B(M(s A 1)B))0
(d P(q((N)(i N(M(F N)(P(s N 1)))1

Try it online!

Exceeds the maximum recursion depth around N=14.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ @DLosc augh! that's what I get for copy-pasting the recursive call. \$\endgroup\$
    – Giuseppe
    Commented Apr 14, 2022 at 18:00
1
\$\begingroup\$

Mathematica - 32 26 bytes

Fibonacci@i~Product~{i,#}&

@MartinEnder chopped 6 bytes!

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

GAP 28 Bytes

Didn't know before today that GAP has a Fibonacci builtin.

n->Product([1..n],Fibonacci)
\$\endgroup\$
2
  • \$\begingroup\$ Can you link to GAP? No idea what it is. \$\endgroup\$
    – Leaky Nun
    Commented Jul 30, 2016 at 8:38
  • \$\begingroup\$ Oh sure (but I'm not the first one to use it here...) \$\endgroup\$ Commented Jul 30, 2016 at 8:44
1
\$\begingroup\$

Ruby, 85 Bytes

g =->x{(1..x).to_a.collect{|y|(0..y).inject([1,0]){|(a,b),_|[b, a+b]}[0]}.inject(:*)}

Turned out fine, but there's probably a shorter solution.

Fast Fibonnaci calculation taken from here: link

Test it here

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

Julia, 36 bytes

!x=[([1 1;1 0]^n)[2]for n=1:x]|>prod
\$\endgroup\$

Your Answer

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

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