Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e3fc6f4

Browse files
Outputting math.sqrt, does one iteration of N's method, might be on the right track but not sure yet...
1 parent d00855b commit e3fc6f4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎9-NewtonSquareRoots.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 9 - Newton's Method for Square Roots
2+
# Source: https://tour.golang.org/flowcontrol/8
3+
# Implement the square root function using Newton's method. In this case, Newton's method is to approximate sqrt(x) by picking a starting point z and then repeating z_next = z - ((z*z - x) / (2 * z))
4+
# To begin with, just repeat that calculation 10 times and see how close you get to the answer for various values (1, 2, 3, ...). Next, change the loop condition to stop once the value has stopped changing (or only changes by a very small delta). How close are you to the math.sqrt value?
5+
6+
# Logic
7+
# x is the number you want to find the sqrt of
8+
# z is a guess at what the sqrt is?
9+
10+
import math
11+
12+
x = float(input("Enter a number: "))
13+
z = float(input("Sqrt guess: "))
14+
15+
print("Math.sqrt(x): " + str(math.sqrt(x))) # Accurate sqrt
16+
17+
approximate = z - ((z*z - x) / (2 * z))
18+
print("Approximate: " + str(approximate))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /