Computes the value of the base raised to the power of the exponent. The method calculates a b by iteratively multiplying the base a with itself b times. If the exponent b is negative, an IllegalArgumentException is thrown. @param a
(int a, int b)
| 24 | * @throws IllegalArgumentException if {@code b} is negative. |
| 25 | */ |
| 26 | public static long pow(int a, int b) { |
| 27 | if (b < 0) { |
| 28 | throw new IllegalArgumentException("Exponent must be non-negative."); |
| 29 | } |
| 30 | long result = 1; |
| 31 | for (int i = 1; i <= b; i++) { |
| 32 | result *= a; |
| 33 | } |
| 34 | return result; |
| 35 | } |
| 36 | } |
no outgoing calls