## Can unary be used in Turing Machine? - turing-machines

Given a number in unary (0 = 1, 1 = 11, 2 = 111, 3 = 1111, ...), leave one blank symbol after it, and write the binary representation of the same number (0 = 0, 1 = 1, 2 = 10, 3 = 11, 4 = 100, ...). It is acceptable (not required) to write the number in a reverse order. When done, the TM should switch to the accepting state. No need to verify input, assume that input is 100% one unary number, and there is nothing else written on the tape.

Here is a solution based on Welbog's hint.
The TM will begin by writing a 0 one blank space after the end of the 1s. We know there will be at least a single 1 on the tape. Then, we can add one to our binary representation for each 1 we see to the left of the first blank. We may as well remember which unary 1s we've already processed by changing them to 0s. If we want to put the tape back like it was when we're done, we can write 1s back over the 0s to the left of the binary representation.
Q T Q' T' d
-----------------------
q0 1 q0 1 R // scan right to first blank space
q0 # q1 # R // after unary. then, write a 0
q1 # q2 0 L // to start the binary.
q2 # q3 # L // scan left past any binary data
q2 0 q2 0 L // to get to the blank separating
q2 1 q2 1 L // unary and binary
q3 # hA # R // scan left for another unary
q3 0 q3 0 L // digit, ignoring ones that have
q3 1 q4 0 R // been processed. if done, halt.
q4 # q5 # R // scan right to the blank separating
q4 0 q4 0 R // unary and binary
q5 # q2 1 L // add one to the binary representation
q5 0 q2 1 L // by toggling bits until you toggle a
q5 1 q5 0 R // zero to a one, completing the addition
Example:
#111#### => #111#### => #111#### => #111#### => (next line)
^q0 ^q0 ^q0 ^q0
#111#### => #111#0## => #111#0## => #110#0## => (next line)
^q1 ^q2 ^q3 ^q4
#110#0## => #110#1## => #110#1## => #110#1## => (next line)
^q5 ^q2 ^q3 ^q3
#100#1## => #100#1## => #100#1## => #100#0## => (next line)
^q4 ^q4 ^q5 ^q5
#100#01# => #100#01# => #100#01# => #100#01# => (next line)
^q2 ^q2 ^q3 ^q3
#100#01# => #000#01# => #000#01# => #000#01# => (next line)
^q3 ^q4 ^q4 ^q4
#000#01# => #000#11# => #000#11# => #000#11# => (next line)
^q5 ^q2 ^q3 ^q3
#000#11# => #000#11# => #000#11#
^q3 ^q3 ^hA

## Related

### If I XOR 2 numbers, do I only get identical results if the numbers are the same?

For example, suppose I have x XOR y = y XOR x = z. Is it possible to have something like a XOR b = z?

Yes. z = y because x ^ y ^ x = y So it is entirely possible for a combination a ^ b = y = z. In fact, for every a there exists a b such that a ^ b = z. To calculate that, b = z ^ a. Be aware that XOR is commutative: this means that x ^ y = y ^ x.

Short answer: Yes Long answer: XOR is a binary operation, it works on the individual bits and it's commutative. It has the truth table: A B Q 0 0 0 0 1 1 1 0 1 1 1 0 As the number is made up of these bits then the result will be the same as long as for each bit position the two bits have the same result. For example take the 2 eight bit numbers 113 and 42 113 = 01110001 42 = 00101010 XOR = 01011011 = 91 but if I swap the fourth bit from the left I get 97 = 01100001 58 = 00111010 XOR = 01011011 = 91 So yes again...

Yes. As a degenerate proof, XORing a number with itself always results in 0.

XOR, will return true if both parameters are different, assuming that the parameters are Boolean values anyway. This is different from or, which will return true if either parameter is true, and NOR, which will return true only if both of them are false.

### Haskell case statements with ord 'x'

import Data.Char x1 xs = sum(map (hilfsfunction) xs) hilfsfunction x | (ord 'x' > 97 && ord 'x' < 107) = (ord 'x' - 96) | (ord 'x' > 48 && ord 'x' < 58) = (ord 'x' - 48) | otherwise = 0 I need to write a programm where the function takes a string and then makes the following things with it: - If the letter is an a, change it to 1 - If the letter is something else, I should take the equivalent number for it. - All other symbols should be changed to 0 My problem now is, everytime I launch this programm the sum is 0, no matter if the first case or secound case is true. Example: Lets take "6": *Main Data.Char> ord '6' > 48 && ord '6' < 58 True after this: *Main Data.Char> ord '6' - 48 6 but if I take my function, everytime thats the case....: *Main Data.Char> x1 "6" [0] I dont know where my error is.

