Regular Expression for Binary Numbers Divisible by 5 - regex

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?

(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))*1)*
Add ^$ to test it with regexp. See it working here.
You can build a DFA and convert it to regular expression. The DFA was already built in another answer. You can read it, it is very well explained.
The general idea is to remove nodes, adding edges.
Becomes:
Using this transformation and the DFA from the answer I linked, here are the steps to get the regular expression:
(EDIT: Note that the labels "Q3" and "Q4" have been mistakenly swapped in the diagram. These states represent the remainder after modulus 5.)

2^0 = 1 = 1 mod 5
2^1 = 2 = 2 mod 5
2^2 = 4 = -1 mod 5
2^3 = 8 = -2 mod 5
2^4 = 16 = 1 mod 5
2^5 = 32 = 2 mod 5
... -1 mod 5
... -2 mod 5
So we have a 1, 2, -1, -2 pattern. There are two subpatterns where only the sign of the number alternates: Let n is the digit number and the number of the least significant digit is 0; odd pattern is
(-1)^(n)
and even pattern is
2x((-1)^(n))
So, how to use this?
Let the original number be 100011, divide the numbers digits into two parts, even and odd. Sum each parts digits separately. Multiply sum of the odd digits by 2. Now, if the result is divisible by sum of the even digits, then the original number is divisible by 5, else it is not divisible. Example:
100011
1_0_1_ 1+0+1 = 2
_0_0_1 0+0+1 = 1; 1x2 = 2
2 mod(2) equals 0? Yes. Therefore, original number is divisible.
How to apply it within a regex? Using callout functions within a regex it can be applied. Callouts provide a means of temporarily passing control to the script in the middle of regular expression pattern matching.
However, ndn's answer is more appropriate and easier, therefore I recommend to use his answer.

However, "^(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))1)$" matches empty string.

Related

How can I use regex to capture this specfic set of ages?

I have a set of age data, like below;
1
2
3
4
5
6
7
8
9
10
1,1
1,2
1,3
2,12
11,13,15
7,8,12
12,15
14,16,17
15,6
13,11,10,2
And so on... I am trying to use Regex in to target a 'mixed' range of childrens ages. The logic requires at least a combination of 2 childen (so requires one of the lines with a comma), with at least one aged under 10 (min is 1), and at least one aged equal or greater to 10 (max 17).
My expected results from the above would be to return these lines below, and nothing else;
2,12
7,8,12
15,6
13,11,10,2
Any advice would be appreciated on how to resolve? Thanks in advance, I am continuing to try to correct.
You can use this regex to meet your requirements:
^(?=.*\b[1-9]\b)(?=.*\b1[0-7]\b)[0-9]+(?:,[0-9]+)+$
RegEx Demo
There are 2 lookaheads to assert 2 numbers one between 1-9 and another between 10-17
([1-9]) matches a number that should be between 1 and 9
1[0-7] matches a number that should be between 10 and 17
[0-9]+(?:,[0-9]+)+ in the regex is for matching 1 or more comma separated numbers in the middle.
You can do it with
\b\d,1[0-7]\b
provided the ages always are sorted (youngest to oldest).
If the age of 0 isn't allowed, change to
\b[1-9],1[0-7]\b
It checks for a single digit followed by a comma and one followed by a single digit in the range 0-7.
See it here at regex101.

RE: Number of a's is divisible by 6 and Number of b's is divisible by 8

Find a regular expression which represents strings made of {a, b}, where number of a's is divisible by 6 and number of b's is divisible by 8.
I tried to create a DFA which accepts such strings. My idea was to use all the remainders mod 6 and mod 8 leading to a total of 48 remainders. Thus each state in DFA is a pair (r, s) where r varies from 0 to 6 and s varies from 0 to 7. Start state (as well as accepting state) is (0, 0) and by we can easily give transitions to states by noting that if we input "a" the state (r, s) transitions to (r + 1, s) and on input "b" it transitions to state (r, s + 1).
However it is too difficult to work with a DFA of 48 states and I am not sure if this can be minimized by using the DFA minimization algorithm by hand.
I am really not sure how then we can get to a regular expression representing such strings.
If you are allowed to use lookaheads:
^(?=b*((ab*){6})+$)a*((ba*){8})+$
Debuggex Demo
Example of matched string: bbaabbaabbaabb
Idea is simple: We know how to match string having number of as divisible by 6 - ^((b*ab*){6})+$, also we know how to match string having number of bs divisible by 8 - ^((a*ba*){8})+$. So we just put one regex to lookahead, and another one to matching part.
In case if you also need to match strings consisting only of as or only of bs, then the following regex will do:
^(?=b*((ab*){6})*$)a*((ba*){8})*$
Examples of matched strings: aaaaaa, bbbbbbbb, bbaabbaabbaabb
Debuggex Demo

Adding one digit (0-9) to the sequence/string creates new 4 digits number

I'm trying to find an algorithm which "breaks the safe" by typing the keys 0-9. The code is 4 digits long. The safe will be open where it identifies the code as substring of the typing. meaning, if the code is "3456" so the next typing will open the safe: "123456". (It just means that the safe is not restarting every 4 keys input).
Is there an algorithm which every time it add one digit to the sequence, it creates new 4 digits number (new combinations of the last 4 digits of the sequence\string)?
thanks, km.
Editing (I post it years ago):
The question is how to make sure that every time I set an input (one digit) to the safe, I generate a new 4 digit code that was not generated before. For example, if the safe gets binary code with 3 digits long then this should be my input sequence:
0001011100
Because for every input I get a new code (3 digit long) that was not generated before:
000 -> 000
1 -> 001
0 -> 010
1 -> 101
1 -> 011
1 -> 111
0 -> 110
0 -> 100
I found a reduction to your problem:
Lets define directed graph G = (V,E) in the following way:
V = {all possible combinations of the code}.
E = {< u,v > | v can be obtained from u by adding 1 digit (at the end), and delete the first digit}.
|V| = 10^4.
Din and Dout of every vertex equal to 10 => |E| = 10^5.
You need to prove that there is Hamilton cycle in G - if you do, you can prove the existence of a solution.
EDIT1:
The algorithm:
Construct directed graph G as mentioned above.
Calculate Hamilton cycle - {v1,v2,..,vn-1,v1}.
Press every number in v1.
X <- v1.
while the safe isn't open:
5.1 X <- next vertex in the Hamilton path after X.
5.2 press the last digit in X.
We can see that because we use Hamilton cycle, we never repeat the same substring. (The last 4 presses).
EDIT2:
Of course Hamilton path is sufficient.
Here in summary is the problem I think you are trying to solve and some explanation on how i might approach solving it. http://www.cs.swan.ac.uk/~csharold/cpp/SPAEcpp.pdf
You have to do some finessing to make it fit into the chinese post man problem however...
Imagine solving this problem for the binary digits, three digits strings. Assume you have the first two digits, and ask your self what are my options to move to? (In regards to the next two digit string?)
You are left with a Directed Graph.
/-\
/ V
\- 00 ----> 01
^ / ^|
\/ ||
/\ ||
V \ |V
/-- 11 ---> 10
\ ^
\-/
Solve the Chinese Postman, you will have all combinations and will form one string
The question is now, is the Chinese postman solvable? There are algorithms which determine weather or not a DAG is solvable for the CPP, but i don't know if this particular graph is necessarily solvable based on the problem alone. That would be a good thing to determine. You do however know you could find out algorithmically weather it is solvable and if it is you could solve it using algorithms available in that paper (I think) and online.
Every vertex here has 2 incoming edges and 2 outgoing edges.
There are 4 (2^2) vertexes.
In the full sized problem there are 19683( 3 ^ 9 ) vertexs and every vertex has 512 ( 2 ^ 9 ) out going and incoming vertexes. There would be a total of
19683( 3 ^ 9 ) x 512 (2 ^ 9) = 10077696 edges in your graph.
Approach to solution:
1.) Create list of all 3 digit numbers 000 to 999.
2.) Create edges for all numbers such that last two digits of first number match first
two digits of next number.
ie 123 -> 238 (valid edge) 123 -> 128 (invalid edge)
3.) use Chinese Postman solving algorithmic approaches to discover if solvable and
solve
I would create an array of subsequences which needs to be updates upon any insertion of a new digit. So in your example, it will start as:
array = {1}
then
array = {1,2,12}
then
array = {1,2,12,3,13,23,123}
then
array = {1,2,12,3,13,23,123,4,14,24,124,34,134,234,1234}
and when you have a sequence that is already at the length=4 you don't need to continue the concatenation, just remove the 1st digit of the sequence and insert the new digit at the end, for example, use the last item 1234, when we add 5 it will become 2345 as follows:
array = {1,2,12,3,13,23,123,4,14,24,124,34,134,234,1234,5,15,25,125,35,135,235,1235,45,145,245,1245,345,1345,2345,2345}
I believe that this is not a very complicated way of going over all the sub-sequences of a given sequence.

What is meant by adding a column in bits?

