Question

In: Computer Science

write a function to determine the square root of a number. The square root of a...

write a function to determine the square root of a number. The square root of a number can be approximated by repeated calculation using the formula NG = 0.5(LG + N/LG) where NG stands for the next guess and LG stands for the last guess. The loop should repeat until the difference between NG and LG is less than 0.00001. Use an initial guess of 1.0. Write a driver program to test your square root function. I WANT THIS PROGRAM IN PYTHON AND WIT PSEUDOCODE!!! AS SIMPLE AS POSSIBLE!!!!

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code


PSEUDO-CODE FOR THE METHOD CALCULATING SQUARE ROOT

MODULE square_root(num)
   DECLARE LG=1.0
   DECLARE NG=(LG+(num/LG))/2
   WHILE ABS(LG-NG) >= 0.00001 DO
      LG=NG
      NG=(LG+(num/LG))/2
   END WHILE
   RETURN LG
END MODULE

#PYTHON CODE FOR THIS PROBLEM INCLUDING TESTING

#required method to estimate the square root of num
def square_root(num):
    #initializing 1 as initial guess
    LG=1.0
    #calculating second term from LG
    NG=(LG+(num/LG))/2
    #looping until the difference of prev and next is too small
    #(less than 0.00001)
    #note: use smaller values for more accurate results.
    while abs(LG-NG)>=0.00001:
        #storing current NG value as LG
        LG=NG
        #finding the next term
        NG = 0.5*(LG+ (num / LG))
    #returning the value of LG at the end.
    return LG


#main method for testing
def main():
    #reading a value
    n=float(input("Enter a number: "))
    #displaying square root
    print('Square root is',square_root(n))

#calling main()
main()

#OUTPUT

Enter a number: 5
Square root is 2.2360688956433634


Related Solutions

Graph the square root of the following function. State the domain and range of the square root of the function.
Graph the square root of the following function. State the domain and range of the square root of the function.y = -2x + 4
determine the range and domain of the following: the function k defined by k(x)= square root...
determine the range and domain of the following: the function k defined by k(x)= square root x-3
why are the domain and range of square root function restricted to [0,∞) ?
why are the domain and range of square root function restricted to [0,∞) ?
Find the domain of the function g(x) = square root of 3x+3
Find the domain of the function g(x) = square root of 3x+3
Deduce from the Completeness Axiom that there exists a square root of a real number a...
Deduce from the Completeness Axiom that there exists a square root of a real number a if and only if a ≥ 0
Estimate the area A between the graph of the function f(x)= square root of x and...
Estimate the area A between the graph of the function f(x)= square root of x and the interval [0,49]. Use an approximation scheme with n=2,5, and 10 rectangles. Use the right endpoints. Round your answers to three decimal places. A2= A5= A10= Click
A positive number is added to the reciprocal of its square root. Use Calculus methods to...
A positive number is added to the reciprocal of its square root. Use Calculus methods to answer the following questions:what if anything, is the smallest such sum? What, if any, positive x-values will yield the smallest sum? Give exact answers as well as answers accurate to Five decimal place. Be sure to give a mathematical justification that you answer represents a minimum and not a maximum.
An investor's utility function for money (Bernoulli utility function) is the square root of money: u(x)=√x....
An investor's utility function for money (Bernoulli utility function) is the square root of money: u(x)=√x. Her decision making can be modeled by assuming that she maximizes her expected utility. Her current wealth is 100. (All quantities are in hundreds of dollars.) She has the opportunity to buy a security that either pays 8 (the "good outcome") or loses 1 (the "bad outcome"). She can buy as many units as she wishes. For example, if she buys 5 units, she...
An investor's utility function for money (Bernoulli utility function) is the square root of money: u(x)=√x....
An investor's utility function for money (Bernoulli utility function) is the square root of money: u(x)=√x. Her decision making can be modeled by assuming that she maximizes her expected utility. Her current wealth is 100. (All quantities are in hundreds of dollars.) She has the opportunity to buy a security that either pays 8 (the "good outcome") or loses 1 (the "bad outcome"). She can buy as many units as she wishes. For example, if she buys 5 units, she...
Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:
Chapter 3 Exercise 1Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:      1. Make a guess at the answer (you can pick n/2 as your initial guess).      2. Computer = n / guess.      3. Set guess = (guess +r) / 2.      4. Go back to step 2 until the last two guess values are within 1% of each other.Write a program that inputs an integer for n, iterates through the Babylonian algorithm until the guess...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT