Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 85625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:07:08+00:00 2026-05-10T22:07:08+00:00

I need some help calculating Pi. I am trying to write a python program

  • 0

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 Gauss-Legendre Algorithm, and I have tried porting it to Python with no success.

I am reading from Here, and I would appreciate any input as to where I am going wrong!

It outputs: 0.163991276262

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() 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:07:08+00:00Added an answer on May 10, 2026 at 10:07 pm
    1. You forgot parentheses around 4*t:

      pi = (a+b)**2 / (4*t) 
    2. You can use decimal to perform calculation with higher precision.

      #!/usr/bin/env python from __future__ import with_statement import decimal  def pi_gauss_legendre():     D = decimal.Decimal     with decimal.localcontext() as ctx:         ctx.prec += 2                         a, b, t, p = 1, 1/D(2).sqrt(), 1/D(4), 1                         pi = None         while 1:             an    = (a + b) / 2             b     = (a * b).sqrt()             t    -= p * (a - an) * (a - an)             a, p  = an, 2*p             piold = pi             pi    = (a + b) * (a + b) / (4 * t)             if pi == piold:  # equal within given precision                 break     return +pi  decimal.getcontext().prec = 100 print pi_gauss_legendre() 

    Output:

    3.141592653589793238462643383279502884197169399375105820974944592307816406286208\     998628034825342117068 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Nehe have a port of most of their tutorials to… May 11, 2026 at 2:48 pm
  • added an answer First of all, those aren't Microsoft Word entities—they are UTF-8.… May 11, 2026 at 2:48 pm
  • added an answer You've run into a pretty specific case of code that… May 11, 2026 at 2:48 pm

Related Questions

I need to select a bunch of data into a temp table to then
I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this
#include <vector> std::vector<long int> as; long int a(size_t n){ if(n==1) return 1; if(n==2) return
I need some help from the shell-script gurus out there. I have a .txt

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.