0% found this document useful (0 votes)
118 views3 pages

Assignment 3: Square Root

The document discusses Newton's method for calculating square roots. It explains that the method starts with an initial guess and iteratively improves the guess using the formula xn+1 = (xn + a/xn)/2 until the result converges within a certain tolerance. The document provides an example Python code to calculate the square root of a given number using this method. It also includes two tasks - to calculate machine epsilon using a while loop, and to print the multiplication table of a given number up to 12 using a while loop.

Uploaded by

Akash Chhabria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views3 pages

Assignment 3: Square Root

The document discusses Newton's method for calculating square roots. It explains that the method starts with an initial guess and iteratively improves the guess using the formula xn+1 = (xn + a/xn)/2 until the result converges within a certain tolerance. The document provides an example Python code to calculate the square root of a given number using this method. It also includes two tasks - to calculate machine epsilon using a while loop, and to print the multiplication table of a given number up to 12 using a while loop.

Uploaded by

Akash Chhabria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

9/11/2018 Assignment3_13174

Assignment 3

Square Root
A classic algorithm that illustrates many of these concerns is Newton’s method to compute square roots
x = √ (a) for a>0, i.e. to solve x = a. The algorithm starts with some guess x 1 > 0 and computes the
2

sequence of improved guesses


1 a
x n+1 = (x n + )
2 xn

it turns out that this algorithm is very old, dating at least to the ancient Babylonians circa 1000 BCE(See e.g.
Boyer,A History of Mathematics, ch. 3; the Babylonians used base 60 and a famous tablet (YBC
7289)shows√2 to about six decimal digits.)

In modern times, this was seen to be equivalent to Newton’s method to find a root of f (x) = x 2 − a Recall
that Newton’s method finds an approximate root of f(x)=0 from a guess x n by approximating f(x) as its
tangent line f (x n ) + f ′ (x n )(x − x n ) eading to an improved guess x n+1 from the root of the tangent:
f (x)
x n+1 = x n +

f (x)

and for f (x) = x


2
− a this yields the Babylonian formula above.

Example
Find the Square root of any input positive integer and evaluate the square root using above Formula

In [6]:

number=input("Enter a number ?")


x=float(number);
guess=1
i=0
while abs(guess*guess-x)>1e-15:
guess=.5*(guess+x/guess)
i=i+1

print("Required Square root of ",x," is ","{:30.8}".format(guess), "\n calculated in",


i ,"iterations" )

Enter a number ?22


Required Square root of 22.0 is 4.6904158
calculated in 7 iterations

Task 1
Use only While loop to solve the following Problems

http://localhost:8888/nbconvert/html/Downloads/Assignment3_13174.ipynb?download=false 1/3
9/11/2018 Assignment3_13174

Q1
When talking about floating point, we discussed machine epsilon, ϵ —this is the smallest number that when
added to 1 is still different from 1.

We'll compute ϵ here:

Pick an initial guess for ϵ of eps = 1.


Create a loop that checks whether 1 + eps is different from 1
Each loop iteration, cut the value of eps in half

What value of ϵ do you find?

In [2]:

eps=1
while(1+eps!=1):
eps=eps/2
print(eps)

1.1102230246251565e-16

Q2
Create a loop to calculate and print the times table (Multiplication table) of given input number upto first 12
places Required output is given as follow

2   ×   1 = 2

2   ×   2 = 4

2   ×   3 = 6

2   ×   5 = 8

2   ×   11 = 22

2   ×   12 = 24

http://localhost:8888/nbconvert/html/Downloads/Assignment3_13174.ipynb?download=false 2/3
9/11/2018 Assignment3_13174

In [6]:

x=int(input("Enter a number = "))


i=0
while (i<12):
i+=1
print(x,"x",i,"=",x*i)

Enter a number = 12
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
12 x 11 = 132
12 x 12 = 144

http://localhost:8888/nbconvert/html/Downloads/Assignment3_13174.ipynb?download=false 3/3

You might also like