MCPcopy Create free account
hub / github.com/Manvityagi/PW-Skills-Java-Course-Codes / Power

Class Power

Lecture 29 - Recursion PS2/src/Power.java:1–19  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1public 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected