User blog:LittlePeng9/Random Turing machines

Ikosarakt1 inspired me to get back to TM programming.

These machines are designed to work on this simulator.

Pólya conjecture test
Challenge - prove this machine halts ; Tape consists of three parts - prime, work value and function value ; Pólya conjecture states that so called summatory Liouville function is everywhere positive ; This is equivalent to 'function value is always non-zero' in my machine ; Number of 1's in function space is equal to -L(n)+1 ; These are setup states. They are here to make tape ready to work 0 _ 1 l 1 1 _ _ l 2 2 _ 1 l 3 3 _ 1 l 4 4 _ 1 l 5 5 _ _ l 6 6 _ 1 l 7 7 _ 1 r x0 ; This is 'get to work' state. It simply moves from leftmost symbol to work space x0 1 1 r x0 x0 x _ r x0 x0 _ _ l w1 ; These are working states. They check if work value is divisible by prime ; w states mark p symbols in work value (where p is value of prime) w1 1 x r w2 w1 x x l w1 w3 1 x l w0 w2 _ _ r w3 w3 _ _ l s0 ; If p doesn't divide work value, it skips to s w0 _ _ l w1 w0 * * l w0 w2 * * r w2 w3 * * r w3 w1 _ _ r c0 ; If we finished marking p, we check if it's end of work value c0 _ _ r c1 c0 * * r c0 c0 x 1 r c0 c1 _ p l d0 ; if it does, skip to d c1 p _ l d0 c1 1 1 l c2 c2 x y l w0 ; If it isn't, return to w c1 * * r c1 ; Division - we divide work value by p and we leave 'p' on tape, marking prime factor d0 x y l d1 d0 y y l d1 d1 x y r d2 d1 y y l d1 d2 p p l d3 d2 _ _ l d3 d2 * * r d2 d3 y _ l d0 d1 _ _ r d4 d4 y 1 r d4 d4 _ _ l d5 d5 1 1 l d6 d6 1 1 l w0 d6 _ _ r a0 ; This simply increments prime s0 x 1 l s0 s0 y 1 l s0 s0 _ _ l s1 s1 x 1 l s1 s1 1 1 l s1 s1 _ 1 r x0 ; Crucial part - depending on number of prime factors we increment or decrement function value ; It also sets work value to next number a0 * * r a1 a1 _ 1 r a1 a1 p 1 r a2 a2 _ 1 r a2 a2 p 1 r a1 a1 1 _ r a3 a3 _ _ r halt ; If function value reaches 0, Pólya conjecture is false, and machine halts a3 1 1 l r0 a2 1 _ r a4 a4 1 1 r a4 a4 _ 1 r a5 a5 _ 1 l r0 ; Return - after changing function value it starts dividing new number r0 1 1 l r0 r0 _ _ l r1 r1 1 1 l r1 r1 _ _ l r2 r2 1 1 l r3 r3 1 1 l r4 r4 1 x l r4 r4 _ _ r x0

Restricted Knuth up-arrow notation
Example input - x^^^x^^^^xx It corresponds to \(2\uparrow 2 \uparrow \uparrow 3\)

Question - how many states we need to add in order to compute Ackermann function? I found 7 is enough ; This machine computes restricted Knuth up-arrow notation ; It is able to compute 2^(a)2^(b)...2^(n)x, where ^(n) means n up-arrows and x is natural ; Setting input - one up arrow means addition, two mean multiplication, three exponentiation etc. ; Every '2' in notation is represented by ONE 'x'. Rightmost number must have one less 'x' than it represents. 0 * * r 0 ; State 0 simply moves to rightmost non-blank cell 0 _ _ l 1 ; State 1 searches for rightmost arrow 1 x x l 1 1 ^ x l 1a 1 _ _ r halt ; If there are no arrows, halt 1a x x r 1 ; If there is only one arrow, add two numbers and search for new arrow 1a ^ ^ r 1b 1b * * r 2 2 x _ l 3 ; Otherwise, we replace 2^(n)x with 2^(n-1)2^(n)x-1 3 x x l 3 3 ^ y r 4 4 * * r 4 4 x x r 5 5 ^ ^ r 5 5 _ ^ r 6 6 x _ r 7 6 _ _ l 11 ; At this point I use error which happens when last number is 2 7 x x r 7 7 _ x l 8 8 * * l 8 8 y y l 9 9 y y l 9 9 ^ y r 4 9 x x r 10 10 y ^ r 10 10 _ ^ r 0 10 * * r 10 11 x _ l 12 ; If last number is 2, no matter how many arrows there are we collapse to 4 11 ^ _ l 11 12 * _ l 12 12 x x r 13 13 _ x r 14 14 _ x l 1

Two color restricted Knuth up-arrow notation
Same rules as above - strings of x's and ^'s replace with strings of 1's with blanks inbetween. Output isn't decreased by 1 in here

