Hydra function

From BusyBeaverWiki
Revision as of 13:17, 15 April 2025 by Coda (talk | contribs) (Coding the Hydra function: Note that integer division by 2 is equivalent to a right bit shift, which is relevant for the Visualizations below)
Jump to navigation Jump to search
A spiral-like figure that gives the first few terms of the Hydra sequences with initial values 2, 5, 8, 11, 14, and 17.

The Hydra function is a Collatz-like function defined as: H(n)n+12n=32n={3n2if n0(mod2),3n12if n1(mod2). It is named as such because of its connection to the unsolved halting problems for the Cryptids Hydra and Antihydra. Due to its simplicity, simulations for both of these Turing machines utilize this function instead of what can initially be proven.

Relationship to Hydra and Antihydra

Using the Hydra function, we can obtain simplified rules for Hydra and Antihydra:

Hydra Antihydra
Let CH(a,b):=0<A203(a2)3b20:0A>020CH(3,0),CH(2a,0)54a248a2039a81A>20,CH(2a,b+1)54a239a5CH(3a,b),CH(2a+1,b)4b+54a23a+4CH(3a+1,b+2). Let AH(a,b):=01a01b4E>0:0A>011AH(0,8),AH(a,2b)2a+3b21AH(a+2,3b),AH(0,2b+1)3b23b70<F11013b60,AH(a+1,2b+1)3b27AH(a,3b+1).
Proof

Recall the high-level rules for Hydra and Antihydra:

Hydra Antihydra
Let C(a,b):=0<A20a3b20:0A>020C(3,0),C(2a,0)6a2+20a+4033a+11A>20,C(2a,b+1)6a2+23a+10C(3a+3,b),C(2a+1,b)4b+6a2+23a+26C(3a+3,b+2). Let A(a,b):=01a01bE>0:0A>011A(0,4),A(a,2b)2a+3b2+12b+11A(a+2,3b+2),A(0,2b+1)3b2+9b10<F11013b0,A(a+1,2b+1)3b2+12b+5A(a,3b+3).

Already, both machines appear to be very similar. They have one parameter that increases exponentially with growth factor 32 and another that takes a pseudo-random walk. Below, the exponentially increasing variables are described by integer sequences:

Hydra Antihydra
a0=3,an+1={3an+62if an0(mod2)3an+32if an1(mod2) a0=4,an+1={3an+42if an0(mod2)3an+32if an1(mod2)

This will make demonstrating the transformation easier. Now we will define a new integer sequence based on the old one and discover the recursive rules for that sequence. This new sequence is bn=13an+2 and bn=an+4 for Hydra and Antihydra respectively. We start by using bn+1 instead and substituting an+1 for its recursive formula. By doing so, we get:

Hydra Antihydra
b0=3,bn+1={an+62if an0(mod2)an+52if an1(mod2) b0=8,bn+1={3an+122if an0(mod2)3an+112if an1(mod2)

After that, we can substitute an for its solution in terms of bn. What results is the following:

Hydra Antihydra
b0=3,bn+1={3(bn2)+62if 3(bn2)0(mod2)3(bn2)+52if 3(bn2)1(mod2) b0=8,bn+1={3(bn4)+122if bn40(mod2)3(bn4)+112if bn41(mod2)

The if statements amount to checking if bn is even or odd. After simplifying, we are done:

Hydra Antihydra
b0=3,bn+1={3bn2if bn0(mod2)3bn12if bn1(mod2) b0=8,bn+1={3bn2if bn0(mod2)3bn12if bn1(mod2)

Now that we have demonstrated a strong similarity in the behaviour of both Turing machines, we can return to using the high-level rules. Doing that while considering the step counts yields the final result.

Under these rules, the halting problem for Hydra is about whether repeatedly applying the function H(n), starting with n=3, will eventually generate more even terms than twice the number of odd terms. Similarly, Antihydra halts if and only if repeatedly applying H(n), starting with n=8, will eventually generate more odd terms than twice the number of even terms.

Coding the Hydra function

The Hydra function's simple definition allows one to write computer programs that simulate Hydra and Antihydra. The following Python program is a straightforward Hydra simulator based on the Hydra function:

# 'a' and 'b' fulfill the same purpose as in the Hydra rules.
a = 3
b = 0
# As long as Hydra has not halted, 'b' remains greater than -1.
while b != -1:
    # If 'a' is even, decrement 'b', otherwise increase 'b' by 2.
    if a % 2 == 0:
        b -= 1
    else:
        b += 2
    # This performs H(a) = a + floor(a/2).
    # Note that integer division by 2 is equivalent to one bit shift to the right (a >> 1)
    a += a//2

Replacing a = 3 with a = 8 and swapping b -= 1 with b += 2 turns this program into an Antihydra simulator.

Properties

The Hydra function can be rewritten as follows: H(2n)=3n,H(2n+1)=3n+1. Now assume that for some positive integer s and every odd integer t, Hs(2st)=3st and Hs(2st+1)=3st+1, where Hi(n) is function iteration. Notice that we can write 2s+1t=22st and 2s+1t+1=22st+1, so if we apply H to these numbers, we get H(22st)=32st and H(22st+1)=32st+1. Now, if we apply H to these numbers s times, we get Hs+1(2s+1t)=Hs(2s3t)=3s+1t and Hs+1(2s+1t+1)=Hs(2s3t+1)=3s+1t+1. Therefore, by mathematical induction we have proved the following formulas: Hs(2st)=3st,Hs(2st+1)=3st+1. This optimization can be directly applied to the high-level rules for Hydra and Antihydra, producing this result:

Hydra Antihydra
Let CH(a,b):=0<A203(a2)3b20:

CH(2st,b+s)f1(s,t)CH(3st,b),CH(2st+1,b)f2(s,t,b)CH(3st+1,b+2s), where f1(s,t)=3t(3s2s)(18(3s+2s)t65)55s and f2(s,t,b)=(b+s)4s+3t(3s2s)(18(3s+2s)t5)5.

Let AH(a,b):=01a01b4E>0:AH(a,2st)f3(s,t,a)AH(a+2s,3st),AH(a+s,2st+1)f4(s,t)AH(a,3st+1),

where f3(s,t,a)=(2a3+2s)s+3t2(9s4s)5 and f4(s,t)=3t2(9s4s)57s.

Visualizations

The four images below depict the first 1000 values of four Hydra sequences with different initial values. Each row of pixels shows a number in binary on the right and its parity on the left (blue for even, red for odd):