
{"id":5496,"date":"2026-05-13T00:48:15","date_gmt":"2026-05-13T04:48:15","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=5496"},"modified":"2026-05-13T00:55:45","modified_gmt":"2026-05-13T04:55:45","slug":"whats-the-shortest-program-for-which-we-cant-tell-if-it-stops","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=5496","title":{"rendered":"What&#8217;s the shortest program for which we can&#8217;t tell if it stops?"},"content":{"rendered":"<p>Here&#8217;s a 10-line Python program. Nobody on Earth currently knows whether it ever stops.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nf = lambda n: 3*n+1 if n%2 else n\/\/2\r\nstop = seed = 0\r\nwhile not stop:\r\n    fast = slow = seed = seed + 1\r\n    while True:\r\n        fast = f(f(fast))\r\n        slow = f(slow)\r\n        if fast == slow:\r\n            stop = fast not in (1,2,4)\r\n            break\r\n<\/pre>\n<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Halting_problem\">halting problem<\/a> is the question of whether, given a program, we can tell if it will eventually halt or run forever. Theoretically, there must exist programs for which we can&#8217;t tell whether they halt. But can we actually build such a program in practice? How short can we make it?<\/p>\n<h1>How it works<\/h1>\n<p>The program uses the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Collatz_conjecture\">Collatz conjecture<\/a>, also known as the 3n+1 problem. Starting with some positive integer n, we build a sequence in which the next member is 3n+1 if n is odd, and n\/2 if n is even. Every starting value tried so far eventually leads to the &#8220;trivial&#8221; loop 1 \u2192 4 \u2192 2 \u2192 1 \u2192 &#8230; The open question is whether there exists a starting value whose sequence leads to a different loop (or escapes to infinity, but our program won&#8217;t catch that case).<\/p>\n<p>To detect a loop without remembering every value in the path, the program uses the &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cycle_detection#Floyd's_tortoise_and_hare\">tortoise and hare<\/a>&#8221; algorithm: one pointer, <code>fast<\/code>, advances 2 steps at a time, while the other, <code>slow<\/code>, advances 1 step at a time. If they ever meet, we&#8217;ve found a loop.<\/p>\n<p>If that meeting point is none of 1, 2, or 4, it means we&#8217;ve detected a non-trivial loop, and the program halts. If no non-trivial loop exists, the program runs forever.<\/p>\n<h1>Unbounded memory<\/h1>\n<p>It&#8217;s worth noting that unbounded memory, in this case the ability to work with arbitrarily large numbers, is essential for the halting problem. If a program runs on a computer with finite memory, we can always figure out whether it halts by running it on a computer with more memory and recording every state it reaches. Since there is only a finite number of states, we just wait for one to repeat. Repeating states means no halting. It&#8217;s the unbounded memory that makes the halting problem undecidable in general.<\/p>\n<p>Python is therefore an ideal language for the job, because its integers don&#8217;t have a fixed number of bits and can grow indefinitely. In practice, we <em>will<\/em> eventually run out of memory, of course. But in principle, if we assume arbitrarily large numbers, no one knows whether this program halts.<\/p>\n<p>So if you ever see it stop, congratulations: you&#8217;ve just resolved one of the oldest open problems in mathematics.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a 10-line Python program. Nobody on Earth currently knows whether it ever stops. f = lambda n: 3*n+1 if n%2 else n\/\/2 stop = seed = 0 while not <a href=\"https:\/\/ikriv.com\/blog\/?p=5496\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[26,5],"tags":[],"class_list":["entry","author-ikriv","post-5496","post","type-post","status-publish","format-standard","category-python","category-dev"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5496"}],"version-history":[{"count":4,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5496\/revisions"}],"predecessor-version":[{"id":5500,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5496\/revisions\/5500"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}