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

  • SEARCH
  • Home
  • 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 9274953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:28:29+00:00 2026-06-18T16:28:29+00:00

To start off, this is the problem. The mathematical constant π (pi) is an

  • 0

To start off, this is the problem.

The mathematical constant π (pi) is an irrational number with value approximately 3.1415928… The precise value of π is equal to the following infinite sum: π = 4/1 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + … We can get a good approximation of π by computing the sum of the first few terms. Write a function approxPi() that takes as a parameter a floating point value error and approximates the constant π within error by computing the above sum, term by term, until the absolute value of the difference between the current sum and the previous sum (with one fewer terms) is no greater than error. Once the function finds that the difference is less than error, it should return the new sum. Please note that this function should not use any functions or constants from the math module. You are supposed to use the described algorithm to approximate π, not use the built-in value in Python.

I’d really appreciate it if someone could help me understand what the problem is asking, since I’ve read it so many times but still can’t fully understand what it’s saying. I looked through my textbook and found a similar problem for approximating e using e’s infinite sum: 1/0! + 1/1! + 1/2! + 1/3!+…

def approxE(error):
    import math
    'returns approximation of e within error'
    prev = 1 # approximation 0
    current = 2 # approximation 1
    i = 2 # index of next approximation
    while current-prev > error:
        #while difference between current and previous
        #approximation is too large
                            #current approximation
        prev = current      #becomes previous
                            #compute new approximation
        current = prev + 1/math.factorial(i) # based on index i
        i += 1              #index of next approximation
    return current

I tried to model my program after this, but I don’t feel I’m getting any closer to the solution.

def approxPi(error):
    'float ==> float, returns approximation of pi within error'
    #π = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...
    prev = 4 # 4/1 = 4 : approx 0
    current = 2.6666 # 4/1 - 4/3 = 2.6666 : approx 1
    i = 5 # index of next approx is 5
    while current-prev > error:
        prev = current
        current = prev +- 1/i
        i = i +- 2
    return current

The successful program should return

approxPi(0.5) = 3.3396825396825403 and approxPi(0.05) = 3.1659792728432157

Again, any help would be appreciated. I’d like to just understand what I’m doing wrong in this.

  • 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. Editorial Team
    Editorial Team
    2026-06-18T16:28:30+00:00Added an answer on June 18, 2026 at 4:28 pm

    If you’re trying to approximate pi using that series, start by writing out a few terms:

    π = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...
         0     1     2     3     4     5     ...
    

    And then write a function that returns the nth term of the series:

    def nth_term(n):
        return 4 / (2.0 * n + 1) * (-1) ** n
    

    From there, the code is pretty generic:

    def approximate_pi(error):
        prev = nth_term(0)  # First term
        current = nth_term(0) + nth_term(1)  # First + second terms
        n = 2  # Starts at third term
    
        while abs(prev - current) > error:
            prev = current
            current += nth_term(n)
            n += 1
    
        return current
    

    It seems to work for me:

    >>> approximate_pi(0.000001)
        3.1415929035895926
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok this is a really weird problem. I wanna start off by saying that
To start off, this is probably an XY problem, sorry about that. I'm loading
Below is my requirement. @echo off cls Set Sleep=0 :start echo This is a
let's start off with the problem statement: My iOS application has a login form.
Let me start off by saying I'm not having a problem when rotating views
Basic problem here.. I will start off by asking that you please not respond
Let me start off by saying that this is my first real Cocoa app.
I've been having this problem for while now, but always seem to put off
Let me start this off by saying that I'm an intern with no Powershell
Okay To start off this is my first post so I apologize initially for

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.