Written Assignment UNIT 5 CS 1101 PDF

Title Written Assignment UNIT 5 CS 1101
Course Written Assignment Unit 5
Institution University of the People
Pages 4
File Size 99.1 KB
File Type PDF
Total Downloads 53
Total Views 161

Summary

Written Assignment UNIT 5 CS 1101...


Description

This assignment is based on Exercise 7.1 from your textbook. Part 1 Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that takes a as a parameter, chooses a starting value for x, and returns an estimate of the square root of a. while True: y = (x + a/x) / 2.0 if y == x: break x=y Part 2 Write a function named test_sqrt that prints a table like the following using a while loop, where "diff" is the absolute value of the difference between my_sqrt(a) and math.sqrt(a). a = 1 | my_sqrt(a) = 1 | math.sqrt(a) = 1.0 | diff = 0.0 a = 2 | my_sqrt(a) = 1.41421356237 | math.sqrt(a) = 1.41421356237 | diff 2.22044604925e-16 a = 3 | my_sqrt(a) = 1.73205080757 | math.sqrt(a) = 1.73205080757 | diff 0.0 a = 4 | my_sqrt(a) = 2.0 | math.sqrt(a) = 2.0 | diff = 0.0 a = 5 | my_sqrt(a) = 2.2360679775 | math.sqrt(a) = 2.2360679775 | diff = 0.0 a = 6 | my_sqrt(a) = 2.44948974278 | math.sqrt(a) = 2.44948974278 | diff 0.0 a = 7 | my_sqrt(a) = 2.64575131106 | math.sqrt(a) = 2.64575131106 | diff 0.0 a = 8 | my_sqrt(a) = 2.82842712475 | math.sqrt(a) = 2.82842712475 | diff 4.4408920985e-16 a = 9 | my_sqrt(a) = 3.0 | math.sqrt(a) = 3.0 | diff = 0.0

= =

= = =

Modify your program so that it outputs lines for a values from 1 to 25 instead of just 1 to 9. You should submit a script file and a plain text output file (.txt) that contains the test output. Multiple file uploads are permitted.

from decimal import * import math # below function is used to find the square roots of a number using the newton's method(i.e; using formula) def mysquare_root(a): x = 1 while True: y = (x + a/x) / 2 if x == y: break x = y return x # below function is used to find the square roots of a number using the built_in function def square_root(a): x = math.sqrt(a) return x # below function gives the difference between thr toe functions above def difference(a): d = square_root(a) - mysquare_root(a) return d # Normally we can print the values as usual by giving selected no. of spaces between one and other values # but, because of different float values, it is not possible to print uniformly as a table. # In below function we use the decimal to over come the possibility. def dec_1(a): # this function is to print the values of function 'mysquare_root()' uniformly as a table. # in order to access the decimals of a value we use 'Decimal' in_built function and we can round it off. d = Decimal(mysquare_root(a)) #print(d) # here we are rounding off the decimal values to '15' after point. d1 = round(d, 15) #print(d1) # if we round off all the values, even perfect square will have '15' zeros after point. we don't want it. # so, we are rounding off the perfect square values to '0' decimal values after point if d1 - Decimal(mysquare_root(a)) == 0: d1 = round(d, 0)

#print(d1) # in order to get one decimal value after point, simply we return a float value return float(d1) #print(d1) # we are return value which is rounded to certain values after point. return (d1) #print(dec_1(2)) # the below function works same as above function, but for in_built method to find square root. def dec_2(a): # This function is working for to print the values of function 'square_root()' d = Decimal(square_root(a)) #print(d) d1 = round(d, 15) if d1 - Decimal(mysquare_root(a)) == 0: d1 = round(d, 0) return float(d1) #print(d1) return Decimal(d1) #print(dec_2(2)) def print_it(): # below two print statements arranged as per requirement. print("numbers", " "*14, 'mysquare_root', ' '*8, 'square_root', ' '*10, "difference") print("-------", " " * 14, '-------------', ' ' * 8, '-----------', ' ' * 10, "----------") for a in range(1, 10): # here we are going to print square roots 1-9 # here we are giving the 18 spaces between the one float value to another in table. # if there is only 15 decimal values after point, we already covered 14 position after float value, so we will give 4 spaces # here to do spacing between values, we just comparing float value and int value to get how much space is needed. print(float(a), " " * 18, f'{dec_1(a)}', " "*18 if dec_1(a) == int(dec_1(a)) else " "* 4, f'{dec_2(a)}', " "*18 if dec_1(a) == int(dec_2(a)) else " "* 4, f"{difference(a)}") print_it()

OUTPUT numbers

mysquare_root

-------

-------------

-----------

1.0

1.0

1.0

square_root ---------0.0

difference

2.0

1.414213562373095

1.414213562373095

2.220446049250313e-16

3.0

1.732050807568877

1.732050807568877

0.0

4.0

2.0

5.0

2.236067977499790

2.236067977499790

0.0

6.0

2.449489742783178

2.449489742783178

0.0

7.0

2.645751311064591

2.645751311064591

0.0

8.0

2.828427124746190

2.828427124746190

4.440892098500626e-16

9.0

3.0

2.0

3.0

0.0

0.0...


Similar Free PDFs