I am not able to understand what is meant by the following text, which I referred from a book:
Consider the four two-bit numbers
00, 01, 10, 11. If you add up
the one’s bit, you get an even number.
Likewise, if you add up two’s bit, you
get an even number. No matter how many
bits in the number, if you add up a
column, you get an even number.
Specifically, what is meant by "add up one's bit" for 00?
They just mean that if you write the four numbers in a column:
00
01
10
11
...and you look at how many bits in the first column (the "one's bits") are 1, you get an even number. Similarly for the second column (the "two's bits").
Their claim is that no matter how many bits the number has, if you write down all numbers with that many bits, the number of 1's in each column will be even.
Their claim is false for one-bit numbers. In general, for n bits, the number of 1s in each column will (obviously) be 2^(n-1), which is even except when n=1.
What book is this? What point are they trying to make?
The bits in a binary number are generally "named" column-wise according to their respective power of two:
00000000
│││││││└── 1's bit
││││││└─── 2's bit
│││││└──── 4's bit
││││└───── 8's bit
│││└────── 16's bit
││└─────── 32's bit
│└──────── 64's bit
└───────── 128's bit
The "one's" bit is the bit that represents a "one", i.e. the rightmost bit. The "two's" bit is the bit used to hold a 2, i.e. the bit second to the right. The next bit to the left of the "two's" bit is the "four", etc.
001 = 1
010 = 2
100 = 4
Thinking back to the way numbers in decimal numbers. For example: 184. Starting at the right-most number, we have 4, it's equivilent to saying "there are 4 ones in this numbers." It's in the ones place, as we progress left we have the 8 in the tens place (meaning to represent that there are 8 tens) and 1 in the hundreds place (only 1 hundred). For a binary number like 10 (binary for 2), the 0 in the right-most place is in the ones place, that "column" (position from the right hand side) is the place that denotes how many 1's are in that number. Along the same lines, the 1 is in the twos place, denoting how many twos are in this number.
i think what it is trying to say is that if you take all the numbers with a certain number of bits - in this example, 2 then add the values of each column, the result will be even.
so, for the 4 2 bit numbers, add each column separately:
0 0
0 1
1 0
+1 +1
- -
2 2
each column adds to 2 - an even number
similarly, for all the 3 bit numbers:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
+1 +1 +1
- - -
4 4 4
each column adds to 4 - even

Regexes for integer constants and for binary numbers

I have tried 2 questions, could you tell me whether I am right or not?
Regular expression of nonnegative integer constants in C, where numbers beginning with 0 are octal constants and other numbers are decimal constants.
I tried 0([1-7][0-7]*)?|[1-9][0-9]*, is it right? And what string could I match? Do you think 034567 will match and 000083 match?
What is a regular expression for binary numbers x such that hx + ix = jx?
I tried (0|1){32}|1|(10)).. do you think a string like 10 will match and 11 won’t match?
Please tell me whether I am right or not.
You can always use http://www.spaweditor.com/scripts/regex/ for a quick test on whether a particular regex works as you intend it to. This along with google can help you nail the regex you want.
0([1-7][0-7])?|[1-9][0-9] is wrong because there's no repetition - it will only match 1 or 2-character strings. What you need is something like 0[0-7]*|[1-9][0-9]*, though that doesn't take hexadecimal into account (as per spec).
This one is not clear. Could you rephrase that or give some more examples?
Your regex for integer constants will not match base-10 numbers longer than two digits and octal numbers longer than three digits (2 if you don't count the leading zero). Since this is a homework, I leave it up to you to figure out what's wrong with it.
Hint: Google for "regular expression repetition quantifiers".
Question 1:
Octal numbers:
A string that start with a [0] , then can be followed by any digit 1, 2, .. 7 [1-7](assuming no leading zeroes) but can also contain zeroes after the first actual digit, so [0-7]* (* is for repetition, zero or more times).
So we get the following RegEx for this part: 0 [1-7][0-7]*
Decimal numbers:
Decimal numbers must not have a leading zero, hence start with all digits from 1 to 9 [1-9], but zeroes are allowed in all other positions as well hence we need to concatenate [0-9]*
So we get the following RegEx for this part: [1-9][0-9]*
Since we have two options (octal and decimal numbers) and either one is possible we can use the Alternation property '|' :
L = 0[1-7][0-7]* | [1-9][0-9]*
Question 2:
Quickly looking at Fermat's Last Theorem:
In number theory, Fermat's Last Theorem (sometimes called Fermat's conjecture, especially in older texts) states that no three positive integers a, b, and c can satisfy the equation an + bn = cn for any integer value of n greater than two.
(http://en.wikipedia.org/wiki/Fermat%27s_Last_Theorem)
Hence the following sets where n<=2 satisfy the equation: {0,1,2}base10 = {0,1,10}base2
If any of those elements satisfy the equation, we use the Alternation | (or)
So the regular expression can be: L = 0 | 1 | 10 but can also be L = 00 | 01 | 10 or even be L = 0 | 1 | 10 | 00 | 01
Or can be generalized into:
{0} we can have infinite number of zeroes: 0*
{1} we can have infinite number of zeroes followed by a 1: 0*1
{10} we can have infinite number of zeroes followed by 10: 0*10
So L = 0* | 0*1 | 0*10
max answered the first question.
the second appears to be the unsolvable diophantine equation of fermat's last theorem. if h,i,j are non-zero integers, x can only be 1 or 2, so you're looking for
^0*10?$
does that help?
There are several tool available to test regular expressions, such as The Regulator.
If you search for "regular expression test" you will find numerous links to online testers.