You here have hardcoded the 'x' character instead of using the x variable: import Data.Char x1 xs = sum(map (hilfsfunction) xs) hilfsfunction x | (ord 'x' > 97 && ord 'x' < 107) = (ord 'x' - 96) | (ord 'x' > 48 && ord 'x' < 58) = (ord 'x' - 48) | otherwise = 0 So that means Haskell does not care what the value of x is: it will simply use the 'x'. Note that here you write a rather cryptic function, you can write it in a cleaner way by making comparisons with other characters, and use ord when you do arithmetic: hilfsfunction :: Char -> Int hilfsfunction x | 'a' <= x && x <= 'z' = ox - ord 'a' + 1 | '0' <= x && x <= '9' = ox - ord '0' | otherwise = 0 where ox = ord x Here it is clear at first sight (without any knowledge of the outline of ASCII) that you first check if x is in the range ['a'..'z'], and if that is the case you basically use its sequence number. If not that you check if it is in the range ['0' .. '9'] and then use its numerical counterpart, and otherwise you return 0. In fact you made a mistake in your implementation, since ord 'z' is 122, and not 107. Although you can use 122, by comparing against 'z', it is probably less likely to make mistakes, since you let Haskell translate characters to its ASCII code equivalent. With the new definition for the hilsfsfunction, we obtain: Prelude Data.Char> x1 "6aa#a1a~dwfwf123" 79

There is a loss of information in the sum. First '0' is not included. Second, the range of ['1'..'9'] and ['a'..'i'] is the very same in the sum. Also, if I were going to write this, I would import Data.Char (ord) sol x | elem x ['0'..'9'] = mod (ord x) 16 | elem x ['a'..'z'] = mod (ord x) 32 | elem x ['A'..'Z'] = mod (ord x) 64 | otherwise = 0 With this, the preceding facts can be demonstrated sol 'd' produces 4 sol '4' produces 4 sol '#' produces 0 sol '0' produces 0 Now that said, ord is an index into the ASCII character set. It is possible to create you own index with just the characters that interest you. tlz = zip [1..] $ concat [['0'..'9'],['a'..'z']] Then you can lookup characters and each will have a unique index number, even zero '0' will register as one. If a character is not in the table it, simply is not counted. The list compression just emits a null []. Create the following function to use the list. The output is larger than the original function, the length of tls is 36 and originally the length is 26. smmat = sum.concat.map (\x->[a|(a,b)<-tlz,x == b]) Just feed it a list. smmat "This is 42% sure" 226 Be assured in the preceding list there were 11 "hits". take sum out of the list to see or changer a in the list comprehension to b to see the characters.

### Writing power function in Standard ML with a predefined compound function

Having trouble writing a power function inStandard Ml. Im trying to write a function called exp of type int -> int -> int. The application exp b e, for non-negative e, should return b^e. For example, exp 3 2 should return 9. exp must be implemented with the function compound provided below. exp should not directly calls itself. Here is the compound function, it takes in a value n, a function, and a value x. All it does is it applies the function to the value x n number of times. fun compound 0 f x = x | compound n f x = compound (n-1) f (f x); Im having trouble figuring out how to write this function without recursion, and with the restraint of having to use a function that only can use a function with one parameter. Anyone have any ideas of where to start with this? This is what I have: fun exp b 0 = 1 | exp b e = (compound e (fn x => x*x) b) I know that this doesn't work, since if i put in 2^5 it will do: 2*2, 4*4, 16*16 etc.

You could just do this instead I believe fun exp 0 0 = 1 | exp b 0 = 1 | exp b e = (compound (e - 1) (fn x => b * x ) b);

You are extremely close. Your definition of exp compounds fn x => x*x which (as you noticed) is not what you want, because it is repeatedly squaring the input. Instead, you want to do repeated multiplication by the base. That is, fn x => b*x. Next, you can actually remove the special case of e = 0 by relying upon the fact that compound "does the right thing" when asked to apply a function 0 times. fun exp b e = compound e (fn x => b*x) 1

this may not be exactly 100% proper code. I sort of just now read a bit of Standard ML documentation and took some code and reworked it for your example but the general idea is the same for most programming languages. fun foo (num, power) = let val counter = ref power val total = 1 in while !counter > 0 do ( total := !total * num counter := !counter - 1 ) end; To be more clear with some pseudo-code: input x, pow total = 1 loop from 1 to pow total = total * x end loop return total This doesn't handle negative exponents but it should get you started. It basically is a simple algorithm of what exponents truly are: repeated multiplication. 2^4 = 1*2*2*2*2 //The 1 is implicit 2^0 = 1

### Range data type or generator in Emacs Lisp (elisp)?

What is elisp's equivalent for Python's range(start, end, [step])?

number-sequence is similar to python's range but its output is quite different. For example: (number-sequence 5) => (5) (number-sequence 1 5) => (1 2 3 4 5) (number-sequence 1 5 2) => (1 3 5) I use this function to give me an output like that from python's range: (defun py-range (start &optional end step) (unless end (setq end start start 0)) (number-sequence start (1- end) step)) Now everything works as expected: (py-range 5) => (0 1 2 3 4) (py-range 1 5) => (1 2 3 4) (py-range 1 5 2) => (1 3)

(number-sequence FROM &optional TO INC)

### Bug when solving CDN allocation rule

I have a problem with my prescription of a mathematical problem to AMPL. I trying to solve this problem: In a network with a set of nodes N and a set of edges E each node has storage to cache content. There is a set O of content objects that are accesses by the clients and can be cached if needed. Let the size of o ∈ O be equal to h_o storage units. Assume each node n ∈ N has clients that request object o ∈ O so that the traffic towards n to download o equals d_n;o. In a managed content delivery network (CDN), the CDN operator can adopt various policies to allocate content copies among the caches. These policies may depend on many factors that can be technical or business in nature, which gives rise to dierent optimisation problems. Let h_max be the maximum total storage that can be used by the CDN (i.e., the sum of storage used bythe CDN over all nodes). Find: the allocation of copies of each object o ∈ O such that the limit on the total storage used by CDN is satised while minimising the overall trac in the network (i.e., the routing cost from the caches to client nodes) Ampl Files: #Model for 'CDN allocation copies' problem #sets #------------------------------------------------------------------------------------- set K; #index of nodes with group of clients set N; #nodes set E; #edges set O; #objects #parameters #------------------------------------------------------------------------------------- param d {K,O}; #demands for object o param t {K,O} symbolic; #destination nodes param r {N,K} binary; #1 if node n is ancestor of node k, 0 otherwise param a {N,E} binary; #1 if edge begins in vertex, 0 otherwise param b {N,E} binary; #1 if edge ends in vertex, 0 otherwise param c {E}; #cost of using an edge param Hmax; #available capacity for allocation object in proxy servers #variables #------------------------------------------------------------------------------------- var f {N,O} binary; #1 if object saved at node k, 0 otherwise var x {E,K,O}; #value of the demand realised over edge for object #goal function #------------------------------------------------------------------------------------- #The function minimizes cost of routing #By saving copies at CDN proxies we minimizing all traffic from all demands #with all objects minimize goal: sum{e in E} sum{k in K} sum{o in O} (x[e,k,o]*c[e]); #constraints #------------------------------------------------------------------------------------- subject to c0 {e in E, k in K, o in O}: x[e,k,o]>=0; subject to c1a {k in K, o in O, n in N: n!=t[k,o]}: (r[n,k]==1 and f[n,o]==1) ==> sum{e in E} (a[n,e]*x[e,k,o]) - sum{e in E} (b[n,e]*x[e,k,o]) = d[k,o]*(1-f[k,o]) else sum{e in E} (a[n,e]*x[e,k,o]) - sum{e in E} (b[n,e]*x[e,k,o]) = 0; subject to c1c {k in K, o in O, n in N: n==t[k,o]}: sum{e in E} (a[n,e]*x[e,k,o]) - sum{e in E} (b[n,e]*x[e,k,o]) = -d[k,o]*(1-f[k,o]); subject to c2: sum{k in K} sum{o in O} f[k,o] <= Hmax; subject to c3 {k in K, o in O}: sum{n in N} r[n,k]*f[n,o] <= 2; subject to c4 {o in O}: f[1,o]=1; And my data file: #Data file for 'CDN allocation copies' problem simple example #indices set K := 2 3; #index of nodes with group of clients set N := 1 2 3; #nodes set E := 1_2 1_3; #edges set O := o1 o2 o3 o4 o5 o6 o7 o8 o9 o10; #objects #parameters param d (tr): #demands for object o 2 3 := o1 2560 512 o2 1280 256 o3 640 128 o4 320 64 o5 160 32 o6 80 16 o7 40 8 o8 20 4 o9 10 2 o10 5 1; #opt= 63 + 75 = 138 param t (tr): #destination nodes 2 3 := o1 2 3 o2 2 3 o3 2 3 o4 2 3 o5 2 3 o6 2 3 o7 2 3 o8 2 3 o9 2 3 o10 2 3; param r (tr): #1 if node n is ancestor of node k, 0 otherwise 1 2 3 := 2 1 0 0 3 1 0 0; param a (tr): #1 if edge begins in vertex, 0 otherwise 1 2 3 := 1_2 1 0 0 1_3 1 0 0; param b (tr): #1 if edge ends in vertex, 0 otherwise 1 2 3 := 1_2 0 1 0 1_3 0 0 1; param c := #cost of using an edge 1_2 1 1_3 1; param Hmax := 10; #available capacity for allocation object in proxy servers When I try to solve my problem i see this bug: Error at _cmdno 15 executing "let" command (file C:\Program Files\AMPLDevX64 Evaluation\plugins\com.ampldev_2.3.0.201211162252 \include/writesol.ampl, line 22, offset 783): Can't evaluate _con[92]: subscript not in 1 .. 91

The error was caused by AMPL incorrectly including the number of logical constraints in _ncons. It is fixed in AMPL version 20130510 (see http://www.netlib.org/ampl/changes). The logical constraint in your model is the indicator constraint c1a.