Example input - 1 111 1 1111 11 It corresponds to \(2\uparrow 2 \uparrow \uparrow 3\) 0 1 1 r 0 0 _ _ r 1 1 1 1 r 0 1 _ _ l 2 2 _ _ l 2 2 1 1 l 3 3 _ 1 r c0 3 1 1 l 4 c0 1 _ l c0 c0 _ 1 r c1 c1 _ 1 r 1 4 1 1 l 4 4 _ 1 l 5a 5a _ _ r halt 5a 1 _ l 5b 5b 1 1 r 5c 5b _ 1 r a0 5c _ _ r 6 6 1 _ r 7 7 1 _ l 8 8 _ _ l 9 9 _ _ l 9 9 1 _ r 10 10 _ 1 r 11 11 1 1 r 11 11 _ _ r 12 12 1 1 r 12 12 _ 1 r 13 13 1 _ r 14 13 _ _ r 13 14 1 1 r 14 14 _ 1 l 15 15 1 1 l 15 15 _ _ l 16 16 1 1 l 16 16 _ _ l 17 17 1 1 l 17 17 _ _ l 18 18 _ _ r x0 18 1 1 r 9 x0 _ 1 r x1 x1 1 1 r x1 x1 _ 1 l x2 x2 1 _ r x3 x3 1 1 r x4 x4 1 _ r x5 x5 1 1 r x5 x5 _ 1 r x6 x6 1 1 r x7 x7 1 _ r x8 x7 _ _ r x9 x8 _ 1 r x9 x8 1 1 r x8 x9 _ 1 r 2 a0 _ 1 r a1 a1 1 1 r a1 a1 _ _ l a2 a2 1 _ l a3 a3 1 _ l 2

Two color Collatz conjecture tester
Works for one given number only ; This machine computes so called hailstone sequence for a number written in unary ; This machine is not checking Collatz conjecture. It tests it for only one given number ; First, machine checks if number is even or odd 0 1 1 r 1 1 1 1 r 0 0 _ _ l 00 1 _ _ l 10 ; If number is even, we divide it by 2 00 1 _ l 01 00 _ _ l r 01 1 _ l 02 02 1 1 l 02 02 _ _ l 03 03 1 1 l 03 03 _ 1 r 04 04 1 1 r 04 04 _ _ r 0 r 1 1 l r r _ _ r 0 ; If it is odd, we first check if it is 1 10 1 _ l x x _ _ l y y _ _ r halt ; If it isn't, we replace n with 3*n+1 10 _ 1 l r x 1 1 l 11 y 1 1 l 12 11 1 1 l 11 11 _ _ l 12 12 _ 1 l 13 12 1 1 l 12 13 _ 1 l 14 14 _ 1 r 15 15 1 1 r 15 15 _ _ r 16 16 _ _ l 10 16 1 1 r 16

Square root calculator
For every natural number it returns \(\lfloor \sqrt{n} \rfloor \) ; For every nonnegative integer, machine computes floor(sqrt(n)) ; It uses fact that n^2=1+3+5+...+(2n-1) ; Short setup ; On the left there is current addend, now it is 1 0 1 1 l 0 0 _ _ l 1 1 _ 1 r 5 ; We mark exactly that many 1's from input as there is in addend 2 _ 1 l 6 2 x x l 2 2 1 x r 3 3 x x r 3 3 _ _ r 4 4 x x r 4 4 1 x l 5 4 _ _ l 8 5 x x l 5 5 _ _ l 2 ; If we used whole addend, we increase it by 2 6 _ 1 r 7 7 1 1 r 7 7 x 1 r 7 7 _ _ l 2 ; If we marked whole input, we remove it, leaving only output 8 x _ l 8 8 _ 1 l 9 ; At this moment, addend is exactly 2n+1, where n is wanted output 9 _ _ l 9 9 x _ l 10 9 1 _ l 10 10 x _ r 11 10 1 _ r 11 10 _ _ r 14 11 _ _ r 11 11 1 1 r 12 12 1 1 r 12 12 _ 1 l 13 13 1 1 l 13 13 _ _ l 9 ; Now there is n+1 left on tape. We remove one 1 and we're done 14 _ _ r 14 14 1 _ r halt

Two color square root calculator
Works same way as previous machine, expect for marking. It uses single blank space as end of marked part. Suprisingly, time complexity haven't increased much.

Challenge: I didn't bother to make it work for all integers. Change it. It should be possible with 5 new states 0 1 1 l 1 0 _ _ l halt 1 _ _ l 2 2 _ 1 l 3 3 * _ l 4 4 * 1 l 5 5 * _ l 6 6 * 1 r 7 7 * _ r 8 8 1 1 l 8 8 _ 1 l 9 9 1 _ r 10 9 _ 1 l 14 10 1 1 r 10 10 _ _ r 11 11 1 1 r 11 11 _ 1 r 12 12 1 _ l 13 12 _ _ l 16 13 1 1 l 13 13 _ _ l 8 14 _ 1 r 15 15 1 1 r 15 15 _ _ l 4 16 1 _ l 16 16 _ _ l 17 17 1 1 l 17 17 _ _ l 18 18 1 _ l 19 19 _ _ l 19 19 1 _ l 20 20 _ _ r 24 20 1 _ r 21 21 _ _ r 21 21 1 1 r 22 22 1 1 r 22 22 _ 1 l 23 23 1 1 l 23 23 _ _ l 19 24 _ _ r 24 24 1 _ r halt

