using recursion

This commit is contained in:
shellhub 2019-10-24 11:54:37 +08:00
parent 56e887213b
commit d11b7347b6

View File

@ -17,10 +17,10 @@ public class PowRecursion {
* @return the value {@code a}<sup>{@code b}</sup>. * @return the value {@code a}<sup>{@code b}</sup>.
*/ */
public static long pow(int a, int b) { public static long pow(int a, int b) {
int result = 1; if (b == 0) {
for (int i = 1; i <= b; i++) { return 1;
result *= a; } else {
return a * pow(a, b - 1);
} }
return result;
} }
} }