I want to write a regular expression for Binary Numbers Divisible by 5.
I have already done the regular expressions for Binary Numbers Divisible by 2 and for 3 but I couldn't find one for 5.
Any suggestions?
5 in base 4 is equivalent to 11. Now apply the rule of divisibility by 11 where you add all the digits at odd places and add all the digits at even places and then subtract one from the other. If the result is divisible by 11(which remember is 5), then the binary number is divisible by 5.
For a number to be divisible by 5, the only condition is that the digit at the unit place in the number must be either 0 or 5.
Efficient Approach: In the binary string, check for the last k bits. If all the last k bits are 0, then the binary number is evenly divisible by 2k else it is not evenly divisible.
A number is divisible by 5 if the number's last digit is either 0 or 5. Divisibility by 5 - examples: The numbers 105, 275, 315, 420, 945, 760 can be divided by 5 evenly.
(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))*1)*
Add ^$
to test it with regexp. See it working here.
Becomes:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With