Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for binary multiple of 3

Tags:

regex

binary

I would like to know how can I construct a regex to know if a number in base 2 (binary) is multiple of 3. I had read in this thread Check if a number is divisible by 3 but they dont do it with a regex, and the graph someone drew is wrong(because it doesn't accept even numbers). I have tried with: ((1+)(0*)(1+))(0) but it doesn't works for some values. Hope you can help me.

UPDATE: Ok, thanks all for your help, now I know how to draw the NFA, here I left the graph and the regular expresion:

In the graph, the states are the number in base 10 mod 3.

For example: to go to state 1 you have to have 1, then you can add 1 or 0, if you add 1, you would have 11(3 in base 10), and this number mod 3 is 0 then you draw the arc to the state 0.

Whiteboard version

((0*)((11)*)((1((00) *)1) *)(101 *(0|((00) *1 *) *0)1) *(1(000)+1*01)*) *

And the other regex works, but this is shorter.

Thanks a lot :)

like image 918
voodoo14 Avatar asked Nov 02 '11 00:11

voodoo14


People also ask

How do you know if a binary string is divisible by 3?

Basically count the number of non-zero odd positions bits and non-zero even position bits from the right. If their difference is divisible by 3, then the number is divisible by 3. For example: 15 = 1111 which has 2 odd and 2 even non-zero bits.

Is binary number multiple of 3 GFG?

The given number can be big upto 2^10000. It is recommended to finish the task using one traversal of input binary string. Example 1: Input: S = "011" Output: 1 Explanation: "011" decimal equivalent is 3, which is divisible by 3.

How many states a DFA has if it accept all binary strings divisible by 3 and 2?

Explanation: In this DFA there are three states q0, q1, q2, q3 and the input is strings of {0, 1} which is interpreted as binary number. The state q0 is final state and q1, q2, q3 are non-final state.

How do you know if a binary number is divisible by 5?

If the number goes to five or above you subtract five. If you end up with 0 the number is divisible by 5.


2 Answers

I know this is an old question, but an efficient answer is yet to be given and this question pops up first for "binary divisible by 3 regex" on Google.

Based on the DFA proposed by the author, a ridiculously short regex can be generated by simplifying the routes a binary string can take through the DFA.

The simplest one, using only state A, is:

0*

Including state B:

0*(11)*0*

Including state C:

0*(1(01*0)*1)*0*

And include the fact that after going back to state A, the whole process can be started again.

0*((1(01*0)*1)*0*)*

Using some basic regex rules, this simplifies to

(1(01*0)*1|0)*

Have a nice day.

like image 196
Kert Ojasoo Avatar answered Sep 28 '22 00:09

Kert Ojasoo


If I may plug my solution for this code golf question! It's a piece of JavaScript that generates regexes (probably inefficiently, but does the job) for divisibility for each base.

This is what it generates for divisibility by 3 in base 2:

/^((((0+)?1)(10*1)*0)(0(10*1)*0|1)*(0(10*1)*(1(0+)?))|(((0+)?1)(10*1)*(1(0+)?)|(0(0+)?)))$/

Edit: comparing to Asmor's, probably very inefficient :)

Edit 2: Also, this is a duplicate of this question.

like image 36
Casey Chu Avatar answered Sep 28 '22 01:09

Casey Chu