Question

Define a well-commented main function in Python that determines the number of terms (N1, N2, and...

Define a well-commented main function in Python that determines the number of terms (N1, N2, and N3) required for each method of calculating pi to a given level of precision E = 10^-3. Store the number of terms in a dictionary with key/pair defined as {n1: ’myPiOne’, N2: ‘myPiTwo’, N3:’myPiThree’}. Sort the keys from low to high and print out the associated Method Number pair.

*Leibnitz, Adamchik, and Nilakantha to be used
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Problem: Find the number to terms it took for different approximations for PI and print sorted methods.

Attached images at the end for clarity and indentation errors.

Python Code Begin:

import math
def main():
terms_dict = {}
#Lebinitz approximation of pi
#pi = 4*(1 - 1/3 + 1/5 - 1/7 + 1/9....)
#Initializing pi and other values for Lebinitz method
denom = 1.0
multiplier = 1.0
pi = (4.0 / denom)
i=0
#Looping until error reaches threshod of 0.001
while(abs(math.pi-pi) > 1e-3):
denom += 2.0
multiplier *= -1.0
pi += ( (4.0 / denom) * multiplier )
i += 1
#print("It took {} terms to error be 0.001 and pi value is {}".format(i,pi))
terms_dict[i] = "Leibnitz"
  
#Adamchik approximation of pi
#pi = sum from i=0 to i = infinity((1/16)^i*(4/(8*i+1)-2/(8*i+4)-1/(8*i+5)-1/(8*i+6)))
#Initializing pi and other values for Adamchik method
pi=0
i=0
#Looping until error reaches threshod of 0.001
while(abs(math.pi-pi) > 1e-3):
term1 = 4.0/(8*i+1)
term2 = 2.0/(8*i+4)
term3 = 1.0/(8*i+5)
term4 = 1.0/(8*i+6)
pi += (1.0/16**i)*(term1-term2-term3-term4)
i += 1
#print("It took {} terms to error be 0.001 and pi value is {}".format(i,pi))
terms_dict[i] = "Adamchik"
  
#Nilakantha approximation of pi
#pi = 3 + (4/(2*3*4)-4/(4*5*6)+4/(6*7*8).....)
#Initializing pi and other values for Nilakantha method
multiplier = 1.0
denom = 2.0
pi = 3.0
i=0
#Looping until error reaches threshod of 0.001
while(abs(math.pi-pi) > 1e-3):
pi += ( (4.0 / (denom * (denom + 1.0) * (denom + 2.0)) ) * multiplier)
denom += 2.0
multiplier *= -1.0
i+=1
#print("It took {} terms to error be 0.001 and pi value is {}".format(i,pi))
terms_dict[i] = "Nilakantha"
for key in sorted(terms_dict):
print("{} method took {} terms to approximate pi with 0.001 error.".format(terms_dict[key],key))

Python Code End.

Add a comment
Know the answer?
Add Answer to:
Define a well-commented main function in Python that determines the number of terms (N1, N2, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python...

    # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are...

  • 1. Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys...

    1. Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are printed in alphabetical order. Note that...

  • python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in...

    python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in addition to being a regular dictionary (using inheritance), it also defines an auxiliary/attribute dictionary that uses the bidict’s values as keys, associated to a set of the bidict’s keys (the keys associated with that value). Remember that multiple keys can associate to the same value, which is why we use a set: since keys are hashable (hashable = immutable) we can store them in...

  • PYTHON Define the remove_e_synonyms() function which is passed a dictionary as a parameter. The keys of...

    PYTHON Define the remove_e_synonyms() function which is passed a dictionary as a parameter. The keys of the parameter dictionary are words and the corresponding values are lists of synonyms (synonyms are words which have the same or nearly the same meaning). The function removes all the synonyms which contain the letter 'e' or 'E') from each corresponding list of synonyms. As well, the function sorts each corresponding list of synonyms. Note: the testing code makes use of the print_dict_in_key_order(a_dict) which...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in...

    Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since...

  • A) Please implement a Python script to define a student class with the following attributes (instance...

    A) Please implement a Python script to define a student class with the following attributes (instance attributes): cwid: student’s CWID first_name : student’s first name last_name: student’s last name gender: student’s gender gpa: student’s gpa Please make these attributes as private ones so they have to be accessed via getter/setter methods or property. For this purpose, please define a getter/setter method and property for each of the attributes. Another thing you need to do is to define a constructor that...

  • Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...

  • VERY URGENT*** THANK YOU IN ADVANCE: THIS IS THE CODE I HAVE GOTTEN SO FAR: PYTHON...

    VERY URGENT*** THANK YOU IN ADVANCE: THIS IS THE CODE I HAVE GOTTEN SO FAR: PYTHON This is a code that is supposed to help someone study/ practice for jeopardy. The two functions described below are required for this assignment. You may add other functions that you think are appropriate: menu() This function, which displays all the user options to the screen, prompt the user for their choice and returns their choice. This function will verify user input and ALWAYS...

  • I NEED HELP WITH THIS HOMEWORK!!!! What is a characteristic of a bag data type? Elements...

    I NEED HELP WITH THIS HOMEWORK!!!! What is a characteristic of a bag data type? Elements are added and removed in any order Elements are removed in the same order they were added Distinct elements are added in any order but are removed beginning with the last one added Distinct elements are removed in the reverse order they were added Which scenario illustrates a data stack structure Standing in a line to be serviced Filing documents in alphabetical order Offering...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT