Pow Recursion

package com.thealgorithms.maths;

public class PowRecursion {

    public static void main(String[] args) {
        assert Double.compare(pow(2, 0), Math.pow(2, 0)) == 0;
        assert Double.compare(pow(0, 2), Math.pow(0, 2)) == 0;
        assert Double.compare(pow(2, 10), Math.pow(2, 10)) == 0;
        assert Double.compare(pow(10, 2), Math.pow(10, 2)) == 0;
    }

    /**
     * Returns the value of the first argument raised to the power of the second
     * argument
     *
     * @param a the base.
     * @param b the exponent.
     * @return the value {@code a}<sup>{@code b}</sup>.
     */
    public static long pow(int a, int b) {
        return b == 0 ? 1 : a * pow(a, b - 1);
    }
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.