I was looking for root algorithms and came across a Babylonian algorithm. I really like it because it is simple and easy to understand. But the problem is that it only takes square roots when I create a function that can take the root of a number with any power. I'm just trying to take his positive integer.
Here is the function:
double functions::rot(double x, double y) {
double z = x;
double w = 1;
double e = 0.000001;
while (z - w > e){
z = (z + w) / 2;
w = x / z;
}
return z;
}
y is the power. Does anyone have a way to change this algorithm, so y is the root strength? For example, if y = 3, it takes a cubic root.
source
share