2

Let $\Sigma$ be 0,1,2. I am interested in the language of all strings $w$ such that the sum of $w$ characters (where the char 0 is treated as the number 0, the char 1 as the number 1 etc) is divideable by 5. Obviously this is a regular language, and I can also find a simple linear grammer.

My idea for a regular expression was to check all the combinations where I can receive sum zero (in modulo 5) with the digits 1 and 2 and put 0* in between. But is there no better way? Something more elegant?

Edit: My expression is $0^* 1 0^* 1 0^* 1 0^* 1 0^* 1 0^* \cup 0^* 1 0^* 1 0^* 1 0^* 2 0^* \cup 0^* 1 0^* 1 0^* 2 0^* 1 0^* \cup 0^* 1 0^* 2 0^* 1 0^* 1 0^* \cup \ldots $ and so it goes on for all the possible combination. My question is if there is a way to simplify this - Or maybe a different approach?

Raphael
  • 72,336
  • 29
  • 179
  • 389
  • Could you clarify when is a regular expression 'simple'? We like to be able to evaluate answers objectively, so such a requirement is unfortunately not a good fit unless explicitly defined. It seems you already have a regular expression. Why is this one not 'simple'? – Discrete lizard Mar 13 '18 at 10:15
  • My expression is $0^* 1 0^* 1 0^* 1 0^* 1 0^* 1 0^* \cup 0^* 1 0^* 1 0^* 1 0^* 2 0^* \cup 0^* 1 0^* 1 0^* 2 0^* 1 0^* \cup 0^* 1 0^* 2 0^* 1 0^* 1 0^* \cup \ldots $ and so it goes on for all the possible combination. My question is if there is a way to simplify this - Or maybe a different approach? – Benicio Agüero Mar 13 '18 at 10:38
  • Alright, that sounds specific enough. I suggest you edit your answer so that it contains your expression and then simply ask for a expression that is simpler (or perhaps shorter) than the expression (or part of expression) that you give. – Discrete lizard Mar 13 '18 at 10:40
  • 1
    Related. If that answers your question, I think you can vote to close as a duplicate and keep things tidy. – Peter Taylor Mar 13 '18 at 11:15
  • 1
    The thing you give isn't a regular expression, because it's infinitely long. Regular expressions are finite strings; if you allow them to be infinite, they stop being intesting, because you can specify literally any language (even uncomputable ones) as "string 1 or string 2 or string 3 or..." – David Richerby Mar 13 '18 at 11:32
  • 3
    There probably isn't a nice regular expression for the language you're interested in. There usually isn't, when the property defining the language is "global" in the sense of requiring you to look at the whole string to evaluate it. (Compared to, say, "Strings that don't contain 101", where you only have to check each three-character substring.) – David Richerby Mar 13 '18 at 11:36
  • @Peter Taylor, I will be trying the suggested approaches, but since my automatun consists of 5 states which all depends on one another, I believe this will be less fitting in this case. – Benicio Agüero Mar 13 '18 at 11:37
  • @David Richerby , My suggested expression is not infinite. There is a finite amount of combinations that will sum up to 5 using 0,1,2. – Benicio Agüero Mar 13 '18 at 11:41
  • 2
    I have now realized that I am not tking into account combinations such as: 22222 or 2221111 that result in sum 10. Going on with this means I will have to consider sum 15 and 20 and so on, which will make my expression infinite and thus wrong (and not just very long..) – Benicio Agüero Mar 13 '18 at 11:43
  • s.b.: the example in the link provided by @petertaylor is effectively the language over the alphabet ${1, 2}$ consisting of all strings whose sums are divisible by 0. (Take $a$ as $1$ and $b$ as $2$. Or vice versa.) You seek the language over the alphabet ${0, 1, 2}$ consisting of strings whose sums are divisible by 5. That's going to be bigger (5 states with 3 transitions each instead of 3 states with 2 transitions each) but I fail to see why you would think the problems are qualitatively different. – rici Mar 13 '18 at 15:09
  • Here's another very similar question, although I think the one Peter suggests has more complete answers: https://cs.stackexchange.com/questions/74741/looking-for-an-intuitive-regular-expression-for-w-in-sigma-ast-mid-2/74748 – rici Mar 13 '18 at 15:25

1 Answers1

1

Forget about the $0$, they are boring. Like you said, you can add $0^*$ basically everywhere and you are done.

Now for the $1,2$. Just try to order them in batches that total 5. Then you realised that this might be problematic in case at the count of 4 we read $2$ and overshoot by one. No problem, we remember this, and continue until we reach a proper multiple of five. Note however, that the overshoot just might repeat when we have $212$, or similar.

So here is how the regular expression is constructed: any multiple of five, or when we reach an overshoot, wait until we are back. Repeat.

First some abbreviations:

$E_5= \{11111,1112,1121,1211,2111,221,212,122\}$.

$E_4 = \{1111,112,121,211,22 \}$.

$E_{\mathrm{shoot}}= \{1112,212,122\}$.

Finally here is your expression:

$\Big ( E_5 \cup E_4\cdot 2 \cdot (E_{\mathrm{shoot}})^*\cdot E_4\Big )^* $.

Ah. Add $0^*$, lots of them.

Hendrik Jan
  • 30,578
  • 1
  • 51
  • 105