| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | title: Exponentiation | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | ## Exponentiation
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Given two integers a and n, write a function to compute a^n. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### Code
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Algorithmic Paradigm: Divide and conquer. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```C | 
					
						
							|  |  |  | int power(int x, unsigned int y) {  | 
					
						
							|  |  |  |     if (y == 0)  | 
					
						
							|  |  |  |         return 1;  | 
					
						
							|  |  |  |     else if (y%2 == 0)  | 
					
						
							|  |  |  |         return power(x, y/2)*power(x, y/2);  | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         return x*power(x, y/2)*power(x, y/2);  | 
					
						
							|  |  |  | }  | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | Time Complexity: O(n) | Space Complexity: O(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ####  Optimized Solution: O(logn)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```C | 
					
						
							|  |  |  | int power(int x, unsigned int y) {  | 
					
						
							|  |  |  |     int temp;  | 
					
						
							|  |  |  |     if( y == 0)  | 
					
						
							|  |  |  |         return 1;  | 
					
						
							|  |  |  |     temp = power(x, y/2);  | 
					
						
							|  |  |  |     if (y%2 == 0)  | 
					
						
							|  |  |  |         return temp*temp;  | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         return x*temp*temp;  | 
					
						
							|  |  |  | }  | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2019-05-25 11:30:31 +05:30
										 |  |  | Why is this faster? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Suppose we have x = 5, y = 4, we know that our answer is going to be (5 * 5 * 5 * 5).  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If we break this down, we notice that we can write (5 * 5 * 5 * 5) as (5 * 5) * 2 and further, we can write (5 * 5) as 5 * 2. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Through this observation, we can optimize our function to O(log n) by calculating power(x, y/2) only once and storing it.  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Modular Exponentiation
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 18:08:17 -05:00
										 |  |  | Given three numbers x, y, and p, compute (x^y) % p | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```C | 
					
						
							|  |  |  | int power(int x, unsigned int y, int p) {  | 
					
						
							|  |  |  |     int res = 1;   | 
					
						
							|  |  |  |     x = x % p;  | 
					
						
							|  |  |  |     while (y > 0) {   | 
					
						
							|  |  |  |         if (y & 1)  | 
					
						
							|  |  |  |             res = (res*x) % p;  | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |         // y must be even now  | 
					
						
							| 
									
										
										
										
											2019-05-25 11:30:31 +05:30
										 |  |  |         y = y >> 1;  | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |         x = (x*x) % p;    | 
					
						
							|  |  |  |     }  | 
					
						
							|  |  |  |     return res;  | 
					
						
							|  |  |  | }  | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | Time Complexity: O(Log y). | 
					
						
							| 
									
										
										
										
											2019-04-01 14:25:53 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #### More Information:
 | 
					
						
							|  |  |  | - [Basic Fundamentals](https://mathinsight.org/exponentiation_basic_rules) |