Skip to content

Instantly share code, notes, and snippets.

@chand1012
Created October 13, 2018 03:14
Show Gist options
  • Save chand1012/1b00aa83fc422856ff1dc99e66aff01a to your computer and use it in GitHub Desktop.
Save chand1012/1b00aa83fc422856ff1dc99e66aff01a to your computer and use it in GitHub Desktop.
Catalan's constant calculator in python
# Simple Catalan's Constant implimentation in python
# Written for some homework, hope someone else finds it useful
# n being the times the number is calculated, increasing n increases accuracy
from fractions import Fraction
n = int(raw_input("enter N:"))
k = 1
sum = 0
sign = 1
while True:
if k<=n:
top = ((-1)**(k-1))
bottom = (((2*k)+1)**2)
frac = Fraction(top, bottom)
sum += frac
k+=1
else:
break
print(float(sum))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment