Check if any permutation of a large number is divisible by 8 | PHP program to check if any permutation of a large number is divisible by 8 or not ; Function to check if any permutation of a large number is divisible by 8 ; Less than three digit number can be checked directly . ; check for the reverse of a number ; Stores the Frequency of characters in the n . ; Iterates for all three digit numbers divisible by 8 ; stores the frequency of all single digit in three - digit number ; check if the original number has the digit ; when all are checked its not possible ; Driver Code
< ? php error_reporting ( 0 ) ; function solve ( $ n , $ l ) { if ( $ l < 3 ) { if ( intval ( $ n ) % 8 == 0 ) return true ; strrev ( $ n ) ; if ( intval ( $ n ) % 8 == 0 ) return true ; return false ; } $ hash [ 10 ] = array ( 0 ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) $ hash [ $ n [ $ i ] - '0' ] ++ ; for ( $ i = 104 ; $ i < 1000 ; $ i += 8 ) { $ dup = $ i ; $ freq [ 10 ] = array ( 0 ) ; $ freq [ $ dup % 10 ] ++ ; $ dup = $ dup / 10 ; $ freq [ $ dup % 10 ] ++ ; $ dup = $ dup / 10 ; $ freq [ $ dup % 10 ] ++ ; $ dup = $ i ; if ( $ freq [ $ dup % 10 ] > $ hash [ $ dup % 10 ] ) continue ; $ dup = $ dup / 10 ; if ( $ freq [ $ dup % 10 ] > $ hash [ $ dup % 10 ] ) continue ; $ dup = $ dup / 10 ; if ( $ freq [ $ dup % 10 ] > $ hash [ $ dup % 10 ] ) continue ; return true ; } return false ; } $ number = "31462708" ; $ l = strlen ( $ number ) ; if ( solve ( $ number , $ l ) ) echo " Yes " ; else echo " No " ;