20-CS-122-001 | Computer Science II | Spring 2012 |
---|---|---|
Cube root algorithm |
Idea:
need to find m so that m*m*m = n. | |
which is the same as m = n/(m*m). | |
or 2m = (m + n/(m*m)). | |
and finally m = (m + n/(m*m))/2. |
Algorithm:
Let m = n be the input number | |
Input a number d representing precision | |
Let oldm be initialized to -1 | |
Repeat the following until abs(oldm-m) < d: | |
Set oldm to m | |
Set m to (m+(n/(m*m)))/2 | |
Output m |