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 Code
let MAX = 100000 function multiply ( x , res , res_size ) { let carry = 0 ; for ( let i = 0 ; i < res_size ; i ++ ) { let prod = res [ i ] * x + carry ; res [ i ] = prod % 10 ; carry = Math . floor ( prod / 10 ) ; } while ( carry ) { res [ res_size ] = carry % 10 ; carry = Math . floor ( carry / 10 ) ; res_size ++ ; } return res_size ; } function power ( x , n ) { if ( n == 0 ) { document . write ( " " ) ; return ; } let res = new Array ( MAX ) ; let res_size = 0 ; let temp = x ; while ( temp != 0 ) { res [ res_size ++ ] = temp % 10 ; temp = Math . floor ( temp / 10 ) ; } for ( let i = 2 ; i <= n ; i ++ ) res_size = multiply ( x , res , res_size ) ; document . write ( x + " " + n + " " ) ; for ( let i = res_size - 1 ; i >= 0 ; i -- ) document . write ( res [ i ] ) ; } let exponent = 100 ; let base = 2 ; power ( base , exponent ) ;