For the second part of this lab, we will write a function (gregoryLiebniz) that
ID: 675548 • Letter: F
Question
For the second part of this lab, we will write a function (gregoryLiebniz) that will estimate pi, using a different approach to that used in the labs. The Gregory-Liebniz series, developed by James Gregory and Gottfried Liebniz, provides an approximation to pi that gets better, the more terms you consider. While inefficient, it is quite simple. The Gregory-Liebniz series is given by the following formula: pi = summation_n=0^infinity 4 Times -1^n / 2n + 1 = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + Your function will take one argument (numlterations), which determines how many steps to take (i.e. how many terms to sum), and returns the approximation for pi. Try to find a value for numlterations that gives a reasonable approximation (e.g. 6 correct decimal places), yet takes only a few seconds to execute. The intersection (n) of two sets (s1, s2) is the set of all elements that are in s1 and are also in s2. Write a function (intersect) that takes two lists as input (you can assume they have no duplicate dements), and return the intersection of those two sets (as a list) without using the in operator or any built in functions, except for range () and len(). Write some code to test your function, as well.Explanation / Answer
python code for first part compiled on ideone:
# your code goes here
def gregoryLiebniz(n):
sum = 0.0
unit = 1.0
for i in range (1,n+1):
lower = 2*i-1
sum += unit/lower
unit = -unit
return 4*sum
a=gregoryLiebniz(100000)
print a
working python code for second part:
# your code goes here
def intersect(a, b):
return list(set(a) & set(b))
a = [1,3,5,7,9,11,13,15,17,19,21,23,25]
b = [1,4,9,16,25]
print intersect(a, b)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.