Skip to content

Commit

Permalink
compute power
Browse files Browse the repository at this point in the history
  • Loading branch information
Atousa committed Apr 21, 2021
1 parent f6421b1 commit aa39d14
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Recursion/Power.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.beans.PropertyEditorSupport;

public class Power{
// 0(k)
public static int powerRec(int n , int k) {
if(k == 0) return 1;
return n*powerRec(n, k-1);
}

public static int powerIter(int n , int k) {
int result = 1;
for(int i = 1; i <= k; i++)
result*=n;
return result;
}

//o(logk) t(k/2) +1 , t(k/4) + 2, t(k/2^i)+ i == logk like binary search
public static int powerRecEf(int n , int k) {
if(k == 0) return 1;
if(k % 2 != 0) {
int result = n*powerRecEf(n,k-1);
return result;
} else {
int result = powerRecEf(n, k / 2);
return result * result;
}
}




public static void main(String[] args) {
System.out.println(powerRec(2,3));
System.out.println(powerIter(2 ,5));
System.out.println(powerRecEf(2 ,6));
}


}

0 comments on commit aa39d14

Please sign in to comment.