📜  不使用 Math pow() 方法在Java计算数字的幂

📅  最后修改于: 2022-05-13 01:55:09.510000             🧑  作者: Mango

不使用 Math pow() 方法在Java计算数字的幂

我们需要在不使用预定义函数Java.lang.Math.pow() 的情况下计算某个其他数字的幂

在Java,我们可以通过以下方式计算任何数字的幂:

  1. 通过while循环或for循环计算一个数的幂。
  2. 用分治法计算一个数的幂。

要计算任何数字的幂,需要基数和指数。

先决条件:

对Java算术运算运算符、数据类型、基本输入/输出和循环等的基本理解。



例子:

Input : base = 3, exponent = 3
Output: 27

Input : base = 2, exponent = 4
Output: 16

执行:

1.使用while循环: base值和power值已经被赋予了各自的值。现在,while 循环将继续将结果变量乘以基变量,直到幂变为零。

下面是使用 while 循环实现问题语句:

Java
// Calculate the power of a number in Java using while loop
import java.io.*;
  
public class JavaApplication1 {
    public static void main(String[] args)
    {
        int base = 3, power = 3;
        int result = 1;
        // running loop while the power > 0
        while (power != 0) {
            result = result * base;
            // power will get reduced after
            // each multiplication
            power--;
        }
        System.out.println("Result =  " + result);
    }
}


Java
// Calculate the power of a number in Java using for loop
import java.io.*;
  
public class JavaApplication1 {
    public static void main(String[] args)
    {
        int base = 3, power = 3;
        int result = 1;
  
        for (power = 3; power != 0; power--) {
            result = result * base;
        }
        System.out.println("Result =  " + result);
    }
}


Java
// Calculate the power of a number
// in Java using Divide and Conquer
class GFG {
  
    public static int power(int x, int y)
    {
        int temp;
        if (y == 0)
            return 1;
        temp = power(x, y / 2);
  
        if (y % 2 == 0)
            return temp * temp;
        else {
            if (y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int x = 2;
        int y = 3;
        System.out.println(power(x, y));
    }
}


输出
Result =  27

时间复杂度: O(N),其中 N 是幂。

2. 使用 for 循环: base 和 power 值已经分配了各自的值。现在,在上面的程序中,我们可以使用 for 循环而不是 while 循环。

下面是使用 while 循环实现问题语句:

Java

// Calculate the power of a number in Java using for loop
import java.io.*;
  
public class JavaApplication1 {
    public static void main(String[] args)
    {
        int base = 3, power = 3;
        int result = 1;
  
        for (power = 3; power != 0; power--) {
            result = result * base;
        }
        System.out.println("Result =  " + result);
    }
}
输出
Result =  27

时间复杂度: O(N),其中 N 是幂。

3.使用分而治之的方法:通过只计算一次幂(基数,指数/2)并存储它,上述操作可以优化为O(log N)。

Java

// Calculate the power of a number
// in Java using Divide and Conquer
class GFG {
  
    public static int power(int x, int y)
    {
        int temp;
        if (y == 0)
            return 1;
        temp = power(x, y / 2);
  
        if (y % 2 == 0)
            return temp * temp;
        else {
            if (y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int x = 2;
        int y = 3;
        System.out.println(power(x, y));
    }
}
输出
8

时间复杂度: O(log N),其中 N 是幂。