Skip to content

Instantly share code, notes, and snippets.

@tantalor tantalor/catalan.py
Created Jun 3, 2012

Embed
What would you like to do?
linear algorithm to generate catalan numbers
def catalan():
"""Yields catalan numbers: 1 1 2 5 14 42 132..."""
n = 0
c = 1
while 1:
yield c
n = n+1
c = c * 2 * (2*n-1) / (n+1)
# print the first ten catalan numbers
from itertools import islice
for n, c in enumerate(islice(catalan(), 10)):
print n, c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.