Count rotations divisible by 8 | function to count of all rotations divisible by 8 ; For single digit number ; For two - digit numbers ( considering all pairs ) ; first pair ; second pair ; considering all three - digit sequences ; Considering the number formed by the last digit and the first two digits ; Considering the number formed by the last two digits and the first digit ; required count of rotations ; Driver Code
< ? php function countRotationsDivBy8 ( $ n ) { $ len = strlen ( $ n ) ; $ count = 0 ; if ( $ len == 1 ) { $ oneDigit = $ n [ 0 ] - '0' ; if ( $ oneDigit % 8 == 0 ) return 1 ; return 0 ; } if ( $ len == 2 ) { $ first = ( $ n [ 0 ] - '0' ) * 10 + ( $ n [ 1 ] - '0' ) ; $ second = ( $ n [ 1 ] - '0' ) * 10 + ( $ n [ 0 ] - '0' ) ; if ( $ first % 8 == 0 ) $ count ++ ; if ( $ second % 8 == 0 ) $ count ++ ; return $ count ; } $ threeDigit ; for ( $ i = 0 ; $ i < ( $ len - 2 ) ; $ i ++ ) { $ threeDigit = ( $ n [ $ i ] - '0' ) * 100 + ( $ n [ $ i + 1 ] - '0' ) * 10 + ( $ n [ $ i + 2 ] - '0' ) ; if ( $ threeDigit % 8 == 0 ) $ count ++ ; } $ threeDigit = ( $ n [ $ len - 1 ] - '0' ) * 100 + ( $ n [ 0 ] - '0' ) * 10 + ( $ n [ 1 ] - '0' ) ; if ( $ threeDigit % 8 == 0 ) $ count ++ ; $ threeDigit = ( $ n [ $ len - 2 ] - '0' ) * 100 + ( $ n [ $ len - 1 ] - '0' ) * 10 + ( $ n [ 0 ] - '0' ) ; if ( $ threeDigit % 8 == 0 ) $ count ++ ; return $ count ; } $ n = "43262488612" ; echo " Rotations : ▁ " . countRotationsDivBy8 ( $ n ) ; ? >