The Fibonacci polynomials are a polynomial sequence defined as:
- \$F_0(x) = 0\$
- \$F_1(x) = 1\$
- \$F_n(x) = x F_{n-1}(x) + F_{n-2}(x)\$
The first few Fibonacci polynomials are:
- \$F_0(x) = 0\$
- \$F_1(x) = 1\$
- \$F_2(x) = x\$
- \$F_3(x) = x^2 + 1\$
- \$F_4(x) = x^3 + 2x\$
- \$F_5(x) = x^4 + 3x^2 + 1\$
When you evaluate the Fibonacci polynomials for \$x=1\$, you get the Fibonacci numbers.
Task
Your task is to calculate the Fibonacci polynomial \$F_n(x)\$.
The usual sequence rules apply. So you may:
- Output all the Fibonacci polynomials.
- Take an input \$n\$ and output the \$n\$-th Fibonacci polynomial.
- Take an input \$n\$ and output the first \$n\$ Fibonacci polynomial.
You may use \0ドル\$-indexing or \1ドル\$-indexing.
You may output the polynomials in any reasonable format. Here are some example formats:
- a list of coefficients, in descending order, e.g. \$x^9+8x^7+21x^5+20x^3+5x\$ is represented as
[1,0,8,0,21,0,20,0,5,0]; - a list of coefficients, in ascending order, e.g. \$x^9+8x^7+21x^5+20x^3+5x\$ is represented as
[0,5,0,20,0,21,0,8,0,1]; - a function that takes an input \$n\$ and gives the coefficient of \$x^n\$;
- a built-in polynomial object.
You may pad the coefficient lists with \0ドル\$s. For example, the polynomial \0ドル\$ can represented as [], [0] or even [0,0].
You may also take two integers \$n, k\$, and output the coefficient of \$x^k\$ in \$n\$-th Fibonacci polynomial. You may assume that \$k<n\$.
This is code-golf, so the shortest code in bytes wins.
Testcases
Here I output lists of coefficients in descending order.
0 -> []
1 -> [1]
2 -> [1, 0]
3 -> [1, 0, 1]
4 -> [1, 0, 2, 0]
5 -> [1, 0, 3, 0, 1]
6 -> [1, 0, 4, 0, 3, 0]
7 -> [1, 0, 5, 0, 6, 0, 1]
8 -> [1, 0, 6, 0, 10, 0, 4, 0]
9 -> [1, 0, 7, 0, 15, 0, 10, 0, 1]
10 -> [1, 0, 8, 0, 21, 0, 20, 0, 5, 0]
11 -> [1, 0, 9, 0, 28, 0, 35, 0, 15, 0, 1]
12 -> [1, 0, 10, 0, 36, 0, 56, 0, 35, 0, 6, 0]
13 -> [1, 0, 11, 0, 45, 0, 84, 0, 70, 0, 21, 0, 1]
14 -> [1, 0, 12, 0, 55, 0, 120, 0, 126, 0, 56, 0, 7, 0]
15 -> [1, 0, 13, 0, 66, 0, 165, 0, 210, 0, 126, 0, 28, 0, 1]
16 Answers 16
J, (削除) 13 (削除ここまで) 11 bytes
1ドルj1#]!&i.-
-2 bytes thanks to @ovs
Looking at the nonzero columns, some keen eyes may have noticed that the columns look like binomial coefficients. And it actually is. (A related fact: summing the coefficients of nth polynomial gives nth Fibonacci number, which you can prove nicely in Lean)
...though we also need to handle the zero columns.
1ドルj1#]!&i.- A train that takes n, and gives a vector of descending coeffs
] - n and -n respectively
&i. Apply range to both sides; 0..n-1 and n-1..0
! (right)C(left)
1ドルj1# Insert a zero after each number and take first n numbers
Jelly, 5 bytes
Ż+\¡1
Full program taking \$n\$ from STDIN and outputting coefficients ascending.
It's always felt a bit silly that the only "repeat" quick is designed for Fibonacci-like recurrences, but when it comes up I can't say it isn't useful.
Since this is invoked as niladic, the initial left argument is 0.
1 Starting with 1 on the right,
¡ repeat n times
\ with the previous left argument replacing the right argument:
+ vectorized add the right argument to
Ż the left argument with a prepended 0.
R, 33 bytes
\(n,k,s=n+k)choose(s/2-.5,k)*s%%2
Takes input as \$n,k\$ and outputs the coefficient of \$x^k\$ in \$n\$-th Fibonacci polynomial. Uses the formula from Wikipedia:
\$F(n,k) = {\frac{n+k-1}{2}\choose k}\$ if \$n\$ and \$k\$ have opposite parity.
Wolfram Language (Mathematica), 14 bytes
#~Fibonacci~x&
Returns a polynomial in \$x\$.
If \$n,x\$ were valid input, Fibonacci alone would do.
Python, 62 bytes (@AnttiP)
f=lambda n:n>1and[*map(sum,zip(f(n-1)+[0]),[0,0]+f(n-2))]or[n]
Old Python, 64 bytes
f=lambda n:n>1and[*map(sum,zip(f(n-1)+[0]),[0,0]+f(n-2))]or[1]*n
Naive implementation of the defining recurrence.
-
\$\begingroup\$ -2 bytes with
[1]*n=>[n]\$\endgroup\$AnttiP– AnttiP2022年06月27日 06:07:56 +00:00Commented Jun 27, 2022 at 6:07
Vyxal, 8 bytes
ʁḂ$ƈ0ZfẎ
Same idea as Bubbler's J answer.
ʁḂ$ƈ0ZfẎ
ʁ # Exclusive zero range, 0..n-1
Ḃ # Bifurcate, push reverse without popping
$ # Swap
ƈ # Binomial coefficients
0Zf # Append zero after each
Ẏ # Only keep the first input items
Porting pajonk's Python answer is 8 bytes as well:
Vyxal, 8 bytes
+=∷1⁄2⌊0ƈ*
Python 2, 43 bytes
lambda n,k:2**(n*(n-~k))/(4**n-1)**-~k%2**n
Uses an arithmetic expression for the binomial adapted to \$ \binom{\frac{n+k-1}2}{k}\$ in a way that gives 0 if the top isn't a whole number.
Python 2, 48 bytes
f=lambda n,k:n>0and(k*k+n<2)+f(n-1,k-1)+f(n-2,k)
A recursive expression taken from tsh for Pascal's triangle but tilted.
Python3, 139 bytes:
def f(n):d={};F(n,d);return[d.get(i,0)for i in range(n-1,-1,-1)]
def F(n,d,c=0):
if n==1:d[c]=d.get(c,0)+1
if n>1:F(n-1,d,c+1);F(n-2,d,c)
-
2\$\begingroup\$
range(n-1,-1,-1)isrange(n)[::-1]=> -2; walrus operator lets you define a variable in a parameter list:F(n,d:={})=> -1; you can useelseinstead ofif n==1if you use+ninstead of+1=> -3; changingfinto alambdalets you useorinstead of;return=> -3. You are also technically allowed to return the coefficients in ascending order so you can userange(n)for an extra 6 bytes. \$\endgroup\$301_Moved_Permanently– 301_Moved_Permanently2022年06月27日 21:23:54 +00:00Commented Jun 27, 2022 at 21:23
Charcoal, 32 bytes
×ばつκ∨λXχθI↨⊟υXχθ
Try it online! Link is to verbose version of code. Takes n as input and outputs the list of coefficients in descending order. Explanation:
Nθ
Input n.
F⊕θ
Repeat n+1 times.
×ばつκ∨λXχθ
Except for the first two numbers (which are just 0 and 1) multiply the last number by a very large base (10n) and add on the penultimate number.
I↨⊟υXχθ
Interpret the last value as a number in that very large base and output the "digits".
Python, (削除) 58 (削除ここまで) (削除) 52 (削除ここまで) 50 bytes
Edit: -8 bytes thanks to 301_Moved_Permanently.
lambda n,k:(n+k)%2*math.comb(n+k>>1,k) import math
Port of my R answer.
-
1\$\begingroup\$ You can change the
andto*for -3 bytes, you can also drop the-1asn+kis guaranteed an odd number and//2will floor the result. \$\endgroup\$301_Moved_Permanently– 301_Moved_Permanently2022年06月27日 13:16:22 +00:00Commented Jun 27, 2022 at 13:16 -
\$\begingroup\$ If you change
(n+k)to(n:=n+k)(Python 3.8+), you can change the(n+k-1)to~-nfor -1. EDIT: Nvm, removing the-1is indeed shorter, as mentioned in the comment above. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年06月27日 13:17:54 +00:00Commented Jun 27, 2022 at 13:17 -
\$\begingroup\$ Also using
math.combandimport mathrather thanfrom math import*gives you -1 byte. \$\endgroup\$301_Moved_Permanently– 301_Moved_Permanently2022年06月27日 13:23:33 +00:00Commented Jun 27, 2022 at 13:23 -
1\$\begingroup\$ Order of operations:
//2and>>1are equivalent, but the latter have lower priority than addition, so you can drop parenthesis. \$\endgroup\$301_Moved_Permanently– 301_Moved_Permanently2022年06月27日 20:30:52 +00:00Commented Jun 27, 2022 at 20:30
05AB1E, (削除) 8 (削除ここまで) 6 bytes
λ0š0ζO
Outputs the infinite 1-based sequence. The inner coefficient-lists are output in ascending order for -2 bytes.
Since I was curious, a port of @Bubbler's J answer would be 9 bytes :
Ý ̈Âc3ドル⁄4RI£
Outputs the 0-based \$n^{th}\$ list, in descending order.
Try it online or verify all test cases.
Explanation:
λ # Create a recursive environment,
# to result in the infinite sequence
# Implicitly starting at a(0)=1
# Where every following a(n) is calculated as:
# (implicitly push the a(n-2)'th and a(n-1)'th terms
# (where a(-1)=0 in the first iteration)
0š # Prepend a 0 in front of the top a(n-1)'th term
# (which implicitly converts integers to digit-lists first)
ζ # Pair and zip/transpose the two lists,
0 # with 0 as filler since the lists are of unequal lengths
O # Sum each inner pair
# (after which the infinite sequence is output implicitly)
Ý # Push a list in the range [0, (implicit) input]
̈ # Remove the last item to make the range [0,input)
 # Bifurcate; short for Duplicate & Reverse copy
