Question

Using Python Version 1. Write a program that uses a "while" loop to print the first...

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 ;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#VERSION 1
i = 1
sum=0
#while loop
while i <= 10:
print(i) #printing new number on each line
sum += i
i+=1

print("Sum of first 10 positive integers is= "+str(sum))

#for loop

i=1
sum=0
for i in range(1,11):
    print(i)
    sum+=i
    i+=1
print("Sum of first 10 positive integers is= "+str(sum))

#VERSION 2

import math #importing the math lib to use math.sqrt()

x=float(input("Enter x ")) #taking input
z=1.0 #starting z with 1.0 acc. to question
tolerance = 0.000001 #fixing tolerance

while abs(x-z**2)>=tolerance: #looping till the difference doesn't become less than tolerance
    z = ( z + x/z ) /2
  
print("The value computed by our method is           "+str(z))
print("The value computed by math.sqrt() function is "+str(math.sqrt(x)))

Add a comment
Know the answer?
Add Answer to:
Using Python Version 1. Write a program that uses a "while" loop to print the first...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT