title: Gauss-Legendre Algorithm in python
body: <p>I need some help calculating Pi. I am trying to write a python program that will calculate Pi to X digits. I have tried several from the python mailing list, and it is to slow for my use. 
  I have read about the <a href="http://en.wikipedia.org/wiki/Gauss-Legendre_algorithm" rel="noreferrer">Gauss-Legendre Algorithm</a>, and I have tried porting it to Python with no success.</p>
  
  <p>I am reading from <a href="http://www.geocities.com/hjsmithh/Pi/Gauss_L.html" rel="noreferrer">Here</a>, and I would appreciate any input as to where I am going wrong!</p>
  
  <p>It outputs: 0.163991276262</p>
  
  <pre><code>from __future__ import division
  import math
  def square(x):return x*x
  a = 1
  b = 1/math.sqrt(2)
  t = 1/4
  x = 1
  for i in range(1000):
      y = a
      a = (a+b)/2
      b = math.sqrt(b*y)
      t = t - x * square((y-a))
      x = 2* x
  
  pi = (square((a+b)))/4*t
  print pi
  raw_input()
  </code></pre>