How to write a function that can calculate power in Java. No loops

后端 未结 9 1102
一个人的身影
一个人的身影 2021-01-03 08:28

I\'ve been trying to write a simple function in Java that can calculate a number to the nth power without using loops.
I then found the Math.pow(a, b) class...

相关标签:
9条回答
  • 2021-01-03 09:11

    I think in Production recursion just does not provide high end performance.

    double power(double num, int exponent)
    {
    
    double value=1;
    int Originalexpn=exponent;
    double OriginalNumber=num;
    
    if(exponent==0)
        return value;
    
    if(exponent<0)
    {
        num=1/num;
        exponent=abs(exponent);
    }
    
    while(exponent>0)
    {
        value*=num;
        --exponent;
    }
    
    cout << OriginalNumber << " Raised to  " << Originalexpn << " is " << value << endl;
    return value;
    

    }

    0 讨论(0)
  • 2021-01-03 09:17

    Sure, create your own recursive function:

    public static int repeat(int base, int exp) {
     if (exp == 1) {
      return base;
     }
    
     return base * repeat(base, exp - 1);
    }
    

    Math.pow(a, b)

    Math is the class, pow is the method, a and b are the parameters.

    0 讨论(0)
  • 2021-01-03 09:21

    A recursive method would be the easiest for this :

    int power(int base, int exp) {
        if (exp != 1) {
            return (base * power(base, exp - 1));
        } else {
            return base;
        }
    }
    

    where base is the number and exp is the exponenet

    0 讨论(0)
  • 2021-01-03 09:28

    Another implementation with O(Log(n)) complexity

    public static long pow(long base, long exp){        
        if(exp ==0){
            return 1;
        }
        if(exp ==1){
            return base;
        }
    
        if(exp % 2 == 0){
            long half = pow(base, exp/2);
            return half * half;
        }else{
            long half = pow(base, (exp -1)/2);
            return base * half * half;
        }       
    }
    
    0 讨论(0)
  • 2021-01-03 09:29

    This one handles negative exponential:

    public static double pow(double base, int e) {
        int inc;
        if(e <= 0) {
            base = 1.0 / base;
            inc = 1;
        }
        else {
            inc = -1;
        }
        return doPow(base, e, inc);
    }
    
    private static double doPow(double base, int e, int inc) {
        if(e == 0) {
            return 1;
        }
        return base * doPow(base, e + inc, inc);
    }
    
    0 讨论(0)
  • 2021-01-03 09:32

    Here is a O(log(n)) code that calculates the power of a number. Algorithmic technique used is divide and conquer. It also accepts negative powers i.e., x^(-y)

    import java.util.Scanner;
    
    public class PowerOfANumber{
            public static void main(String args[]){
                    float result=0, base;
                    int power;
                    PowerOfANumber calcPower = new PowerOfANumber();
                    /* Get the user input for the base and power */
                    Scanner input = new Scanner(System.in);
                    System.out.println("Enter the base");   
                    base=input.nextFloat();
                    System.out.println("Enter the power");
                    power=input.nextInt();
                    result = calcPower.calculatePower(base,power);
                    System.out.println(base + "^" + power + " is " +result);
            }   
            private float calculatePower(float x, int y){ 
                    float temporary;
                    /* Termination condition for recursion */    
                    if(y==0)
                            return 1;
                    temporary=calculatePower(x,y/2);
                    /* Check if the power is even */
                    if(y%2==0)
                            return (temporary * temporary);
                    else{
                            if(y>0)
                                    return (x * temporary * temporary);
                            else
                                    return (temporary*temporary)/x;
                    }    
            }
    }
    
    0 讨论(0)
提交回复
热议问题