Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Python Version 1. Write a program that uses a while loop to print the first 10 positive integers and to compute their sum. Print

Using Python

Version 1. Write a program that uses a "while" loop to print the first 10 positive integers and to compute their sum. Print the sum after it is computed. Do the same with a "for" loop.

Version 2. Write a program to approximate the square root of a number. Recall that the square root of a number x is a number r such that r*r = x. Newton discovered that if one initial estimate of r is z then a better estimate is obtained by taking the average of z and x/z. The estimate can be improved by using this rule again and again until a satisfactory value is obtained (for example, when the difference between x and z*z becomes less than 0.000001 ).

Ask the user to give a value for x. Start with the estimate

z = 1.0 , then update z inside a while loop using the computation

z = ( z + x/z ) /2

until the difference (x - z**2) has an absolute value less than a given tolerance.

Let tolerance = 0.000001. Print to the screen the value found and compare it with the one returned by math.sqrt(x), that is, the square root function from the math module.

Note on Newton's method: If you have taken calculus, this is using Newton's method to find an approximation of the zeros for a function of the type f(z) = x - z*z, where x is fixed. Newton's method consists in following the equation of the tangent line at the current estimate and using the intersection with the x-axis as an improvement of the estimate. The derivative f'(z) = - 2z gives the slope of the tangent at the current z. Replacing f(z_new) by 0 we get the equation of the tangent line of the form

f(z_old) - 0 = f'(z_old) (z_old - z_new).

z_new = z_old - f(z_old)/f'(z_old)

or for the computer code the update is just

z = z - f(z)/ f'(z)

for the specific f(z) = x - z*z, we have z - f(z)/ f'(z) = (z -( x - z*z)/(-2z)) = z/2 + x/(2z)

which leads to the update statement in the form

z = ( z + x/z ) /2 ;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sybase Database Administrators Handbook

Authors: Brian Hitchcock

1st Edition

0133574776, 978-0133574777

More Books

Students also viewed these Databases questions