Programming Assignment: Square Root Calculation in Python, Assignments of Computer Science

This document is very effective and helped me understand more about the topic

Typology: Assignments

2020/2021

Uploaded on 04/19/2021

jisca-noubissi
jisca-noubissi 🇿🇦

5

(2)

3 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming Assign. Unit 5
1. Does the submission include a my_sqrt function that takes a single argument and includes
the while loop from the instructions? yes
2. Does the my_sqrt function initialize x and return its final value? yes
3. Does the test_sqrt function print a values from 1 to 25? yes
4. Does the test_sqrt function print the values returned by my_sqrt for each value of a? yes
5. Does the test_sqrt function print correct values from math.sqrt for each value of a? yes
6. Does the test_sqrt function print the absolute value of the differences
between my_sqrt and math.sqrt for each value of a? yes
7. Does the my_sqrt function compute values that are almost identical to math.sqrt ("diff" less than
1e-14)? yes
Part 1:
import math
def my_sqrt(a): #method for the square root of a
x = 1
while True:
y = (x + a / x) / 2.0 #finding the value of y
if y == x:
return y
x = y #updating x to assign the current value of y
pf3

Partial preview of the text

Download Programming Assignment: Square Root Calculation in Python and more Assignments Computer Science in PDF only on Docsity!

Programming Assign. Unit 5

  1. Does the submission include a my_sqrt function that takes a single argument and includes the while loop from the instructions? yes
  2. Does the my_sqrt function initialize x and return its final value? yes
  3. Does the test_sqrt function print a values from 1 to 25? yes
  4. Does the test_sqrt function print the values returned by my_sqrt for each value of a? yes
  5. Does the test_sqrt function print correct values from math.sqrt for each value of a? yes
  6. Does the test_sqrt function print the absolute value of the differences between my_sqrt and math.sqrt for each value of a? yes
  7. Does the my_sqrt function compute values that are almost identical to math.sqrt ("diff" less than 1e-14)? yes

Part 1:

import math

def my_sqrt(a): #method for the square root of a

x = 1

while True:

y = (x + a / x) / 2.0 #finding the value of y

if y == x:

return y

x = y #updating x to assign the current value of y

Part 2: def test_sqrt(): #method test_sqrt() test the above function a = 1 while a < 26: #looping a from 1 to 25 diff = abs(math.sqrt(a)-my_sqrt(a)) # differences between my_sqrt and math.sqrt print ("a=",a,"|","my_sqrt(a)=",my_sqrt(a),"|","math.sqrt(a)=", math.sqrt(a),"|", "diff=",diff)#displaying everything in the format asked a = a+ test_sqrt() Answers / Output: a= 1 | my_sqrt(a)= 1.0 | math.sqrt(a)= 1.0 | diff= 0. a= 2 | my_sqrt(a)= 1.414213562373095 | math.sqrt(a)= 1.4142135623730951 | diff= 2.220446049250313e- a= 3 | my_sqrt(a)= 1.7320508075688772 | math.sqrt(a)= 1.7320508075688772 | diff= 0. a= 4 | my_sqrt(a)= 2.0 | math.sqrt(a)= 2.0 | diff= 0. a= 5 | my_sqrt(a)= 2.23606797749979 | math.sqrt(a)= 2.23606797749979 | diff= 0. a= 6 | my_sqrt(a)= 2.449489742783178 | math.sqrt(a)= 2.449489742783178 | diff= 0. a= 7 | my_sqrt(a)= 2.6457513110645907 | math.sqrt(a)= 2.6457513110645907 | diff= 0. a= 8 | my_sqrt(a)= 2.82842712474619 | math.sqrt(a)= 2.8284271247461903 | diff= 4.440892098500626e- a= 9 | my_sqrt(a)= 3.0 | math.sqrt(a)= 3.0 | diff= 0. a= 10 | my_sqrt(a)= 3.162277660168379 | math.sqrt(a)= 3.1622776601683795 | diff= 4.440892098500626e- a= 11 | my_sqrt(a)= 3.3166247903554 | math.sqrt(a)= 3.3166247903554 | diff= 0. a= 12 | my_sqrt(a)= 3.4641016151377544 | math.sqrt(a)= 3.4641016151377544 | diff= 0. a= 13 | my_sqrt(a)= 3.6055512754639896 | math.sqrt(a)= 3.605551275463989 | diff= 4.440892098500626e- a= 14 | my_sqrt(a)= 3.7416573867739413 | math.sqrt(a)= 3.7416573867739413 | diff= 0. a= 15 | my_sqrt(a)= 3.872983346207417 | math.sqrt(a)= 3.872983346207417 | diff= 0. a= 16 | my_sqrt(a)= 4.0 | math.sqrt(a)= 4.0 | diff= 0.