c # Calculate the binomial coefficient of the values in the two lists at
# the same positions
3ドル⁄4 # Prepend a 0 in front of each item
R # Reverse this list
I£ # Keep just the first input amount of items
# (after which this list is output implicitly as result)
-
\$\begingroup\$
+DÉ*<;²calso works for 8 bytes, outputting thek-th coefficient in then-th polynomial \$\endgroup\$Command Master– Command Master2022年06月30日 04:12:45 +00:00Commented Jun 30, 2022 at 4:12 -
\$\begingroup\$ @CommandMaster That seems to output the 0-based \$(n-k-1)^{th}\$ coefficient (or 1-based \$(n-k)^{th}\$) in the \$n^{th}\$ polynomial, though. E.g.
n=11,k=2outputs15instead of9: try it online. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年06月30日 07:23:19 +00:00Commented Jun 30, 2022 at 7:23 -
1\$\begingroup\$ The 11-th Fibonacci polynomial is \$ x^{10} + 9 x^8 + 28 x^6 + 35 x^4 + 15 x^2 + 1\,ドル and 15 is the coefficient of \$ x^2 \,ドル so 15 is a correct answer \$\endgroup\$Command Master– Command Master2022年06月30日 12:02:17 +00:00Commented Jun 30, 2022 at 12:02
-
\$\begingroup\$ @CommandMaster Good point. I was still looking at it from the descending list perspective, but if you explain it like that it indeed makes total sense! But maybe it's better if you post it as a separated answer? It's quite a bit different than mine, and I also already golfed 2 bytes off it. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年06月30日 12:05:32 +00:00Commented Jun 30, 2022 at 12:05
Desmos, (削除) 62 (削除ここまで) 41 bytes
L=[n...0]
f(n)=mod(L+n,2)nCr((n+L-1)/2,L)
Function f outputs a list of coefficients in descending order, with a leading zero.
Explore related questions
See similar questions with these tags.
[0]rather than empty? \$\endgroup\$[0,1]for \$n=1\$. \$\endgroup\$