#! /usr/bin/env python """ catalan.py is program which calculates increasing order the Catalan numbers up to 10 billion Paul Eugenio PHZ4151C Jan 25, 2019 """ # program header code from __future__ import division, print_function def catalan_number(num): """ catalan_number(num) function which calculates the catalan number C_n where C_0 = 1 and C_{n+1} = (4n+2)/(n+2) """ Cn, Cd, n = 1, 1, 0 if num == 0: return 1 else: while n <= num: # calculate the interger numerators and demoninators # and perform the integer division after the loop. # This provides better precision for large n. Cn *= 4*n + 2 Cd *= n + 2 n += 1 return Cn//Cd # # main # # initialize to C_0 = 1 n = 0 Cn = 1 maxCn = int(float(raw_input("Enter a maximum value: "))) # print out a table of all C_n values less than maxCn value # print("n\tC_n") while Cn <= maxCn: print(n, Cn, sep='\t') n +=1 Cn = catalan_number(n)