#! /usr/bin/env python3 # def problem0 ( ): #*****************************************************************************80 # ## problem0(): Estimate the square root of pi. # from math import pi from math import sqrt print ( '' ) print ( 'problem0():' ) print ( ' Estimate the square root of pi.' ) x = 10.0 x = 0.5 * ( x + pi / x ); print ( x ) x = 0.5 * ( x + pi / x ); print ( x ) x = 0.5 * ( x + pi / x ); print ( x ) x = 0.5 * ( x + pi / x ); print ( x ) x = 0.5 * ( x + pi / x ); print ( x ) x = 0.5 * ( x + pi / x ); print ( x ) x = 0.5 * ( x + pi / x ); print ( x ) print ( '' ) print ( ' Exact value = ', sqrt ( pi ) ) return def problem1 ( ): #*****************************************************************************80 # ## problem1(): Energy equivalent. # print ( '' ) print ( 'problem1():' ) print ( ' E=mc^2.' ) m = 3.5 c = 299792458 E = m * c**2 print ( E, ' = ', m, '*', c,'**2' ) return def problem2 ( ): #*****************************************************************************80 # ## problem2(): BMI. # print ( '' ) print ( 'problem2():' ) print ( ' Body Mass Index.' ) w_lb = 180 h_in = 5 * 12 + 8 w_kg = w_lb * 0.45 h_m = h_in * 0.025 BMI = w_kg / h_m**2 print ( ' Sandy\'s BMI is ', BMI ) w_lb = 120 h_in = 5 * 12 + 3 w_kg = w_lb * 0.45 h_m = h_in * 0.025 BMI = w_kg / h_m**2 print ( ' Dale\'s BMI is ', BMI ) return def problem3 ( ): #*****************************************************************************80 # ## problem3(): nchoosek. # from math import factorial print ( '' ) print ( 'problem3():' ) print ( ' nchoosek' ) n = 6 k = 2 nchoosek = factorial ( n ) // factorial ( n - k ) // factorial ( k ) print ( n, 'choose', k, ' = ', nchoosek ) n = 60 k = 20 nchoosek = factorial ( n ) // factorial ( n - k ) // factorial ( k ) print ( n, 'choose', k, ' = ', nchoosek ) n = 600 k = 200 nchoosek = factorial ( n ) // factorial ( n - k ) // factorial ( k ) print ( n, 'choose', k, ' = ', nchoosek ) return def problem4 ( ): #*****************************************************************************80 # ## problem4(): mandelbrot. # from math import factorial print ( '' ) print ( 'problem4():' ) print ( ' mandelbrot' ) c = - 0.80 + 0.1j print ( '' ) print ( 'starting point = ', c ) z = c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) c = - 0.75 + 0.4j print ( '' ) print ( 'starting point = ', c ) z = c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) z = z**2 + c; print ( abs ( z ) ) return def problem5 ( ): #*****************************************************************************80 # ## problem5(): Heron's formula. # from math import sqrt print ( '' ) print ( 'problem5():' ) print ( ' Heron\'s triangle area formula.' ) a = 10 b = 12 c = 6 s = ( a + b + c ) / 2.0 area = sqrt ( a * ( s - a ) * ( s - b ) * ( s - c ) ) print ( ' Area = ', area ) print ( '' ) a = 5 b = 12 c = 6 s = ( a + b + c ) / 2.0 try: area = sqrt ( a * ( s - a ) * ( s - b ) * ( s - c ) ) print ( ' Area = ', area ) except ( ValueError ): print ( ' Illegal square root!' ) return def problem6 ( ): #*****************************************************************************80 # ## problem6(): Dice problem. # from math import sqrt print ( '' ) print ( 'problem6():' ) print ( ' The dice problem.' ) a = 2 b = 6 c = ( ( 3 * ( a**3 * b - b**3 * a ) ) % 7 ) print ( ' a = ', a, ' b = ', b, ' c = ', c ) a = 3 b = 5 c = ( ( 3 * ( a**3 * b - b**3 * a ) ) % 7 ) print ( ' a = ', a, ' b = ', b, ' c = ', c ) return def problem7 ( ): #*****************************************************************************80 # ## problem7(): Paper folding # from math import log print ( '' ) print ( 'problem7():' ) print ( ' Repeatedly fold a piece of paper.' ) tmm = 0.1 Dkm = 384400 k = log ( Dkm * 1000000 / tmm ) / log ( 2 ) k = int ( k + 1 ) print ( ' k = ', k ) return def problem8 ( ): #*****************************************************************************80 # ## problem8(): Earth surface area # from math import atanh from math import pi from math import sqrt print ( '' ) print ( 'problem8():' ) print ( ' Earth surface area.' ) a = 6378137.0 c = 6356752.314245 e = sqrt ( 1 - c**2 / a**2 ) area = 2.0 * pi * a**2 * ( 1 + ( 1 - e**2 ) / e * atanh ( e ) ) print ( ' Estimate for Earth surface area = ', area ) return def problem9 ( ): #*****************************************************************************80 # ## problem9(): Estimate exp(4) # from math import exp from math import factorial print ( '' ) print ( 'problem9():' ) print ( ' Estimate exp(4).' ) exact = exp ( 4.0 ) print ( ' Exact value of exp(4) = ', exact ) est = 0 k = -1 x = 4.0 k = k + 1 est = est + x**k / factorial ( k ) print ( ' Estimate ', k, ' = ', est, ' error = ', abs ( est - exact ) ) k = k + 1 est = est + x**k / factorial ( k ) print ( ' Estimate ', k, ' = ', est, ' error = ', abs ( est - exact ) ) k = k + 1 est = est + x**k / factorial ( k ) print ( ' Estimate ', k, ' = ', est, ' error = ', abs ( est - exact ) ) k = k + 1 est = est + x**k / factorial ( k ) print ( ' Estimate ', k, ' = ', est, ' error = ', abs ( est - exact ) ) k = k + 1 est = est + x**k / factorial ( k ) print ( ' Estimate ', k, ' = ', est, ' error = ', abs ( est - exact ) ) k = k + 1 est = est + x**k / factorial ( k ) print ( ' Estimate ', k, ' = ', est, ' error = ', abs ( est - exact ) ) k = k + 1 est = est + x**k / factorial ( k ) print ( ' Estimate ', k, ' = ', est, ' error = ', abs ( est - exact ) ) k = k + 1 est = est + x**k / factorial ( k ) print ( ' Estimate ', k, ' = ', est, ' error = ', abs ( est - exact ) ) k = k + 1 est = est + x**k / factorial ( k ) print ( ' Estimate ', k, ' = ', est, ' error = ', abs ( est - exact ) ) k = k + 1 est = est + x**k / factorial ( k ) print ( ' Estimate ', k, ' = ', est, ' error = ', abs ( est - exact ) ) return if ( __name__ == "__main__" ): problem0 ( ) problem1 ( ) problem2 ( ) problem3 ( ) problem4 ( ) problem5 ( ) problem6 ( ) problem7 ( ) problem8 ( ) problem9 ( )