| 1 | public class Power { |
| 2 | // static int pow(int p, int q){ // p^q -> TC - O(q) |
| 3 | // if(q == 0) return 1; |
| 4 | // return pow(p, q-1) * p; // p^q-1 * p = p^q |
| 5 | // } |
| 6 | |
| 7 | static int pow(int p, int q){ |
| 8 | if(q == 0) return 1; |
| 9 | int smallPow = pow(p, q/2); |
| 10 | if(q % 2 == 0){ // even |
| 11 | return smallPow * smallPow; |
| 12 | } |
| 13 | return p * smallPow * smallPow; |
| 14 | } |
| 15 | |
| 16 | public static void main(String[] args) { |
| 17 | System.out.println(pow(2, 5)); |
| 18 | } |
| 19 | } |
nothing calls this directly
no outgoing calls
no test coverage detected