Check if binary string multiple of 3 using DFA | Function to check if the binary String is divisible by 3. ; checking if the bit is nonzero ; checking if the nonzero bit is at even position ; Checking if the difference of non - zero oddbits and evenbits is divisible by 3. ; Driver Program
function CheckDivisibilty ( A ) { var oddbits = 0 , evenbits = 0 ; for ( var counter = 0 ; counter < A . length ; counter ++ ) { if ( A [ counter ] == ' ' ) { if ( counter % 2 == 0 ) { evenbits ++ ; } else { oddbits ++ ; } } } if ( Math . abs ( oddbits - evenbits ) % 3 == 0 ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; } } var A = " " ; CheckDivisibilty ( A ) ;