Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 177 Lab #3 Spring 2017 Due Date This assignme is due by 59 pm June 22nd. Prob

ID: 3851210 • Letter: C

Question

CS 177 Lab #3 Spring 2017 Due Date This assignme is due by 59 pm June 22nd. Problem Description The mathematical constant can be to at least decimals by using the mathematical Notioe that the sign alternates from minus ()to plus for each term. This approximation beoomes you increase the number of terms used. You need to evaluate this caloulaton more accurate based on a varying number of terms, (echow accurate is the approximaton with 50, 100.500, 1000 or more terms) With your team Write a Python program teamLab3.py that informs the user ofits function (approximati the value of m) then prompts for the number of terms to use in the calculation. Your team's code should then compute the approximate value, print the result, the correct value of (using the math library) and the difference between the two values Example input/output for Part These are just examples and are notthe sexactestimates of pi you shouldexpect a CS 177 assignments, you must folow the coding guideines. Prior to writing Python code. any for each section. Lab 3 Part 1- Task List Example Input Output for Part 2 These examples show several vanations on the number of spaces/fabs between the columns TODO 1: Setup program mport math library talize any ssary variables and constants To Do 2: Prompt for user input Prompt the user the number of to use in the calculation TO DO 83: Calculate an approximation ofR Create a for loop based the number of elements in the calculation Create an algorithm to approximate TO DO 84: Display the results Display the calculated value Display the actual value of R Display the difference between Mand your calculated value TODO B5 Submit to Blackboard teamLab3.py) to Upload your completed Blackboard as your first Lab 3 All students should submt a copy teamLab3.py file to Blackboard before starting Part 2. Part 2: On your own After your team has fully completed Part1 and submitted the team program to Blackboard, work on One way to keep float values from being displayed in e notation is to use the format: atr 10 float your your own to expand the functionality of yourprogram to approximate 1T across a series of elements, indicates a foat with 10 decimal places Starting with the teamLab3.py file, create a program named mylab3.py that prompts the user refers to the float value you want to display for the starting and ending number of tems to be used, as well as the (or interval) between the number of terms. Also prompt the user for the precision (number of decimal places) to display in the Adjust the number of spaces andor tabsbetween columns to suit your preference calculated values, Save your final program as mytab3.py Submit your nyt ab3.py fie to the Lab 3 Blackboard assignment. For example, ifthe user entered a starting number of 50 terms, an ending number of 1000 and a stop of 50, the program would display of first with 50 terms, then 100, 150, 200,250. Lab 3 Grading Rubri Points up to and including 1000 terms. The program wil also display statistics about the accuracy of each and precision values for start, end, approximation should be displayed along with the values. are setup to correctly approximate PI for multiple values are displayed for each number of elements approximation and diference Upload a copy of your finished program yLab3.py to your sooond Lab 3 formatted, easy to read and neatly aligned submission. This wiloount as your Lab 3 score. Style Format: Python code is formatted, commented and easy to understand tal Points

Explanation / Answer

Update:

The link to python file for part 2 is here

https://drive.google.com/open?id=0B21QkpYJQztnYzBfSG5WazlEVkU

import math

print "This program approximates PI with a range of elementes."

  

str1 = raw_input("Starting number of elements: ")

start = int(str1)

str1 = raw_input("Ending number of elements: ")

ending = int(str1)

str1 = raw_input("Interval: ")

interval = int(str1)

str1 = raw_input("Precision (decimals): ")

precision = int(str1)

print "PI = ", math.pi

  

approximation = float(0)

step = 0;

formatstr = "%20." + str(precision) + "f"

print " Num Approximate PI Difference"

for i in range(ending):

numerator = (-1/3.0)**i

denominator = 2.0 * i + 1

term = numerator / denominator

approximation = approximation + term

if i < start:

continue

else:

if step % interval == 0:

approx = 12**(1/2.0) * approximation

difference = math.pi - approx

print str("%10s" % i), " ", str(formatstr % approx), " ", str("%.25f" % difference)

step = step + 1


output

This program approximates PI with a range of elementes.
Starting number of elements: 50
Ending number of elements: 1650
Interval: 100
Precision (decimals): 10
PI = 3.14159265359
   Num        Approximate PI        Difference
50    3.1415926536        -0.0000000000000008881784197
150    3.1415926536        -0.0000000000000008881784197
250    3.1415926536        -0.0000000000000008881784197
350    3.1415926536        -0.0000000000000008881784197
450    3.1415926536        -0.0000000000000008881784197
550    3.1415926536        -0.0000000000000008881784197
650    3.1415926536        -0.0000000000000008881784197
750    3.1415926536        -0.0000000000000008881784197
850    3.1415926536        -0.0000000000000008881784197
950    3.1415926536        -0.0000000000000008881784197
1050    3.1415926536        -0.0000000000000008881784197
1150    3.1415926536        -0.0000000000000008881784197
1250    3.1415926536        -0.0000000000000008881784197
1350    3.1415926536        -0.0000000000000008881784197
1450    3.1415926536        -0.0000000000000008881784197
1550    3.1415926536        -0.0000000000000008881784197

===========

Here is the code to approximate PI. According to formula given, the calculations are done. The output screens shown in the question show different values. (There are other formulas to calculate pi as well)

Other formula of pi is  4*(1-1/3+1/5-1/7+1/9-...) When this formula is used , the output shown in the screen matches.

Can you please check and let me know which formula you want to use ?

Below is the python code. In case the indentations are lost, I am giving you a shared link to the uploaded file.

https://drive.google.com/open?id=0B21QkpYJQztnZUVvMlBTOF9vM0k

import math


print "This program approximates PI"
print "more elements increases accuracy"
  
str = raw_input("Enter the number of elements to use: ")
  
n = int(str)
print "Number of elements: ", n
  
approximation = float(0)

for i in range(n):
numerator = (-1/3.0)**i
denominator = 2.0 * i + 1
term = numerator / denominator
approximation = approximation + term


approximation = 12**(1/2.0) * approximation
difference = math.pi - approximation

print "Approx. PI: ", approximation
print "Correct PI: ", math.pi
print "Difference: ", difference