[Offer] [16] [] value of a power of

Title Description

  Function to achieve double Power (double base, int exponent), seeking base of the exponent power. Not use library functions, while eliminating the need to consider the problem of large numbers.
  

Ideas analysis

  1. After taking into consideration the situation index is negative, and when the base index is negative can not be zero, because the index is negative, is the reciprocal of the absolute value of the power of the index (the denominator can not be zero), taking into account these circumstances, it is case can be converted to an absolute value of exponent power demand problem, i.e., the index is a positive number
  2. Now consider (the index case is a positive number) How to find the value of the integer power:

    • One method is a direct request, cycle times exponent exponent a base to obtain the product,
    • The second clever way, you can use Fibonacci number of ideas, here recursive method,

      • When the index is an even number: it can be represented by two baseof ex/2the product of the power
      • Odd: can be represented by two baseof the ex/2power multiplied by a product base(here ex/2is an operation in the program, 5/2 = 2)
      • Formula is as follows:

Test Case

  Index and base number, are provided as positive, negative and zero.

Java code

public class Offer16 {

    public static void main(String[] args) {
        test1();
        test2();
        test3();
    }

    public static double powCustom(double base, int exponent) {
        return Solution2(base, exponent);
    }

    /**
     * 解法一, 要考虑到边界值, exponent为负数时,还要比如0的负数次方
     * 
     * 其中powCustomCore1 方法是利用 直接求的方法
     * 
     * @param base
     * @param exponent
     * @return
     */
    private static double Solution1(double base, int exponent) {
        if (base == 0 && exponent < 0) {
            throw new IllegalArgumentException("0的指数不能为负数");
        }
        int absExponent = exponent;
        if (exponent < 0) {
            absExponent = -exponent;
        }
        double result = powCustomCore1(base, absExponent);
        if (exponent < 0) {
            result = 1.0 / result;
        }
        return result;
    }

    /**
     * 方法一: 直接求,将exponent个 base 相乘
     * 
     * @param base     基数
     * @param exponent 指数
     * @return
     */
    private static double powCustomCore1(double base, int exponent) {
        double result = 1.0;
        for (int i = 1; i <= exponent; i++) {
            result *= base;
        }
        return result;
    }

    /**
     * 解法二, 要考虑到边界值, exponent为负数时,还要比如0的负数次方
     * 
     * 其中 powCustomCore2 方法是利用 递归的方法
     * 
     * @param base
     * @param exponent
     * @return
     */
    private static double Solution2(double base, int exponent) {
        if (base == 0 && exponent < 0) {
            throw new IllegalArgumentException("0的指数不能为负数!");
        }
        int absExponent = exponent;
        if (exponent < 0) {
            absExponent = -exponent;
        }
        double result = powCustomCore2(base, absExponent);
        if (exponent < 0) {
            result = 1.0 / result;
        }
        return result;
    }

    /**
     * 方法二:
     * 
     * exponent为偶数时,可以将其简化为 两个base的 ex/2 次幂 相乘 exponent为奇数时,可以将其简化为,两个base的 ex/2 次幂
     * 相乘之后再乘以base
     * 
     * @param base     基数
     * @param exponent 指数
     * @return
     */
    private static double powCustomCore2(double base, int exponent) {
        if (exponent == 0) {
            return 0;
        }
        if (exponent == 1) {
            return base;
        }
        double result = powCustomCore2(base, exponent >> 1);
        result *= result;
        if ((exponent & 1) == 1) {
            result *= base;
        }
        return result;
    }

    private static void test1() {
        System.out.println("3,3---->" + powCustom(3, 3));
    }

    private static void test2() {
        System.out.println("3,-3----->" + powCustom(3, -3));
    }

    private static void test3() {
        System.out.println("-3,3------>" + powCustom(-3, 3));
    }
}

Code link

Offer to prove safety codes -Java

Guess you like

Origin www.cnblogs.com/haoworld/p/offer16-shu-zhi-de-zheng-shu-ci-fang.html