如何在没有 math.h 的情况下计算 n 次方根

How to calculate nth roots without math.h

我必须在没有 math.h 的情况下进行第 n 次根 这是我到目前为止得到的: n√(x)=x^(1/n)

#include <stdio.h>
#include <iostream.h>
#include <windows.h>
int main ()
{
float num1,num2,t; t=1;
cout<<"Type your radicand "; cin>>num1;
cout<<"Type your index "; cin>>num2;

for (int i=0;i<1/num2;i++) //this is the exponent
{t=t*num1;}
cout<<"The answer is= "<<t;
system ("PAUSE");
}

(Borland C++ 5.5) 问题是我找不到方法来做 x^(1/n)

最直接的方法可能是使用牛顿法来近似平方根。

http://en.wikipedia.org/wiki/Newton%27s_method

你想要的方程式是:
sqrt(N) = x ~= 1/2 * (N/x + x)

重复几次直到 x 停止快速变化(从对 x 的猜测开始,例如“N 的 1/2”)。您可以通过比较当前迭代 (p) 和上一次迭代 (q) 的值来完成此操作,如下所示:

e = (p-q)/p

一旦 e 低于您为 "close enough" 选择的值,您就到达了根!

或者,您可以

您可以重新表述问题 y = sqrt(x) 以找到 y 的值,使得 y*y - x 等于零。求解该方程式的最简单方法是对 y (http://en.wikipedia.org/wiki/Bisection_method) 在 0 和 x 之间进行二等分(因为 0 <= sqrt(x) <= x 对于所有实数 x 其中x 应该满足 x >= 0).

您也可以求助于牛顿法,尽管它稍微高级一点并且不能保证收敛(而二分法是,如果正确答案在初始区间内)。