📜  java 二进制求幂 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:18.701000             🧑  作者: Mango

代码示例1
/**
* Calculate a^n in O(logn) time, instead of O(n) time with naive approach.
**/
public static long powerRecursive(int base, int exponent){
  if(exponent == 0) return 1;
  return (exponent % 2 == 0) ?
    powerRecursive(base, exponent / 2) * powerRecursive(base, exponent / 2)
    : base * powerRecursive(base, (exponent - 1) / 2) *
      powerRecursive(base, (exponent - 1) / 2);
}