Skip to content

Instantly share code, notes, and snippets.

@gorakhargosh
Last active September 25, 2015 14:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorakhargosh/a857438d6aae21ef9170 to your computer and use it in GitHub Desktop.
Save gorakhargosh/a857438d6aae21ef9170 to your computer and use it in GitHub Desktop.
How to calculate pi, slowly.
#!/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