## Is this language decidable? Set of turing machines where their language is a superset of some finite set - turing-machines

### Why pumping lemma for CFG doesn't work

```Language:
{(a^i)(b^j)(c^k)(d^l) : i = 0 or j = k = l}
We take word
w = a^0 b^n c^n d^n
Which obviously belongs to the language because j = k = l
w = uvxyz
|vxy| <= n
|vy| > 1
and now v and y can be:
just a single character and if we pump single character the word is no longer in the language
two characters, count of the third will be lower so the word is not in the language
So, the proof that this language is not CF is not supposed to be do-able with standard pumping lemma, just with the ogdens lemma, but I don't understand why the proof above is invalid.
```
```It doesn't work because in fact every pumped string is in the language, because you still have no as (that is, i=0).
And if you choose a string where i > 0, then you can't guarantee that v isn't just some number of as, and x is the empty string.```

### How can we prove this using proof by induction?

```Let G be the following grammar:
S → T⊣
T → TaTb | TbTa | ε
Show that L(G) = {w⊣| w contains equal numbers of a’s and b’s} using proof by induction on the length of w.
What can be assumed in this situation?
```
```For simplicity we can ignore S symbol and just prove that T produces equal number of a's and b's.
Assuming L={w| w contains equal numbers of a’s and b’s}, the proof is comprised of two parts:
1-Every string with length n that T produces, is in L.
2-Every string in L with length n can be produced by T.
1)
The proof of 1 is simple by induction. The rule (T → ε) produces equal No. of a's and b's, and by induction the rules T → TaTb | TbTa also keeps a's and b's equal.
2)
We assert that every string in L with length n which ends with b, can be produced by first using the rule T → TaTb.
The proof can be established by numbering the letters. we give each 'a' +1 and each 'b' -1. Every string in L has total rank of '0' (due to equal No of a's and b's). Every string in L ending with b, starts with rank 0 and reaches rank +1 just before last b. It may reaches rank 0 again in between before seeing an 'a'. This is where we can rewrite the string w as T1aT2b in which T1 and T2 are in L too and by induction can be produced by T.(If the rank of w never reaches 0 in between it means that w starts with a, so T1=ε)
The proof for strings ending with 'a' is similar.```

### Significant digits using a logarithm and a floating point number

```Related to this question: Printf width specifier to maintain precision of floating-point value.
I want to print the significant digits of a number n represented using a double d as n = 2^d.
Is there a function of d which computes the significant digits of n? A maximum number (larger than 0) for the significant digits is also fine, like DBL_DIG in float.h.
Edit:
I will be doing many additions to d such that 2^d >> MAX_DOUBLE. I suspect therefore the number of significant digits in n will be less than the significant digits of d. Is this true? By addition I mean (hopefully this makes sense):
Let n = n1 + n2, n1 = 2^d1, n2 = 2^d2 then I will perform d = d1 + d2, such that n = 2^d.
The reason I am using this is because it is nearly impossible to let d overflow and I am fine with loosing lots of precision in n.
I will be using a function described here: http://buddy.sourceforge.net/manual/group__info_ge551a6cc544c7d50a3d7c7fb7f5f9374.html#ge551a6cc544c7d50a3d7c7fb7f5f9374.```

### Non deterministic Turing machine

```I am new to NDTM, but I do understand the concept of a turing machine. when it comes to NDTM I get a little confused, I m supposed to develop a NDTM for language {a,b,c} and
L = {w ∈ Σ*| Ǝv ∈ Σ*, Ǝn >= 2 with w = v (to the power of) n }
First thing that I want to know is how to read L, for example what is the meaning of Ǝ.
I do understand that a NDTM gives twp possibilities of one outcome, like for example for a:
we would have with a and without a if i am correct, Can someone help me fugure this out?
```
```This should be marked as "Homework" I think.
Ǝ is "there exists"
Σ is "the set of symbols in the language" ({a, b,c}) in this case
∈ is "element of"
Now that we have that, we can read this language. So L is the set of words w in {a, b, c}* such that there exists a word v and there exists a n >= 2 such that w is a repetition of v n times. E.g. ababab = (ab)^3 ∈ L.
Now you want to come up with a Turing machine, M, to represent this language, so you have to consider:
When do we reject a word (what is our rejection state, what is on the stack)
When do we accept a word (what is our accepting state, what is on the stack)
How do we guarantee that M terminates.
We can see that a is not in L because n >= 2 which implies that the length of v^n is at least 2 (0 in the case of the empty string though, which is an outlier). Similarly for b and c. With that consideration and the knowledge that n >= 2, figure out what words are not accepted (e.g. consider b, abc, cab, cca, etc.).```

### Integer exponentiation in OCaml

```Is there a function for integer exponentiation in OCaml? ** is only for floats. Although it seems to be mostly accurate, isn't there a possibility of precision errors, something like 2. ** 3. = 8. returning false sometimes? Is there a library function for integer exponentiation? I could write my own, but efficiency concerns come into that, and also I'd be surprised if there isn't such a function already.
```
```Regarding the floating-point part of your question: OCaml calls the underlying system's pow() function. Floating-point exponentiation is a difficult function to implement, but it only needs to be faithful (that is, accurate to one Unit in the Last Place) to make 2. ** 3. = 8. evaluate to true, because 8.0 is the only float within one ULP of the mathematically correct result 8.
All math libraries should(*) be faithful, so you should not have to worry about this particular example. But not all of them actually are, so you are right to worry.
A better reason to worry would be, if you are using 63-bit integers or wider, that the arguments or the result of the exponentiation cannot be represented exactly as OCaml floats (actually IEEE 754 double-precision numbers that cannot represent 9_007_199_254_740_993 or 253 + 1). In this case, floating-point exponentiation is a bad substitute for integer exponentiation, not because of a weakness in a particular implementation, but because it is not designed to represent exactly integers that big.
(*) Another fun reference to read on this subject is “A Logarithm Too Clever by Half” by William Kahan.
```
```Not in the standard library. But you can easily write one yourself (using exponentiation by squaring to be fast), or reuse an extended library that provides this. In Batteries, it is Int.pow.
Below is a proposed implementation:
let rec pow a = function
| 0 -> 1
| 1 -> a
| n ->
let b = pow a (n / 2) in
b * b * (if n mod 2 = 0 then 1 else a)
If there is a risk of overflow because you're manipulating very big numbers, you should probably use a big-integer library such as Zarith, which provides all sorts of exponentiation functions.
(You may need the "modular exponentiation", computing (a^n) mod p; this can be done in a way that avoids overflows by applying the mod in the intermediary computations, for example in the function pow above.)
```
```Here's another implementation which uses exponentiation by squaring (like the one provided by #gasche), but this one is tail-recursive
let is_even n =
n mod 2 = 0
(* https://en.wikipedia.org/wiki/Exponentiation_by_squaring *)
let pow base exponent =
if exponent < 0 then invalid_arg "exponent can not be negative" else
let rec aux accumulator base = function
| 0 -> accumulator
| 1 -> base * accumulator
| e when is_even e -> aux accumulator (base * base) (e / 2)
| e -> aux (base * accumulator) (base * base) ((e - 1) / 2) in
aux 1 base exponent
```
```A simpler formulation of the solution above:
let pow =
let rec pow' a x n =
if n = 0 then a else pow' (a * (if n mod 2 = 0 then 1 else x)) (x * x) (n / 2) in
pow' 1
pow' a x n computes a times x to the n.```