Question: #note: should NOT import any packages or variables for this problem def double_fact(n): #this uses a while loop ''' return the double factorial of an
#note: should NOT import any packages or variables for this problem
def double_fact(n): #this uses a while loop
''' return the double factorial of an integer n usually denoted as n!!, (where n>=-1),
which is defined as
(-1)!! = 1 and 0!! = 1,
for n>0: n!! = n*(n-2)*(n-4)* ...*k, where k=1 if n is odd, and k=2 if n is even.
Requirements;
1. you are not allowed to call any external functions;
2. you need to use a 'while' loop, do not write a recursive function
'''
def double_fact2(n):
""" return and requirement are the same as above, except that you need to use a 'for' loop here"""
def Ramanujan(tol=1e-14):
'''compute and return the converged sum of
1/1!! + 1/3!! + 1/5!! + 1/7!! + ... + 1/(2n-1)!! + ... ,
you can consider the sum as convergent when the last term you add to the sum becomes
Requirements:
1. you should call the double_fact() or double_fact2() function you write above,
2. use 'for' loop with 10**9 as the loop upper bound, break out of loop when the sum converges.
'''
def Ramanujan2(tol=1e-14):
'''compute and return the converged sum of
1/1!! + 1/3!! + 1/5!! + 1/7!! + ... + 1/(2n-1)!! + ... ,
you can consider the sum as convergent when the last term you add to the sum becomes
Requirements:
1. you should NOT call the double_fact() or any other factorial function, but
instead uyou should se update: i.e., you should compute (n+2)!! as n!!*(n+2),
reusing the n!! that has already been computed at a previous iteration.
2. for this problem you can use either 'for' or 'while' loop
'''
## do not modify the test code below this line
if __name__=='__main__':
#test on n = -1, 0, 1, 2, 3, ..., 11
DF0 = [ double_fact(n) for n in range(-1,12) ]
DF0_ext =[1, 1, 1, 2, 3, 8, 15, 48, 105, 384, 945, 3840, 10395]
if DF0 != DF0_ext:
print(' ***** double_fact() failed the test ***** ')
print(' you got DF0 =', DF0)
print(' should be ', DF0_ext)
else:
print(' Great, double_fact() passed the test ')
DF02 = [ double_fact2(n) for n in range(-1,12) ]
if DF02 != DF0_ext:
print(' ***** double_fact2() failed the test ***** ')
print(' you got DF02 =', DF02)
print(' should be ', DF0_ext)
else:
print(' Great, double_fact2() passed the test ')
#test on other n
DF1 = [ double_fact(n) for n in range(10, 18) ]
DF1_ext = [3840, 10395, 46080, 135135, 645120, 2027025, 10321920, 34459425]
if DF1 != DF1_ext:
print(' ***** double_fact() failed the test ***** ')
print(' you got DF1 =', DF1)
print(' should be ', DF1_ext)
else:
print(' Great, double_fact() passed another test ')
DF2 = [ double_fact2(n) for n in range(20, 26) ]
DF2_ext = [3715891200, 13749310575, 81749606400, 316234143225, 1961990553600, 7905853580625]
if DF2 != DF2_ext:
print(' ***** double_fact2() failed the test ***** ')
print(' you got DF2 =', DF2)
print(' should be ', DF2_ext)
else:
print(' Great, double_fact2() passed another test ')
## test on the sum
sumR=[1.4106861040194372, 1.4106861345664234, 1.4106861346391544, 1.4106861346423165, 1.4106861346424477]
for i, tol in enumerate([1e-6, 1e-8, 1e-10, 1e-11, 1e-14]):
sum = Ramanujan(tol=tol); sum2 = Ramanujan2(tol=tol)
if abs(sumR[i] - sum) > 2*tol:
print('***** Ramanujan ({}) test failed, your sum={}, err={} *****'.
format(tol, sum, abs(sumR[i] - sum)))
else:
print(' Great. Ramanujan ({}) test passed!'.format(tol))
if abs(sumR[i] - sum2) > 2*tol:
print('***** Ramanujan2({}) test failed, your sum={}, err={} *****'.
format(tol, sum, abs(sumR[i] - sum)))
else:
print(' Great. Ramanujan2({}) test passed!'.format(tol))
0.6 0.4 0.2 0.0 0.4 0.2 -2 -1 0 l2-2 -1 l 2 Figure 1: Plot of logistic functions P2. (40 points) Finish the python script double fact.py, which contains several subproblems: (a) Write two functions to compute the double factorials (n!!) of an input integer n, where double factorial is the same as factorial, except that its step-length is 2. For odd n, n!! = n * (n-2) * . . . * 1; F -2) * * 2; Also, 0!! = 1, and or even n, n!! n(n (-1)! 1, you can disregard other negative n's. (b) Compute the following sum in two different ways (2i- 1 Follow the instructions in double-fact.py for requirements of the d
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
