java 如何获得任何数字的N次方
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6592169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Nth Power of any number
提问by Sunil Kumar Sahoo
I faced an interview. where I was asked the following question
我遇到了面试。我被问到以下问题的地方
Write a function in any of programming language that computes the nth power of a number w/o using + * or ^ or declaring a new variable inside the function or using any library function (eg Math lib in java).
使用任何一种编程语言编写一个函数,使用 + * 或 ^ 或在函数内声明一个新变量或使用任何库函数(例如 java 中的 Math lib)来计算数字的 n 次方。
I have used pow function of java Math.pow(a, b)
我用过java的pow函数 Math.pow(a, b)
Thanks
谢谢
采纳答案by Sunil Kumar Sahoo
I am using java programming language. The interviewer restricted you to declare a new variable inside the method better you pass it to the function. The interviewer didnt restrict you to use division operator (/) so you can use that.
我正在使用 java 编程语言。面试官限制你在方法中声明一个新变量,你最好将它传递给函数。面试官没有限制你使用除法运算符(/),所以你可以使用它。
static double getNthPowerOfNumber(double originalNumber,
int power) {
if (power == 0) {
return 1;
}
if (originalNumber == 0) {
return 0;
} else {
originalNumber/=1/getNthPowerOfNumber(originalNumber, --power);
return originalNumber;
}
}
if you want to get 5th power of a number 3 then write System.out.println("4..double..." + getNthPowerOfNumber(4, 1));
如果你想得到数字 3 的 5 次方,那么写 System.out.println("4..double..." + getNthPowerOfNumber(4, 1));
回答by GargantuChet
They're asking whether you understand recursion. Considering x ^ k for some integer k,
他们问你是否理解递归。考虑某个整数 k 的 x ^ k,
- when k < 0, xk= xk+1/ x
- when k = 0, xk= 1
- when k > 0, xk= xk-1* x
- 当 k < 0 时,x k= x k+1/ x
- 当 k = 0 时,x k= 1
- 当 k > 0 时,x k= x k-1* x
Turning this into code shouldn't be too bad. Let's use multiplication for now, and take it out later.
把它变成代码应该不会太糟糕。我们先用乘法,以后再取出来。
double recursivePower(double x, int k) {
if (k < 0) {
return power(x, ++k) / x;
} else if (k == 0) {
return 1;
} else {
return power(x, --k) * x;
}
}
Now, to get rid of the multiplication. Since n * m = n / (1/m), we can rewrite the last calculation as power(x, --k) / (1/x)
:
现在,摆脱乘法。由于 n * m = n / (1/m),我们可以将最后的计算重写为power(x, --k) / (1/x)
:
double recursivePower(double x, int k) {
if (k < 0) {
return recursivePower(x, ++k) / x;
} else if (k == 0) {
return 1;
} else {
return recursivePower(x, --k) / (1 / x);
}
}
Fractional exponents could probably be done in the same style. If they want irrational exponents to be handled in the same way, I'd ask for Google and a fair amount of time to think about the problem.
分数指数可能以相同的方式完成。如果他们希望以同样的方式处理非理性指数,我会要求谷歌和相当多的时间来考虑这个问题。
回答by Dapeng
static public int power(int value, int pow){
if(pow == 0) return 1;
return value * power(value, pow -1);
}
回答by jaredbranum
Done in JavaScript:
在 JavaScript 中完成:
function power(num,pow){
if (pow == 0) return 1
num /= 1/(power(num,--pow))
return num
}
Call it like:
像这样称呼它:
power(2,0) // -> 1
power(5,2) // -> 25
power(7,3) // -> 343
I feel like inverse division is cheating the no * operator rule, but eh, maybe that's what they were looking for.
我觉得逆除法在欺骗 no * 运算符规则,但是呃,也许这就是他们正在寻找的。