Program to find last two digits of 2 ^ n | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; JavaScript function to calculate number of digits in x ; JavaScript function to prlet last 2 digits of 2 ^ n ; Generating 10 ^ 2 ; Calling modular exponentiation ; Printing leftmost zeros . Since ( 2 ^ n ) % 2 can have digits less then 2. In that case we need to prlet zeros ; If temp is not zero then print temp . If temp is zero then already prleted ; driver program
function power ( x , y , p ) { while ( y > 0 ) { let r = y & 1 ; if ( r == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function numberOfDigits ( x ) { let i = 0 ; while ( x != 0 ) { x /= 10 ; i ++ ; } return i ; } function LastTwoDigit ( n ) { document . write ( " " + 2 + " " + 2 + " " ) ; document . write ( n + " " ) ; let temp = 1 ; for ( let i = 1 ; i <= 2 ; i ++ ) temp *= 10 ; temp = power ( 2 , n , temp ) ; for ( let i = 0 ; i < ( 2 - numberOfDigits ( temp ) ) ; i ++ ) document . write ( 0 + " " ) ; if ( temp != 0 ) document . write ( temp ) ; } let n = 72 ; LastTwoDigit ( n ) ;