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 ; PHP function to calculate number of digits in x ; PHP function to print 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 print zeros ; If temp is not zero then print temp . If temp is zero then already printed ; Driver Code
< ? php function power ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function numberOfDigits ( $ x ) { $ i = 0 ; while ( $ x ) { $ x /= 10 ; $ i ++ ; } return $ i ; } function LastTwoDigit ( $ n ) { echo ( " Last ▁ " . 2 ) ; echo ( " ▁ digits ▁ of ▁ " . 2 ) ; echo ( " ^ " . $ n . " ▁ = ▁ " ) ; $ temp = 1 ; for ( $ i = 1 ; $ i <= 2 ; $ i ++ ) $ temp *= 10 ; $ temp = power ( 2 , $ n , $ temp ) ; for ( $ i = 0 ; $ i < 2 - numberOfDigits ( $ temp ) ; $ i ++ ) echo ( 0 ) ; if ( $ temp ) echo ( $ temp ) ; } $ n = 72 ; LastTwoDigit ( $ n ) ; ? >