|
CS 3343/3341 Analysis of Algorithms Spring 2012 Recitation 1 Circular Queues, Etc. Partial Answers |
2^27: First build the table: 2^0 = 1 2^1 = 2<-- use (2^1)^2 = 2^2 = 4<-- use (2^2)^2 = 2^4 = 16 (2^4)^2 = 2^8 = 256<-- use (2^8)^2 = 2^16 = 65536 <-- use (2^16)^2 = 2^32 = 8 589 934 592 (not needed or wanted here) 27 = 2^4 + 2^3 + 2^1 + 2^0 = 16 + 8 + 2 + 1 = 1 1 0 1 1 (base 2) (This shows which table entries to use above, marked "<-- use".) 2^27 = 2^16 * 2^8 * 2^2 * 2^1 = 65536*256*4*2 = 134 217 728
// exps: slow exponentiation int exps(int A, int B) { int a = A, b = B; int d = 1; while(b > 0) { d = d*a; b = b--; } return d; } |
d*ab = AB |