JavaRush /Java Blog /Random EN /How to perform exponentiation in Java

How to perform exponentiation in Java

Published in the Random EN group
hello world! Googling something or asking for help on the forum is a common thing even for an experienced programmer. But there are some basic and simple topics in development that even a green beginner should know about them. And here is one of those topics. Today we will talk about how exponentiation is performed in the Java language. How to perform exponentiation in Java - 1Imagine for a second that you were given a task: to find a number to a certain degree. Sounds pretty simple, but how to implement the solution? Let's look at the most common way and a few alternatives. And before we “dive” into solutions, first, let’s remember what raising a number to a power is: How to perform exponentiation in Java - 2Exponentiation is an operation in which one number is multiplied by itself several times. The number that is multiplied is called the base, and the number of times it is multiplied is called the exponent. Well, the result of this self-multiplication of the base is called exponentiation. For example, for 8 it is 2 to the third power, since 2x2x2=8. Raising any number to the second power indicates that we are making it a multiplier twice, and as a rule this power is called the square. That is, 4 squared = 4x4 = 16. So, we refreshed our memory, and now we go directly to how to use pow in Java - a method for raising to a power.
  1. Math pow

    The easiest way to solve this problem is to use the Math class. This is the solution you will use in most cases.

    How to perform exponentiation in Java - 3

    The Math class contains methods related to trigonometry, geometry, and other aspects of mathematics. In it, the methods are implemented as static, so you can immediately call through the class name Math without creating a class object.

    How we will look like exponentiation:

    public static int pow(int value, int powValue) {
       return (int) Math.pow(value, powValue);
    }

    We had to use a type cast (int), since this method of the Math class returns a value of type double (the arguments are also double, but implicit type casting is used there).

    And now - a bonus: additional options.

  2. The value of the square of a number

    Let's start with the most simple.

    This is how the method for squaring is written:

    public static int pow(int value){
       return value*value;
    }

    Call to main:

    public static void main(String[] args) {
       System.out.println(Solution.pow(7));
    }

    That's all - nothing complicated and superfluous.

  3. Number to the power

    But the number squared is far from all we need. Most often, we will need a number in our work to a certain extent, so the following is a slightly more complicated version, but with a custom java pow value:

    public static void main(String[] args) {
       System.out.println(Solution.pow(7, 4));
    }
    
    public static int pow(int value, int powValue) {
       int result = 1;
       for (int i = 1; i <= powValue; i++) {
           result = result * value;
       }
       return result;
    }

    The algorithm is very simple: we kind of set the starting point of result, and then multiply it by our value value as many times as the loop with powValue will run (powValue number of times)

  4. recursion

    The next way will be a little more exotic, but no less cool.

    How to perform exponentiation in Java - 4

    Recursion is a facility that allows a method to call itself. Java has such a mechanism, and such methods are called recursive methods, respectively.

    Many, if not all, algorithmic problems can be solved recursively. This one will be no exception either, so let's take a look at how you can raise a number to a certain power in a recursive way:

    public static int pow(int value, int powValue) {
       if (powValue == 1) {
           return value;
       } else {
           return value * pow(value, powValue - 1);
       }
    }

    As you can see, we have two cases:

    1. The condition for exiting the recursion, or in other words, when our degree value reaches one, we will begin to be thrown back.
    2. The very mechanism of multiplying value by the result of calling the same method, but with powValue - 1.

    Well, now it's time to look at more lazy ways, namely, out-of-the-box methods.

  5. BigInteger

    The main purpose of the BigInteger class is to store integers of arbitrary size, but at the same time it has various arithmetic methods that allow you to work with these huge (well, or not so) numbers.

    How to perform exponentiation in Java - 5

    You can read more about BigInteger in this article .

    So what would exponentiation look like with BigInteger in Java?

    public static int pow(int value, int powValue) {
       BigInteger a = new BigInteger(String.valueOf(value));
      return a.pow(powValue).intValue();
    }

    Pretty simple and no frills, right?

How to perform exponentiation in Java - 6Well, that's all for today! Now you know about a variety of ways to exponentiate. Agree, it was a simple topic :)
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION