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 29f6abd

Browse files
Some readme edits, #9 finished
1 parent 466499f commit 29f6abd

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

‎9-NewtonSquareRoots.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77
# x is the number you want to find the sqrt of
88
# z is a guess at what the sqrt is?
99

10+
# Decimal functionality adapted from https://docs.python.org/2/library/decimal.html and https://stackoverflow.com/questions/35619309/how-to-increase-the-precision-of-float-values-to-more-than-10-in-python
11+
1012
import math
13+
from decimal import *
14+
getcontext().prec = 15
1115

12-
x = float(input("Enter a number: "))
13-
z = float(input("Sqrt guess: "))
16+
x = Decimal(input("Enter a number: "))
17+
z = Decimal(input("Sqrt guess: "))
1418

15-
print("Math.sqrt(x): \t" + str(math.sqrt(x))) # Accurate sqrt
19+
print("Math.sqrt(x): \t\t" + str(Decimal(math.sqrt(x)))) # Accurate sqrt
1620

1721
approximate = z - ((z*z - x) / (2 * z))
22+
difference = 1
1823

19-
foriinrange (0,3):
24+
whiledifference>0.00000000001:
2025
z = approximate
2126
approximate = z - ((z*z - x) / (2 * z))
27+
difference = z - approximate
2228

23-
print("Approximate: \t" + str(approximate))
29+
print("Newton's Method: \t" + str(approximate))

‎README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ This program outputs all numbers 1 - 100 inclusive. If a number is divisible by
2323

2424
**4. Factorial**
2525
The user enteres a number. The factorial of that number is calculated by multiplying it by every number preceding it and the result is printed to the screen.
26-
*Not finished yet, working on the sum of digits in the factorial answer.*
2726

2827
**5. Guessing Game**
2928
The program generates a random number for the user to guess. The program tells the user if their guess was too high or too low, and tells them how many tries it took them when they get the number right. The program does not count multiple guesses of the same number.
@@ -44,7 +43,7 @@ The user enters a number and their guess for the square root of that number. The
4443
newApproximate = z - ((z* - x) / (2 * z))
4544
```
4645

47-
*Not finished yet, need to make float more accurate (import decimal?) and loop stop when value starts changing by tiny amount.*
46+
The program stops calculating a new approximate when the change in results between loops becomes less than 0.00000000001.
4847

4948
**10. Reverse a String**
5049
The user enters a word or sentence. The program reverses the string using both the easy, built in `str(word)[::-1]` function, and a written function that loops through the string to reverse it manually. Both results are the same.

0 commit comments

Comments
(0)

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