Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- The Leibniz formula for π is as follows:
- π = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - ...)
- More info at:
- https://www.cuemath.com/calculus/leibniz-rule/
- Watch out Python will throw an error at a key point.
- """
- def calculate_pi(n, i=1, sum=0, sign=1):
- if n == 0:
- return 4 * sum
- else:
- term = sign * (1 / i)
- print(term)
- return calculate_pi(n - 1, i + 2, sum + term, -sign)
- # Test the function
- approx_pi = calculate_pi(800) # Increase the number of iterations for a more accurate result
- print(approx_pi)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement