{"id":801,"date":"2009-09-03T15:53:31","date_gmt":"2009-09-03T15:53:31","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2009\/09\/03\/pathological-programming-with-primes-repost\/"},"modified":"2018-03-02T17:02:15","modified_gmt":"2018-03-02T22:02:15","slug":"pathological-programming-with-primes-repost","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2009\/09\/03\/pathological-programming-with-primes-repost\/","title":{"rendered":"Pathological Programming with Primes (REPOST)"},"content":{"rendered":"<p><em> I&#8217;m currently away on a family vacation, and as soon as vacation is over, I&#8217;m off on a business trip for a week. And along the way, I&#8217;ve got some deadlines for my book. So to fill in, I&#8217;m recycling some old posts. I decided that it&#8217;s been entirely too long since there was any pathological programming &#8217;round these parts, so I&#8217;m going to repost some of my favorites.<\/em><\/p>\n<p> Today&#8217;s pathological language is my personal all-time favorite pathological monstrosity. It&#8217;s an incredibly combination of perfect simplicity and complete incomprehensibility. It&#8217;s based on a piece of work called <em>Fractran<\/em> by John Conway of game theory fame. It&#8217;s a really fascinating bugger; absolutely insanely difficult to program in, but based on one of the most bizarrely elegant concepts of computation that I&#8217;ve ever seen. It&#8217;s amazing that this is Turing complete. It&#8217;s not a real programming language in the sense of being able to write practical programs; it&#8217;s more of a simple theoretical computational model which has been implemented as a<br \/>\nlanguage.<\/p>\n<p><!--more--><\/p>\n<p> It&#8217;s based on the idea of numbers as products of prime factors. As you should remember from elementary school, every number can be represented by a collection of prime numbers that, multiplied together, produce the number. For a few examples:<\/p>\n<ul>\n<li> 24 = 2\u00d72\u00d72\u00d73, or 2<sup>3<\/sup>\u00d73<sup>1<\/sup><\/li>\n<li> 291 = 3\u00d797<\/li>\n<li>1800 = 5\u00d75\u00d73\u00d73\u00d72\u00d72\u00d72=5<sup>2<\/sup>\u00d73<sup>2<\/sup>\u00d72<sup>3<\/sup><\/li>\n<\/ul>\n<p> Conway figured out that using something based on that concept, you can express any computable function using nothing but a list of positive fractions.<\/p>\n<p> Every computation takes a single integer I as input, and operates by repeatedly doing the following:<\/p>\n<ol>\n<li> Set f equal the first fraction in the list.<\/li>\n<li> Set p=f\u00d7I.<\/li>\n<li> If p is an integer, then set I=p, and go back to step 1.<\/li>\n<li> Otherwise, set f to the next fraction in the list, and go back to step 2.<\/li>\n<\/ol>\n<p> When you get through the entire list without any of the multiplications producing an integer, then the computation halts.<\/p>\n<p> That, my friends, is Turing complete.<\/p>\n<p> Let&#8217;s look at an example. How would you implement basic multiplication in Fractran?<\/p>\n<pre>\n385\/13, 13\/21, 1\/7, 3\/11, 7\/2, 1\/3\n<\/pre>\n<p> To make it a tad easier to follow, let&#8217;s factorize the numbers that form the fractions:<\/p>\n<pre>\n(7\u00d711\u00d75)\/13, 13\/(3\u00d77), 3\/11, 7\/2, 1\/3\n<\/pre>\n<p> How is this a multiplication program? If you take any integer I which is the product of 2<sup>a<\/sup> and 3<sup>b<\/sup>, then running it through here will produce the number 5<sup>a\u00d7b<\/sup>.<\/p>\n<p> Let&#8217;s try it: take 2<sup>4<\/sup>\u00d73<sup>3<\/sup>=432. It&#8217;ll be easiest to follow if we use the prime factorings.<\/p>\n<ol>\n<li> I=2<sup>4<\/sup>\u00d73<sup>3<\/sup>; f=385\/13. That won&#8217;t be an integer; 13 isn&#8217;t a factor of I.<\/li>\n<li>f=13\/(3\u00d77). That won&#8217;t be an integer, because 7 isn&#8217;t a factor of I.<\/li>\n<li>f=3\/11. That won&#8217;t be an integer, because 11 isn&#8217;t a factor of I.<\/li>\n<li> f=7\/2. That will be an integer, 2<sup>3<\/sup>\u00d73<sup>3<\/sup>\u00d77.<\/p>\n<li>I=2<sup>3<\/sup>\u00d73<sup>3<\/sup>\u00d77; f=385\/13. Not a prime, because 13 isn&#8217;t a factor of I.<\/li>\n<li>f=13\/(3\u00d77). That will be an integer; I=13\u00d72<sup>3<\/sup>\u00d73<sup>2<\/sup>&#8230; By now, you should have a basic feel for what&#8217;s going on, so I&#8217;m going to start skipping the steps where I\u00d7f is not an integer.<\/li>\n<li> f=385\/13, I becomes 7\u00d711\u00d75\u00d72<sup>3<\/sup>\u00d73<sup>2<\/sup><\/li>\n<li>f=13\/(3\u00d77), I becomes 13\u00d711\u00d75\u00d72<sup>3<\/sup>\u00d73.\n<li> f=(7\u00d711\u00d75)\/13, I becomes 7\u00d711<sup>2<\/sup>\u00d75<sup>2<\/sup>\u00d72<sup>3<\/sup>\u00d73.<\/li>\n<li> f=13\/(3\u00d77), I becomes 13\u00d711<sup>2<\/sup>\u00d75<sup>2<\/sup>\u00d72<sup>3<\/sup>.<\/li>\n<li> f=(7\u00d711\u00d75)\/13, I becomes 7\u00d711<sup>3<\/sup>\u00d75<sup>3<\/sup>\u00d72<sup>3<\/sup>.<\/li>\n<li> &#8230; <\/li>\n<\/ol>\n<p> It keeps going like that. Let&#8217;s analyze it to see what&#8217;s really going on.<\/p>\n<p> the 7\/2 fraction swaps a factor of 2 for a factor of 7. That&#8217;s basically removing a factor of two, which means subtracting 1 from a; and then adding in the 7 is keeping track of the fact that we haven&#8217;t yet added b to the result to match the subtraction of 1 from a. the 13\/(3\u00d77) rule allows us to start the process of adding b to the result. It removes one three, and the placeholder that says we subtracted one from a; and adds in a placeholder to say that we&#8217;ve removed one three, but haven&#8217;t finished adding. the (7\u00d711\u00d75)\/13 rule says that if we&#8217;ve removed a three, we can add one to the exponent of five; and then we also need to add placeholders to continue the addition: we&#8217;ve adding one to the result, but we need to add b to the result. So we&#8217;re effectively subtracting one from b in order to keep track of the fact that we&#8217;ve done that much of an addition of b to the result. 3\/11 says that if the<br \/>\nfirst two rules didn&#8217;t work, then we&#8217;ve finished an addition, we we want to re-add 1 to b, in order to restore it to its original value. The other rules have added one 11 for each time we subtracted one from b, so this will restore b. Finally, if get get to the 1\/3 rule, it means that we&#8217;ve removed all of the 2s, which means we&#8217;ve completed the multiplication. So we want to remove the b leaving the result. Why is this turing complete? It should be pretty easy to see, once you get a sense of what&#8217;s going on. Prime numbers are basically variables &#8211; each prime number holds an integer value (its exponent). The factors of the denominators do two things: subtract values from a variable, and operate as statement guards determining what statements are executable. In terms of control flow, the end result is something that&#8217;s actually quite similar to Version. The primes that aren&#8217;t really being used as variables are the complement of the &#8220;ignore&#8221; set.<\/p>\n<p>Evil, huh?<\/p>\n<p> While researching this post, I discovered (via mathworld) that Conway figured out a way of writing an astonishing prime number generator in Fractran. If you take the following sequence as a fractran program, in the numbers that it generates, the exponent on 2 in every number that it generates will always be prime.<\/p>\n<pre>\n17\/91, 78\/85, 19\/51, 23\/38, 29\/33, 77\/29, 95\/23, 77\/19, 1\/17, 11\/13,\n13\/11, 15\/2, 1\/7, 55\/1\n<\/pre>\n<p> Definitely the most incomprehensible prime number generator that I&#8217;ve ever seen.<\/p>\n<p> For your further fun and edumication, following are two complete Fractran interpreters! The first is one that I wrote in Scheme, and the second is a Haskell implementation which was written by a commenter on the original version of this post under the name &#8220;Psuedonym&#8221;.<\/p>\n<pre>\n;; A Trivial Fractran Interpreter\n;;\n;; Copyright 2006 Mark C. Chu-Carroll\n;; http:\/\/scienceblogs.com\/goodmath\n;;\n;; You're welcome to do anything you want with this code as long\n;; as you keep a copy of this header to identify the original source.\n;;\n;; This program runs a fractran program. A fractran program is a list\n;; of fractions. The fractions are represented by a list of two integers:\n;; the numerator, and the denominator. For example, the classic fractran\n;; multiplication program could be written:\n;;     ((385 13) (13 21) (1 7) (3 11) (7 2) (1 3))\n;; or:\n;;     (((* 7 11 5) 13) (13 (* 3 7)) (1 7) (3 11) (7 2) (1 3))\n;;\n;;\n;; To run a program until it terminates, call (run-fractran program input).\n;; This will return a list; the car of the list will be the result of\n;; running the program, and the cdr will be a trace of the executions in the\n;; form of a list of the fractions that ran at each step.\n;;\n;; To run a program for a specific maximum number of steps, call\n;; (run-fractran-bounded program input maxsteps)\n;;\n(define (step-fractran fract int)\n  (if (equal? fract ()) \n      int\n    (let ((fr (car fract)))\n      (if (= (remainder int (cadr fr)) 0)\n      (cons (\/ (* int (car fr)) (cadr fr))\n        (list fr))\n    (step-fractran (cdr fract) int)))))\n\n(define (run-fractran fract int)\n  (let ((step-result (step-fractran fract int)))\n    (if (list? step-result)`\n    (let ((new-int (car step-result))\n          (last-step (cadr step-result)))\n      (cons step-result (run-fractran fract new-int)))\n      (list int ))))\n\n(define (run-fractran-bounded fract int bound)\n  (if (&gt; bound 0)\n       (let ((step-result (step-fractran fract int)))\n     (if (list? step-result)\n         (let ((new-int (car step-result))\n           (last-step (cadr step-result)))\n           (cons step-result (run-fractran-bounded fract new-int (- bound 1))))\n       (list int)))\n       (list int)))\n\n;; The mult program.\n(define mult '((385 13) (13 21) (1 7) (3 11) (7 2) (1 3)))\n;;\n;; (run-fractran mult 432)\n;; The primes program\n(define primes\n  '((17 91) (78 85) (19 51) (23 38) (29 33) (77 29) (95 23)\n    (77 19) (1 17) (11 13) (13 11) (15 2) (1 7) (55 1)))\n;; (run-fractran-bounded primes 2 1000)\n<\/pre>\n<p> And the Haskell:<\/p>\n<pre>\nmodule Fractran where\nimport Ratio\nimport Data.Maybe\nimport Control.Monad.Fix\ntype Program = [Rational]\n\nrunFractran :: [()] -&gt; Program -&gt; Integer -&gt; [Integer]\nrunFractran bound prog l =\n  step bound prog l\n  where\n    step _ [] l = []\n    step [] (f:fs) l\n      = []\n    step (_:bound) (f:fs) l\n      = let p = f * fromIntegral l\n      in case denominator p of\n      1 -&gt; let pi = numerator p\n        in pi : step bound prog pi\n    _ -&gt; step bound fs l\n\nfractran :: Program -&gt; Integer -&gt; [Integer]\nfractran prog l\n  = runFractran (fix (():)) prog l\n\nfractranBounded :: Int -&gt; Program -&gt; Integer -&gt; [Integer]\nfractranBounded b prog l\n  = runFractran (take b $ fix (():)) prog l\n\nmult = [385%13, 13%21, 1%7, 3%11, 7%2, 1%3]\n\nprimes = [17%91, 78%85, 19%51, 23%38, 29%33, 77%29, 95%23,\n          77%19, 1%17, 11%13, 13%11, 15%2, 1%7, 55%1]\n-- fractran mult (2^4 * 3^3)\n-- fractranBounded 1000 primes 2\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m currently away on a family vacation, and as soon as vacation is over, I&#8217;m off on a business trip for a week. And along the way, I&#8217;ve got some deadlines for my book. So to fill in, I&#8217;m recycling some old posts. I decided that it&#8217;s been entirely too long since there was any [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[92],"tags":[],"class_list":["post-801","post","type-post","status-publish","format-standard","hentry","category-pathological-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-cV","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=801"}],"version-history":[{"count":2,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/801\/revisions"}],"predecessor-version":[{"id":3560,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/801\/revisions\/3560"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}