in python 3.2 1. Package Newton’s method for approximating square roots (Case St
ID: 3753856 • Letter: I
Question
in python 3.2
1. Package Newton’s method for approximating square roots (Case Study 3.6) in a
function named newton. This function expects the input number as an argument
and returns the estimate of its square root. The script should also include a main
function that allows the user to compute square roots of inputs until she presses
the enter/return key.
2. Convert Newton’s method for approximating square roots in Project 1 to a recursive
function named newton. (Hint: The estimate of the square root should be
passed as a second argument to the function.)
3. Restructure Newton’s method (Case Study 3.6) by decomposing it into three
cooperating functions. The newton function can use either the recursive strategy
of Project 1 or the iterative strategy of Case Study 3.6. The task of testing for the
limit is assigned to a function named limitReached, whereas the task of computing
a new approximation is assigned to a function named improveEstimate. Each
function expects the relevant arguments and returns an appropriate value.
Explanation / Answer
Answer2:--
import math
class A:
def __init__(self):
self.number=int(input("Enter Perfect squre number whose root you want to calculate"))
self.guess=int(input("Enter guess square root of a number"))
self.result=newton(self.number,self.guess)
print("square root of a number is")
print(self.result)
def newton(number,guess):
while guess**2<number: #if the guess is smaller than the actual result
guess=guess+1
while guess**2>number: # if the guess is larger than actual result
guess=guess-1
return guess
a=A()
Answer3:--
#squareroot through iteration and guess
import math
class A:
def __init__(self):
self.number=int(input("Enter Perfect squre number whose root you want to calculate"))
self.result=keywordArg(self.number,guess=10) #guess=10 is my default parameter now user dont have to enter any guess
print("square root of a number is")
print(self.result)
def keywordArg(number,guess=10):
while guess**2<number: #if the guess is smaller than the actual result
guess=guess+1
while guess**2>number: # if the guess is larger than actual result
guess=guess-1
return guess
a=A()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.