Python calculating Catalan Numbers

In addition to xnx's answer, note that starting Python 3.8, with the addition of math.comb (binomial coefficient) in the standard library, we can also calculate Catalan numbers as such:

import math

def catalan(n):
  return math.comb(2*n, n) / (n+1)

catalan(511) # 2.1902514917394773e+303

I assume you're using Python 3.

Your res /= (i + 1) should be res //= (i + 1) to force integer arithmetic:

def BinominalCoefficient(n,k):
    res = 1
    if (k > n - k):
        k = n - k
    for i in range(k):
        res *= (n - i)
        res //= (i + 1)
    return res
def CatalanNumbers(n):
   c = BinominalCoefficient(2*n, n)
   return (c//(n+1))
print (CatalanNumbers(511))



You get nan because the divison /= in Python 3 returns a float which overflows to inf.