From 02ec0b6e319134ae17fc43213331038c57ce3f95 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Tue, 26 May 2015 16:23:09 -0400 Subject: [PATCH 1/5] added square_root.py --- square_root.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 square_root.py diff --git a/square_root.py b/square_root.py new file mode 100644 index 0000000..a3db30f --- /dev/null +++ b/square_root.py @@ -0,0 +1,56 @@ +import math +import cmath + + +#functions + +#returns the next estimate using Newton's Method +def new_estimate(old_estimate,number_): + return old_estimate - (old_estimate**2 - number_)/(2*old_estimate) + +#returns the number truncated to the requested number of decimals +def truncate(number_,decimals=0): + return math.floor(abs(number_)*(10**decimals))/(10**decimals) + +#returns true if estimates are equal to the specified decimal place +def compare_estimates(estimate1,estimate2,decimals): + return truncate(estimate1,decimals) == truncate(estimate2,decimals) + +#prints the provided loop number and estimate +def print_details(estimate,loop): + print("{}: {}".format(loop,estimate)) + +#Prompt for number +input_number = input("Enter a number > ") + +#Convert input_number to a float +try: + number = float(input_number) +except: + print ("You did not enter a number.") + exit() + +#Original Estimate +current_estimate = number / 2 +if number < 0: + current_estimate += 1j * number / 2 + +number_of_loops = 1 +print_details(current_estimate,0) + +#Algorithm +while True: + #Calculate new estimate + next_estimate = new_estimate(current_estimate,number) + #Print current information + print_details(next_estimate,number_of_loops) + #Compare new estimate to old estimate + if compare_estimates(next_estimate,current_estimate,2): + #If hundredth's place hasn't changed, break + break + #Else repace old estimate with new estimate and repeat + current_estimate = next_estimate + number_of_loops += 1 +#Print Results +print("The square root of {} is {}.".format(number,current_estimate)) +#print("It took {} loops through the algorithm to get the answer.".format(number_of_loops)) From 6d2e16b7232049b6f78aec1fe3a6f721283016fc Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 07:50:47 -0400 Subject: [PATCH 2/5] Reformatted comments --- square_root.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/square_root.py b/square_root.py index a3db30f..d725d08 100644 --- a/square_root.py +++ b/square_root.py @@ -2,35 +2,35 @@ import cmath -#functions +# functions -#returns the next estimate using Newton's Method +# returns the next estimate using Newton's Method def new_estimate(old_estimate,number_): return old_estimate - (old_estimate**2 - number_)/(2*old_estimate) -#returns the number truncated to the requested number of decimals +# returns the number truncated to the requested number of decimals def truncate(number_,decimals=0): return math.floor(abs(number_)*(10**decimals))/(10**decimals) -#returns true if estimates are equal to the specified decimal place +# returns true if estimates are equal to the specified decimal place def compare_estimates(estimate1,estimate2,decimals): return truncate(estimate1,decimals) == truncate(estimate2,decimals) -#prints the provided loop number and estimate +# prints the provided loop number and estimate def print_details(estimate,loop): print("{}: {}".format(loop,estimate)) -#Prompt for number +# Prompt for number input_number = input("Enter a number > ") -#Convert input_number to a float +# Convert input_number to a float try: number = float(input_number) except: print ("You did not enter a number.") exit() -#Original Estimate +# Original Estimate current_estimate = number / 2 if number < 0: current_estimate += 1j * number / 2 @@ -38,19 +38,18 @@ def print_details(estimate,loop): number_of_loops = 1 print_details(current_estimate,0) -#Algorithm +# Algorithm while True: - #Calculate new estimate + # Calculate new estimate next_estimate = new_estimate(current_estimate,number) - #Print current information + # Print current information print_details(next_estimate,number_of_loops) - #Compare new estimate to old estimate + # Compare new estimate to old estimate if compare_estimates(next_estimate,current_estimate,2): - #If hundredth's place hasn't changed, break + # If hundredth's place hasn't changed, break break - #Else repace old estimate with new estimate and repeat + # Else repace old estimate with new estimate and repeat current_estimate = next_estimate number_of_loops += 1 -#Print Results +# Print Results print("The square root of {} is {}.".format(number,current_estimate)) -#print("It took {} loops through the algorithm to get the answer.".format(number_of_loops)) From 5f9d4032dd929b9ed8caccb0cc0b251b55a51ddd Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 08:50:30 -0400 Subject: [PATCH 3/5] Changed except to look for ValueError --- square_root.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/square_root.py b/square_root.py index d725d08..5469f46 100644 --- a/square_root.py +++ b/square_root.py @@ -26,7 +26,7 @@ def print_details(estimate,loop): # Convert input_number to a float try: number = float(input_number) -except: +except ValueError: print ("You did not enter a number.") exit() From e38a05eb1d95cdbbf03a951cddfaa3f7ef45610c Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 08:52:36 -0400 Subject: [PATCH 4/5] Fixed formatting --- square_root.py | 1 + 1 file changed, 1 insertion(+) diff --git a/square_root.py b/square_root.py index 5469f46..4fc5242 100644 --- a/square_root.py +++ b/square_root.py @@ -51,5 +51,6 @@ def print_details(estimate,loop): # Else repace old estimate with new estimate and repeat current_estimate = next_estimate number_of_loops += 1 + # Print Results print("The square root of {} is {}.".format(number,current_estimate)) From 5d9a941fd62ae07c747738ecc5eff229f3748e96 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 08:55:47 -0400 Subject: [PATCH 5/5] fixed operator formatting --- square_root.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/square_root.py b/square_root.py index 4fc5242..ab983d0 100644 --- a/square_root.py +++ b/square_root.py @@ -6,11 +6,11 @@ # returns the next estimate using Newton's Method def new_estimate(old_estimate,number_): - return old_estimate - (old_estimate**2 - number_)/(2*old_estimate) + return old_estimate - (old_estimate**2 - number_)/(2 * old_estimate) # returns the number truncated to the requested number of decimals def truncate(number_,decimals=0): - return math.floor(abs(number_)*(10**decimals))/(10**decimals) + return math.floor(abs(number_)*(10**decimals)) / (10**decimals) # returns true if estimates are equal to the specified decimal place def compare_estimates(estimate1,estimate2,decimals):