1

bin is shortest number in binary

Is bin(n)bin(2^(k+1) * n + 1)^R context free?

k,n belongs to natural numbers.

I know bin(n)bin(n + 1)^R is context free but I don't have idea how to solve bin(n)bin(2^(k+1) * n + 1)^R . If is context-free can someone help me build context-free grammar?

PoliteMan
  • 43
  • 4

2 Answers2

1

Assuming x^R means x reversed, then you're looking for strings in the form

n1(many zeros)(n)^R

Since "many zeros" in this case is just 0*, a regular expression, you can adapt whatever grammar you have for n(n+1)^R into this language and it will still be context free.

Let's look at n=5, k=2

n = 101
2^(k+1) = 2^3 = 1000
1000 * 101 is 101000
101000 + 1 is 101001
101001^R is 100101

The final string is

n1(zeroes)n^R
101100101
Welbog
  • 59,154
  • 9
  • 110
  • 123
  • how will be look grammar context-free look like? – PoliteMan Feb 11 '19 at 18:44
  • As this looks like a homework question, I will answer vaguely. The grammar will look very similar to grammars that match the language `ww^R`, except with `10*` between `w` and `w^R`. – Welbog Feb 11 '19 at 18:46
  • add C-> C0 | 1 | to grammar from below website. Will be good? It isn't homework. https://cs.stackexchange.com/questions/75890/context-free-grammar-for-binnbinn1r – PoliteMan Feb 11 '19 at 18:56
0

The question is whether the language bin(n)bin(2^(k+1) * n + 1)^R is context-free. I take bin(n) to mean the binary representation of the natural number n without any leading zeroes.

Suppose bin(n') = x. Here, x is a finite string of binary digits beginning with 1. Let us determine what bin(2^(k+1) * n + 1) looks like. First, note that multiplying a number by two adds a zero to the end of that number's binary representation; just the same as multiplying by ten when using decimal. Multiplying by 2^(k+1) will add k+1 zeroes. Because k is a natural number, at least one zero must be added. Adding one to this number will flip the least significant bit over to 1 from 0. The end result is that bin(2^(k+1) * n + 1) = x(0^k)1.

The language bin(n)bin(2^(k+1) * n + 1)^R consists of strings of the form x(x(0^k)1)^R. We can distribute the ^R by reversing each of the concatenated substrings and the order of concatenation to see that these strings are of the form x1(0^k)(x^R). We notice the outermost component of these strings begin with an arbitrary binary string x and end with x^R; we can handle this with a context-free grammar in the same we can handle languages of palindromes. The innermost component is 1(0^k), which describes the regular language 10*; we can certainly handle that in a CFG. A CFG that works is the following:

S := 0S0 | 1S1 | T
T := T0 | 1

The main insight in deriving this is in determining the form of (bin(2^(k+1) * x + 1)^R.

Patrick87
  • 27,682
  • 3
  • 38
  • 73