You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 9-NewtonSquareRoots.py
+11-5Lines changed: 11 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -7,17 +7,23 @@
7
7
# x is the number you want to find the sqrt of
8
8
# z is a guess at what the sqrt is?
9
9
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
+
10
12
importmath
13
+
fromdecimalimport*
14
+
getcontext().prec=15
11
15
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: "))
14
18
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
Copy file name to clipboardExpand all lines: README.md
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,6 @@ This program outputs all numbers 1 - 100 inclusive. If a number is divisible by
23
23
24
24
**4. Factorial**
25
25
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.*
27
26
28
27
**5. Guessing Game**
29
28
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
44
43
newApproximate = z - ((z* - x) / (2 * z))
45
44
```
46
45
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.
48
47
49
48
**10. Reverse a String**
50
49
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