Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Carson Witt

2061 views
n = input("How many decimals of e would you like to approximate?") sum = 0 desired_e = N(e, digits = n + 1) term_number = 0 while sum != desired_e: sum += 1/factorial(term_number) term_number += 1 print "NOTE: The code will approximate to the " + str(n) + " digits you requested, but it will show " + str(n+1) + " digits to prevent rounding errors." print "-----------------------------------------------------------------------" print "Estimated value of e: " + str(N(sum, digits = n + 1)) print "-----------------------------------------------------------------------" print "Actual value of e: " + str(desired_e) print "-----------------------------------------------------------------------" print "Difference: " + str((N(desired_e - sum, digits = 2))) print "-----------------------------------------------------------------------"
How many decimals of e would you like to approximate?
NOTE: The code will approximate to the 20 digits you requested, but it will show 21 digits to prevent rounding errors. ----------------------------------------------------------------------- Estimated value of e: 2.71828182845904523536 ----------------------------------------------------------------------- Actual value of e: 2.71828182845904523536 ----------------------------------------------------------------------- Difference: 0.00 -----------------------------------------------------------------------
n_2 = input("How many decimals of sqrt(2) would you like to approximate?") sum_2 = 0 desired_2 = N(sqrt(2), digits = n_2 + 1) term_number_2 = 0 while sum_2 != desired_2: sum_2 += (factorial(2*(term_number_2)+1))/((2^(3*(term_number_2)+1))*(factorial(term_number_2))^2) term_number_2 += 1 print "NOTE: The code will approximate to the " + str(n_2) + " digits you requested, but it will show " + str(n_2+1) + " digits to prevent rounding errors." print "-----------------------------------------------------------------------" print "Estimated value of 2: " + str(N(sum_2, digits = n_2 + 1)) print "-----------------------------------------------------------------------" print "Actual value of 2: " + str(desired_2) print "-----------------------------------------------------------------------" print "Difference: " + str((N(desired_2 - sum_2, digits = 2))) print "-----------------------------------------------------------------------"
How many decimals of sqrt(2) would you like to approximate?
NOTE: The code will approximate to the 20 digits you requested, but it will show 21 digits to prevent rounding errors. ----------------------------------------------------------------------- Estimated value of 2: 1.41421356237309504880 ----------------------------------------------------------------------- Actual value of 2: 1.41421356237309504880 ----------------------------------------------------------------------- Difference: 0.00 -----------------------------------------------------------------------
n_pi = input("How many decimals of pi would you like to approximate?") sum_pi = 0 desired_pi = N(pi, digits = n_pi + 1) term_number_pi = 0 while sum_pi != desired_pi: sum_pi += (1/(16^term_number_pi))*((4/(8*(term_number_pi)+1))-(2/(8*(term_number_pi)+4))-(1/(8*(term_number_pi)+5))-(1/(8*(term_number_pi)+6))) term_number_pi += 1 print "NOTE: The code will approximate to the " + str(n_pi) + " digits you requested, but it will show " + str(n_pi+1) + " digits to prevent rounding errors." print "-----------------------------------------------------------------------" print "Estimated value of pi: " + str(N(sum_pi, digits = n_pi + 1)) print "-----------------------------------------------------------------------" print "Actual value of pi: " + str(desired_pi) print "-----------------------------------------------------------------------" print "Difference: " + str((N(desired_pi - sum_pi, digits = 2))) print "-----------------------------------------------------------------------"
How many decimals of pi would you like to approximate?
NOTE: The code will approximate to the 20 digits you requested, but it will show 21 digits to prevent rounding errors. ----------------------------------------------------------------------- Estimated value of pi: 3.14159265358979323846 ----------------------------------------------------------------------- Actual value of pi: 3.14159265358979323846 ----------------------------------------------------------------------- Difference: 0.00 -----------------------------------------------------------------------