GeezerVison is showing github.com — add pow · pythonpeixun/Java@56e8872
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package maths; |
| 2 | + |
| 3 | +public class Pow { |
| 4 | + public static void main(String[] args) { |
| 5 | + assert pow(2, 0) == Math.pow(2, 0); |
| 6 | + assert pow(0, 2) == Math.pow(0, 2); |
| 7 | + assert pow(2, 10) == Math.pow(2, 10); |
| 8 | + assert pow(10, 2) == Math.pow(10, 2); |
| 9 | + } |
| 10 | + |
| 11 | + /** |
| 12 | + * Returns the value of the first argument raised to the power of the |
| 13 | + * second argument |
| 14 | + * |
| 15 | + * @param a the base. |
| 16 | + * @param b the exponent. |
| 17 | + * @return the value {@code a}<sup>{@code b}</sup>. |
| 18 | + */ |
| 19 | + public static long pow(int a, int b) { |
| 20 | + long result = 1; |
| 21 | + for (int i = 1; i <= b; i++) { |
| 22 | + result *= a; |
| 23 | + } |
| 24 | + return result; |
| 25 | + } |
| 26 | +} |
Collapse file
Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package Maths; |
| 2 | + |
| 3 | +public class PowRecursion { |
| 4 | + public static void main(String[] args) { |
| 5 | + assert pow(2, 0) == Math.pow(2, 0); |
| 6 | + assert pow(0, 2) == Math.pow(0, 2); |
| 7 | + assert pow(2, 10) == Math.pow(2, 10); |
| 8 | + assert pow(10, 2) == Math.pow(10, 2); |
| 9 | + } |
| 10 | + |
| 11 | + /** |
| 12 | + * Returns the value of the first argument raised to the power of the |
| 13 | + * second argument |
| 14 | + * |
| 15 | + * @param a the base. |
| 16 | + * @param b the exponent. |
| 17 | + * @return the value {@code a}<sup>{@code b}</sup>. |
| 18 | + */ |
| 19 | + public static long pow(int a, int b) { |
| 20 | + int result = 1; |
| 21 | + for (int i = 1; i <= b; i++) { |
| 22 | + result *= a; |
| 23 | + } |
| 24 | + return result; |
| 25 | + } |
| 26 | +} |