Last active
September 25, 2015 14:53
-
-
Save gorakhargosh/a857438d6aae21ef9170 to your computer and use it in GitHub Desktop.
How to calculate pi, slowly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import division | |
def term(i): | |
# n = 4.0/(2*i + 1) | |
# return -n if i & 1 else n | |
return (-1)**i * 4.0/(2*i + 1) | |
def pi(n): | |
"""Pi up to n terms.""" | |
return sum(map(term, xrange(n))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment