The ubiquitous Catalan numbers \$C_n\$ count the number of Dyck paths, sequences of up-steps and down-steps of length \$2n\$ that start and end on a horizontal line and never go below said line. Many other interesting sequences can be defined as the number of Dyck paths satisfying given conditions, of which the Fine sequence \$F_n\$ (not the Fibonacci numbers and not related to any common definition of "fine") is one.

Let a hill be a sequence of an up-step followed by a down-step that starts – and therefore also ends – on the horizontal line. \$F_n\$ is then the number of Dyck paths of length \$2n\$ with no hills. The picture below illustrates this: there are \$C_5=42\$ Dyck paths of length \$10\$, of which \$F_5=18\$ (marked in black) have no hills.

This sequence is OEIS A000957 and begins $$\begin{array}{c|ccccccccccc} n&0&1&2&3&4&5&6&7&8&9&10\\ \hline F_n&1&0&1&2&6&18&57&186&622&2120&7338 \end{array}$$ $$\begin{array}{c|ccccccccccc} n&11&12&13&14&15\\ \hline F_n&25724&91144&325878&1174281&4260282 \end{array}$$

Other things counted by the Fine numbers include

  • the number of Dyck paths of length \$2n\$ beginning with an even number of up-steps
  • the number of ordered trees with \$n+1\$ vertices where the root has no leaf children
  • the number of ordered trees with \$n+1\$ vertices where the root has an even number of children
  • and so on. For more interpretations see Deutsch and Shapiro's "A survey of the Fine numbers".


You may use any correct formula to generate the sequence. Here are some:

  • The generating function is $$\sum_{n=0}^\infty F_nz^n=\frac1z\cdot\frac{1-\sqrt{1-4z}}{3-\sqrt{1-4z}}$$
  • For \$n\ge1\$, \$C_n=2F_n+F_{n-1}\$.
  • An explicit formula: $$F_n=\frac1{n+1}\sum_{k=0}^n(-1)^k(k+1)\binom{2n-k}{n-k}$$


Standard rules apply to this challenge, where permissible behaviours are

  • outputting the \$n\$th term in 0- or 1-based indexing given \$n\$
  • outputting the first \$n\$ terms given \$n\$
  • outputting the infinite sequence with no input, either by printing or returning a lazy list/generator

This is ; fewest bytes wins.


15 Answers 15


Python 2, 58 bytes

while 1:print a;i+=1;a,b=2*b+7*a/2-(2*a+b)*3/i,a

Try it online!

Prints the sequence endlessly. The method substitutes \$C_n = 2F_n + F_{n-1} \$ into the Catalan recurrence $$ C_n = 4C_{n-1} - \left\lfloor \frac{6C_{n-1}}{n+1} \right\rfloor $$

  • 1
    \$\begingroup\$ @dingledooper awesome. I think you should post your own answer, it's sufficently different (and more clever) \$\endgroup\$
    – Sisyphus
    Feb 23 at 1:56

Vyxal, 22 10 7 bytes

-12 bytes by emanresu A (double welp)
-3 bytes by alephalpha using a clever approach


Try it Online!

How it works

ꜝ$               Bitwise not n and swap with input
  ʀƈ             Take the binomial coefficient
    ¦ṁȧ          Cumultative sum, take the mean and push the absolute value
  • 4
    \$\begingroup\$ 15 \$\endgroup\$
    – emanresu A
    Feb 22 at 10:16
  • 3
    \$\begingroup\$ @emanresuA ... wth man \$\endgroup\$
    – math scat
    Feb 22 at 10:42
  • 4
    \$\begingroup\$ 10 (several tricks stolen from Unrelated String) \$\endgroup\$
    – emanresu A
    Feb 22 at 11:27
  • 3
    \$\begingroup\$ 7 \$\endgroup\$
    – alephalpha
    Feb 24 at 9:11

HOPS, 19 bytes


Attempt This Online!

The generating function of the Catalan numbers satisfies \$C(x)=1+x\ C(x)^2\$. The generating function of this sequence is \$F(x)=C(x)/(1+x\ C(x))\$.

HOPS, 21 bytes


Attempt This Online!

A simplified version of the generating function.

It seems that HOPS on ATO is currently not working.


Python 3, 61 bytes

f=lambda n,v=1:sum(f(i,0)*f(n+~i,v)for i in range(v,n))or 1-n

Try it online!

JavaScript (Node.js), 50 bytes


Try it online!

Another 47 bytes JavaScript answer but output true and false for f(0), f(1).

First, we write down following recursive formula:

$$F_n=\sum_{i=1}^{n-1}C_i\cdot F_{n-i-1}$$ $$F_1=0, F_0=1$$ $$C_n=\sum_{i=0}^{n-1}C_i\cdot C_{n-i-1}$$ $$C_0=1$$

Then, we try to merge two functions into one

$$f(x,1):=F_x$$ $$f(x,0):=C_x$$ $$f(x,v)=\begin{cases}\sum_{i=v}^{x-1}f(i,0)\cdot f(x-i-1,v) & x>v\\ 0 & x=v=1 \\ 1 & x=0 \end{cases}$$


JavaScript (ES6), 48 bytes

Returns the \$n\$-th term (0-indexed).

Inspired by the Catalan recurrence pointed out by Sisyphus.


Try it online!

JavaScript (ES6), 60 bytes

Returns the \$n\$-th term (0-indexed).

This is based on the explicit formula provided in the challenge.


Try it online!


Jelly, 12 11 bytes


Try it online!

Port of emanresu A's golf to mathcat's Vyxal solution.

Ḥrc            Each of [2n .. n] choose n.
   µ    ḋ      Take the dot product of that with
    J          [1 .. n+1]
     NÐe       with every other element negated,
         :     and (floor) divide that by
   µ      L    n+1.

Jelly, 18 bytes


Try it online!

Haven't actually tried any closed form or recursive formulae, but ignoring the special case fix for \$n=0\$ this brute-force enumeration feels elegant enough to post, especially considering how the current golflang closed form solutions are. Leverages the "even number of leading up-steps" interpretation--or rather, "first down-step at an odd 1-index", hence the special case.

o2µ                   If n is 0, from here on out, pretend it's 2.
    œc                Get every combination of n elements from
   Ḥ                  [1 .. 2n],
      µ         )     then for each combination:
       Ṭ-*            Produce a list of -1 at those indices and 1 elsewhere,
          Ä           take its cumulative sums,
           AƑ         and check that none of those is negative.
             ×        Multiply that result by
              Ṃ       the smallest element of the combination.
               Ḃ S    How many are odd?
  • 1
    \$\begingroup\$ Why is the 12-byter funnier? \$\endgroup\$ Feb 22 at 11:08
  • \$\begingroup\$ @ParclyTaxel Explanation pending \$\endgroup\$ Feb 22 at 11:09
  • 1
    \$\begingroup\$ Wow, nice port! I tried porting it to Jelly but couldn't find the binomial coefficient builtin. \$\endgroup\$
    – emanresu A
    Feb 22 at 11:21

05AB1E, 22 21 14 bytes


Port of @mathcat's Vyxal answer, so make sure to upvote him as well!

Uses the formula: $$G_{n,k} = (-1)^{(k+1)}\binom{k}{n} + G_{n-1,k}\\ F_n=\left|\frac{1}{n+1}\sum_{k=n}^{2n}G_{n,k}\right|$$
Given \$n\$, it'll output \$F_n\$.

Try it online or verify all test cases.


x              # Push double the (implicit) input (without popping)
 Ÿ             # Pop both, and push a list in the range [input,2*input]
  Ic           # Calculate the bionomical coefficient of each value with the input
    2Å€(}      # Negate every second item, starting with the first:
    2          #  Push a 2
     ŀ }      #  Map over each item where the 0-based index is divisible by 2:
       (       #   Negate that item
         ηO    # Calculate its cumulative sum:
         η     #  Get all prefixed of this list
          O    #  Sum each inner prefix-list together
           ÅA  # Get the arithmetic mean of that
             Ä # Convert it to its absolute value
               # (after which it is output implicitly as result)

Original 22 21 bytes answer:


-1 byte thanks to @emanresuA

Uses the given explicit formula: $$F_n=\sum_{k=0}^n(-1)^k(k+1)\binom{2n-k}{n-k}\frac1{n+1}$$
Given \$n\$, it'll output \$F_n\$.

Try it online or verify all test cases.


Ý              # Push a list in the range [0, (implicit) input]
 ε             # Map each integer to:
  È            #  Check whether it's even
   ·           #  Double that check (2 if even; 0 if odd)
    <          #  Decrease it by 1 (1 if event; -1 if odd)
  y>           #  Push the current integer, and decrease it by 1
  I            #  Push the input
   x           #  Double it (without popping)
    s‚         #  Swap, and pair them together: [2n,n]
      y-       #  Subtract the current integer from each: [2n-k,n-k]
        `      #  Pop and push the values back to the stack
         c     #  Calculate their binomial coefficient
  I>z          #  Push 1/(input+1)
  P            #  Take the product of the four values on the stack
 }O            # After the map: sum them together
               # (after which the result is output implicitly)
  • 2
    \$\begingroup\$ I don't know 05AB1E but this seems to work? \$\endgroup\$
    – emanresu A
    Feb 22 at 10:20
  • \$\begingroup\$ @emanresuA Thanks. And porting mathcat's Vyxal answer is even 7 bytes shorter. \$\endgroup\$ Feb 22 at 17:55

Python 2, 49 bytes

Prints the sequence indefinitely.

while 1:print x;n+=1;c=c*4-c*6/n;x=c-x>>1

Try it online!

Based on the Sage program at the end of the OEIS page.


Desmos, 46 41 bytes


A port of the explicit formula but with one-indexing instead of zero-indexing.

Try It On Desmos!

Try It On Desmos! - Prettified

Proof for the formula:

We start with the explicit formula given in the question: $$F(n)=\frac1{n+1}\sum_{k=0}^n(-1)^k(k+1)\binom{2n-k}{n-k}$$ Then convert it to one-indexing (making a new function \$f\$) by doing the following: $$f(n)=F(n-1)=\frac1n\sum_{k=0}^{n-1}(-1)^k(k+1)\binom{2n-k-2}{n-k-1}$$ From there, shift the bounds of the summation up by one, making sure to correct the shift within the summation: $$\frac1n\sum_{k=0}^{n-1}(-1)^k(k+1)\binom{2n-k-2}{n-k-1}=\frac1n\sum_{k=1}^n(-1)^{k-1}k\binom{2n-k-1}{n-k}$$ Factoring out a \$-1\$ gives the formula used in my answer: $$f(n)=-\frac1n\sum_{k=1}^n(-1)^kk\binom{2n-k-1}{n-k}$$


PARI/GP, 36 bytes


Attempt This Online!

Using the generating function.

PARI/GP, 39 bytes


Attempt This Online!

Using this interesting formula on OEIS:

a(n) = the upper left term in M^n, n>0; where M = the infinite square production matrix:
0, 1, 0, 0, 0, 0, ...
1, 1, 1, 0, 0, 0, ...
1, 1, 1, 1, 0, 0, ...
1, 1, 1, 1, 1, 0, ...
1, 1, 1, 1, 1, 1, ...
- Gary W. Adamson, Jul 14 2011 

Jelly, 7 bytes


This calculates abs(mean(cumsum(choose(-n-1, [0..n])))).

Try it online!

The "negative binomial coefficients" are defined as $$\binom{-a}{b} = (-1)^b \binom{a+b-1}{b}.$$ And they are supported by Jelly's c. So we can rewrite the closed form as:

\begin{align} F_n&=\frac{1}{n+1}\sum_{k=0}^n(-1)^k(k+1)\binom{2n-k}{n-k} \\ & \color{gray}{\text{(introduce negative binomials:)}} \\ &=\frac{1}{n+1}\sum_{k=0}^n(-1)^k(k+1) \color{#0bf}{(-1)^{n-k} \binom{-n-1}{n-k}} \\ & \color{gray}{\text{(factor out powers of $-1$:)}} \\ &=(-1)^n \frac{1}{n+1} \sum_{k=0}^n (k+1) \binom{-n-1}{n-k} \\ & \color{gray}{\text{(substitute $j=n-k$:)}} \\ &=(-1)^n \frac{1}{n+1} \sum_{j=0}^n (n+1-j) \binom{-n-1}{j} \end{align}

The ~cŻ generates the negative binomials \$\binom{-n-1}{0}\$ through \$\binom{-n-1}{n}\$.

Then we use an obscure trick: we can calculate \$\sum_{j=0}^n (n+1-j) \cdot z_j\$ as sum(cumsum(z)), or ÄS in Jelly parlance. But then because we want to divide by \$n+1\$ immediately after, and our list has \$n+1\$ elements, we can write Æm (mean) instead of S (sum).

Here's an example of why this works: \begin{align} & \textrm{sum}(\textrm{cumsum}([a,b,c,d])) \\ =~& (a) + (a+b) + (a+b+c) + (a+b+c+d) \\ =~& 4a+3b+2c+d \end{align}

Finally we still have to multiply by \$(-1)^n\$ to fix the sign. But because we know the Fine numbers are never negative, we can just take the absolute value with A.


Factor + koszul math.combinatorics, 69 bytes

[ 4 dupn + [a,b] [ -1^ -rot nCk * ] with map-index cum-sum mean abs ]

Try it online!

"Mathy" answers cause far too much whitespace in Factor so this is a port of mathcat's Vyxal answer.


Charcoal, 25 bytes


Attempt This Online! Link is to verbose version of code. Explanation:


Start with C(0) = 1.


Start with F(0) = 1.


Loop n times.


Calculate the next Catalan number from the dot product of the list of numbers with its reverse and use that to calculate the next Fine number.


Output F(n).


Pyt, 32 bytes


Try it online!

Naive implementation of the formula in the question.

The following walkthrough of the code is worked on an input of n=3

Code Stack Action
Đ 3 3 implicit input; Đuplicate
3 4 increment
Đ 3 4 4 Đuplicate
ř 3 4 [1,2,3,4] řangify
3 4 [0,1,2,3] decrement
ĐĐ 3 4 [0,1,2,3] [0,1,2,3] [0,1,2,3] Đuplicate twice
05Ș 3 0 [0,1,2,3] [0,1,2,3] [0,1,2,3] 4 Push 0, then Șwap the top 5 items on the stack
4 [0,1,2,3] [0,1,2,3] [0,1,2,3] 0 3 Flip the entire stack
+ 4 [0,1,2,3] [0,1,2,3] [0,1,2,3] 3 Remove that pesky 0 by adding
4 [0,1,2,3] [0,1,2,3] [0,1,2,3] 3 [1,2] Push 2 and řangify
* 4 [0,1,2,3] [0,1,2,3] [0,1,2,3] [3,6] Multiply
4 [0,1,2,3] [0,1,2,3] [3,6] [0,1,2,3] Swap the top two items on the stack
ɐ- 4 [0,1,2,3] [0,1,2,3] [[3,2,1,0],[6,5,4,3]] For ɐll pairs of values, subtract
Á 4 [0,1,2,3] [0,1,2,3] [3,2,1,0] [6,5,4,3] Push contents of Árray to stack
4 [0,1,2,3] [0,1,2,3] [6,5,4,3] [3,2,1,0] Swap the top two items on the stack
ć 4 [0,1,2,3] [0,1,2,3] [20,10,4,1] nCr
4 [0,1,2,3] [20,10,4,1] [0,1,2,3] Swap top two items on stack
4 [0,1,2,3] [20,10,4,1] [1,2,3,4] Increment
* 4 [0,1,2,3] [20,20,12,4] multiply element-wise
4 [20,20,12,4] [0,1,2,3] Swap top two items
1~ 4 [20,20,12,4] [0,1,2,3] -1 Push 1, then negate
⇹^ 4 [20,20,12,4] [1,-1,1,-1] Swap top two, then exponentiate
· 4 8 Dot product
⇹/ 2 Swap top two items, then divide; implicit print

Mathematica, 51 bytes

It's A000957

Use the generating function

$$\sum_{n=0}^\infty F_nz^n=\frac1z\cdot\frac{1-\sqrt{1-4z}}{3-\sqrt{1-4z}}$$

Try it online!


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.