# The Hydra Game runs forever

I saw this question here: The Hydra Game algorithm

I am also running into troubles with the same problem.

I also learned of the problem in this Numberphile video, and also tried to compute it myself.

The problem with my code is that it runs forever for n=4. It produces the correct results for n=1-3. I have had it running for about 3 hours now, and it is still running.

Here is my code:

class Node:
def __init__(self, parent):
self.parent = parent
self.children = []

def is_root(self):
return self.parent == None

self.children.append(Node(self))

def chop(self, n = 2):
if not self.parent.is_root():
for i in range(n):
self.parent.children.remove(self)

def find_leftmost(self):
if len(self.children) > 0:
return self.children[-1].find_leftmost()
return self

if __name__ == '__main__':
root = Node(None)

steps = 0
while len(root.children) > 0:
root.find_leftmost().chop(steps+1)
steps += 1

print(steps)

• What makes you think it's running forever? If you periodically print steps, have you seen it pass 938038? Commented Apr 19 at 13:44
• Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
– Community Bot
Commented Apr 19 at 13:51
• Programming questions are off-topic here. Commented Apr 19 at 14:16

But if it is hanging, it is because of while len(root.children) > 0: root.children may never actually be reaching a length of 0.