mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
Change project structure to a Maven Java project + Refactor (#2816)
This commit is contained in:

committed by
GitHub

parent
8e533d2617
commit
9fb3364ccc
28
src/main/java/com/thealgorithms/maths/Pow.java
Normal file
28
src/main/java/com/thealgorithms/maths/Pow.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
// POWER (exponentials) Examples (a^b)
|
||||
public class Pow {
|
||||
|
||||
public static void main(String[] args) {
|
||||
assert pow(2, 0) == Math.pow(2, 0); // == 1
|
||||
assert pow(0, 2) == Math.pow(0, 2); // == 0
|
||||
assert pow(2, 10) == Math.pow(2, 10); // == 1024
|
||||
assert pow(10, 2) == Math.pow(10, 2); // == 100
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
long result = 1;
|
||||
for (int i = 1; i <= b; i++) {
|
||||
result *= a;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user