Writing power function for large numbers | Maximum number of digits in output ; This function multiplies x with the number represented by res [ ] . res_size is size of res [ ] or number of digits in the number represented by res [ ] . This function uses simple school mathematics for multiplication . This function may value of res_size and returns the new value of res_size ; Initialize carry ; One by one multiply n with individual digits of res [ ] ; Store last digit of ' prod ' in res [ ] ; Put rest in carry ; Put carry in res and increase result size ; This function finds power of a number x ; printing value "1" for power = 0 ; Initialize result ; Multiply x n times ( x ^ n = x * x * x ... . n times ) ; Driver program
MAX = 100000 NEW_LINE def multiply ( x , res , res_size ) : NEW_LINE INDENT carry = 0 NEW_LINE for i in range ( res_size ) : NEW_LINE INDENT prod = res [ i ] * x + carry NEW_LINE res [ i ] = prod % 10 NEW_LINE carry = prod // 10 NEW_LINE DEDENT while ( carry ) : NEW_LINE INDENT res [ res_size ] = carry % 10 NEW_LINE carry = carry // 10 NEW_LINE res_size += 1 NEW_LINE DEDENT return res_size NEW_LINE DEDENT def power ( x , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT print ( "1" ) NEW_LINE return NEW_LINE DEDENT res = [ 0 for i in range ( MAX ) ] NEW_LINE res_size = 0 NEW_LINE temp = x NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT res [ res_size ] = temp % 10 ; NEW_LINE res_size += 1 NEW_LINE temp = temp // 10 NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res_size = multiply ( x , res , res_size ) NEW_LINE DEDENT print ( x , " ^ " , n , " ▁ = ▁ " , end = " " ) NEW_LINE for i in range ( res_size - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( res [ i ] , end = " " ) NEW_LINE DEDENT DEDENT exponent = 100 NEW_LINE base = 2 NEW_LINE power ( base , exponent ) NEW_LINE