Binary logarithm calculator
It works only for powers of 2. Even now it is very inefficient. Calculates \(log_2(1024)\) in 1082294 steps. 0 1 1 r 2 1 _ 1 r 2 2 _ _ r 2 2 1 _ r 10 3 _ _ r 3 3 1 _ r 4 4 1 _ r 5 4 _ _ l 9 5 1 1 r 5 5 _ _ r 6 6 1 1 r 6 6 _ 1 l 7 7 1 1 l 7 7 _ _ l 8 8 1 1 l 8 8 _ _ r 3 9 _ _ l 9 9 1 1 r 1 10 1 _ r 5 10 _ _ l 11 11 _ _ l 11 11 1 1 r halt

String subsequence determiner
I improved Ikosarakt's design, and I think I improved it much. It works for any number of characters, for n characters it uses 5+2*n states, and has simplier input: write sequence we search for and sequence being search space. ; First machine checks what is first symbol on subsequence we search for 0 a _ r 1a 0 b _ r 1b 0 c _ r 1c 0 | _ r 4 ; If we found whole subsequence, we want to mark it ; Next five transitions repeat for every character 1a * * r 1a 1a _ | r 2a ; We search for blank space marking where we finished our search 2a * * r 2a 2a a _ l 3 ; If we find charachter we look for, we return to the beginning 2a _ _ l 6 ; Otherwise, there is no subsequence we need. We clear tape 1b * * r 1b 1b _ | r 2b 2b * * r 2b 2b b _ l 3 2b _ _ l 6 1c * * r 1c 1c _ | r 2c 2c * * r 2c 2c c _ l 3 2c _ _ l 6 3 * * l 3 3 _ _ r 0 ; This transitions looks for beginning of tape. Then we repeat 4 * _ r 4 4 _ _ r 5 5 * _ r 5 5 _ | l halt ; We clear tape, leaving '|' meaning 'subsequence found' 6 * _ l 6 6 _ _ r halt ; We clear tape, leaving nothing Note: this machine uses lemma I found - consider sequences \(a_1a_2...a_n\) and \(b_1b_2...b_m\). Search algorithm is as this: find smallest \(k_1\) such that \(a_1=b_{k_1}\). Now find smallest \(k_2>k_1\) such that \(a_2=b_{k_2}\) etc. We call sequence \(b_{k_1}b_{k_2}...b_{k_n}\) (if it exists) a minimal subsequence. If minimal subsequence exists, it is isomorphic to a, so a is subsequence of b. Lemma states inverse relation holds: if a is subsequence of b, there exists minimal subsequence.

Challenge: prove this lemma

Prime factorization
I posted it in Turing golf, here is commented version ; Number we currently try to divide is n. Current prime is p ; Setup 0 1 1 l 1 1 _ _ l 2 2 _ 1 l 3 3 _ 1 r 4 4 1 1 r 4 4 _ _ l 5 ; Trying to add current prime to remaining number 5 x x l 5 5 1 x r 6 5 _ _ r 9 ; If succesful, we check if we filled number 6 x x r 6 6 _ _ r 7 7 x x r 7 7 y y r 7 7 1 x l 8 7 _ _ l 27 ; If unsuccesful, number isn't divisible by prime, so we increase prime 8 x x l 8 8 y y l 8 8 _ _ l 5 ; We have to mark number can fit another copy of prime 9 x 1 r 9 9 _ _ r 10 10 x x r 10 10 y y r 10 10 1 1 l 11 10 _ _ l 12 ; If we filled number, we can divide number by prime 11 x y l 8 ; It's important that number of y's is equal to n/p 12 x 1 l 13 13 y 1 l 13 13 1 1 l 13 13 x 1 r 14 13 _ y l 16 ; We finish dividing 14 1 1 r 14 14 _ _ l 15 15 1 _ l 13 ; Now p is prime factor of n, so we copy it to far left of tape 16 1 x l 17 16 x x l 16 16 _ _ l 21 17 1 1 l 17 17 _ _ l 18 18 1 1 l 17 18 _ x r 20 18 x x l 19 19 x x l 19 19 _ x r 20 20 * * r 20 20 y y l 16 ; We make tape ready for another division 21 * * l 21 21 x x l 22 22 x x l 22 22 _ _ r 23 23 x 1 r 23 23 * * r 23 23 y _ r 24 ; Here is check-if-n-is-1 subroutine 24 1 1 r 25 25 1 1 l 26 25 _ _ l 31 ; If it is, we have to finish job 26 1 1 l 4 ; Otherwise, we try to divide by same prime ; Here we have to increase prime ; At the same moment we move all known factors to the left 27 * 1 l 27 27 _ y l 28 28 * 1 l 28 28 _ 1 l 29 29 1 _ l 28 29 _ _ r 30 30 * * r 30 30 y _ l 5 ; And we repeat with new prime ; n is 1, so we delete it. We also delete current prime 31 1 _ l 31 31 _ _ l 32 32 1 _ l 32 32 _ _ l halt I used same principle as in Pólya conjecture checker