JavaRush /Java Blog /Random EN /How to Exponentiate in Java

How to Exponentiate in Java

Published in the Random EN group
Hello world! Googling something or asking for help on a forum is a common thing even for an experienced programmer. But there are such basic and simple topics in development that even a green beginner should know them. And here is one of those topics. Today we'll talk about how exponentiation is performed in Java. How to do exponentiation in Java - 1Let's imagine for a second that you were given a task: find a number to a certain degree. Sounds quite simple, but how to implement the solution? Let's look at the most common method and several alternatives. And before we “dive” into solutions, let’s first remember what raising a number to a power is: How to do exponentiation in Java - 2Raising to a power is an action 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-multiplying of the base is called exponentiation. For example, for 8 it is 2 to the third power, since 2x2x2=8. Raising a number to the second power indicates that we are making it a factor twice, and as a rule this power is called a square power. That is, 4 squared = 4x4 = 16. So, we have refreshed our memory, and now we move directly to ways to use pow in Java - a method for exponentiation.
  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 Exponentiate in Java - 3

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

    What does exponentiation look like:

    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 an implicit type cast is used there).

    And now - a bonus: additional options.

  2. The value of the square of a number

    Let's start, perhaps, with the simplest thing.

    This is how the method for squaring is written:

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

    Call in main:

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

    That's all - nothing complicated or unnecessary.

  3. Number to the power

    But the squared number is not all we need. Most often in our work we will need a number to a certain degree, so what follows 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 set the starting point result, and then multiply it by our value as many times as the loop with powValue runs (powValue number of times)

  4. Recursion

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

    How to Exponentiate in Java - 4

    Recursion is a feature that allows a method to call itself. In Java, such a mechanism is present, and such methods are accordingly called recursive.

    Many, if not all, algorithmic problems can be solved recursively. This one will also be no exception, 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 we can see, we have two cases:

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

    Well, now it’s time to look at lazier methods, namely, the “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 (or not so huge) numbers.

    How to Exponentiate in Java - 5

    You can read more about BigInteger in this article .

    So what would exponentiation look like using BigInteger in Java?

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

    Quite simple and without any problems, isn’t it?

Well, that's all for today! Now you know about a variety of ways to exponentiate. Agree, this was not a difficult topic :)
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION