Objective: To write and run a python script that approximates the cube root of an integer, by another integer. Problem:
Posted: Mon May 09, 2022 7:02 am
Objective: To write and run a python script that approximates the cube root of an integer, by another integer. Problem: Let n > 0 be an integer, and let f(x) = x3, n. There is a unique real root to the equation f(x) = 0. It is called the cube root of n, and denoted n1/3. In this assignment, the problem is to find an integer r which is an approximation to n1/3. Two different methods will be used to solve this problem. The two methods are described in more detail below. It turns out that the methods produce different outcomes. Method 1. Since a cube root of n is a root of f(x), the first method is based on the notion that an approximation to n1/3 should be an integer rı such that f(t) is very close to zero. In other words, method one solves for an integer such that 123 - n| is minimized. To minimize the function |23 – nl, you may follow one of the examples from class. A good place to start is the python script optimum.py. It uses a linear search method to locate the maximum and minimum values of a sequence. You should modify it to work for the function f(x)]. The bisection algorithm discussed in class is faster than the linear search. If you like 'this idea, and if you prefer the greater programming challenge, I recommend that you look in the python script integer_square_root.py. It contains a function int_sqrt that minimizes the function 22 - nl. The algorithm used is based on the bisection method. Modify it so that it computes a cube root instead of a square root.
= Method 2. The second method is to use the built-in python power function to approximate n1/3 by a floating point number. After that, we simply take r2 to be the closest integer to n1/3. To find the integer that is closest to n1/3, first get a floating point approximation, then round off to an integer. Python has a built-in function for computing powers. The command pow(x, y) returns the floating point approximation to r'. The command round(x) uses a built-in python function to round the floating point number u to the nearest integer. If these two functions are applied sequentially: r = pow(n, 1.0/3.0) r2 = round(r) then the variable r2 contains the integer which is closest to n1/3. Program. Write a python program that implements the two methods. Test your program on the following values of n. (1) Apply both methods to compute the cube roots of the integers 90, 91, ..., 99. For which integers in this list are the methods finding different cube roots? (2) Apply both methods to compute the cube roots of x*** (x+1), for some large & values. For x you can try using the numbers in the list: 123456789,234567890, 345678901, 67890123456. If your program runs very slow, try substituting some smaller numbers. 1
(3) Count how many times the two methods give differing answers for n in the range 1, 2, ..., N. For N, you can try using 100000. If your program runs very slow, try substituting a smaller number for N.
= Method 2. The second method is to use the built-in python power function to approximate n1/3 by a floating point number. After that, we simply take r2 to be the closest integer to n1/3. To find the integer that is closest to n1/3, first get a floating point approximation, then round off to an integer. Python has a built-in function for computing powers. The command pow(x, y) returns the floating point approximation to r'. The command round(x) uses a built-in python function to round the floating point number u to the nearest integer. If these two functions are applied sequentially: r = pow(n, 1.0/3.0) r2 = round(r) then the variable r2 contains the integer which is closest to n1/3. Program. Write a python program that implements the two methods. Test your program on the following values of n. (1) Apply both methods to compute the cube roots of the integers 90, 91, ..., 99. For which integers in this list are the methods finding different cube roots? (2) Apply both methods to compute the cube roots of x*** (x+1), for some large & values. For x you can try using the numbers in the list: 123456789,234567890, 345678901, 67890123456. If your program runs very slow, try substituting some smaller numbers. 1
(3) Count how many times the two methods give differing answers for n in the range 1, 2, ..., N. For N, you can try using 100000. If your program runs very slow, try substituting a smaller number for N.