{"id":3233,"date":"2015-11-17T17:32:13","date_gmt":"2015-11-17T22:32:13","guid":{"rendered":"http:\/\/www.goodmath.org\/blog\/?p=3233"},"modified":"2015-11-18T14:52:28","modified_gmt":"2015-11-18T19:52:28","slug":"lychrel-numbers-and-the-196-algorithm","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2015\/11\/17\/lychrel-numbers-and-the-196-algorithm\/","title":{"rendered":"Lychrel Numbers and the 196 Algorithm"},"content":{"rendered":"<p> The first donor to the Chesapeake Math program asked me to write about the 196 algorithm, a problem also known as the mystery of the Lychrel numbers. To be completely honest, that&#8217;s something that people have asked for in the past, but I&#8217;ve never written about it, because I didn&#8217;t see what I could add. But I made a promise, so&#8230; here goes!<\/p>\n<p> Take any number with at least two-digits, N. Reverse the digits of N, giving you M, and then add N+M. That gives you a new number. If you keep repeating this process,  most of the time, you&#8217;ll get a palindromic number really fast. <\/p>\n<p> For example, let&#8217;s start with 16:<\/p>\n<ol>\n<li> 16 reversed is 61.<\/li>\n<li>16+61=77. 77 is a palindromic number.<\/li>\n<li> So one reverse+add, and we have a palindromic number.\n<\/ol>\n<p> Or 317:<\/p>\n<ol>\n<li> 317 reversed is 713. <\/li>\n<li> 317+713=1030.<\/li>\n<li> 1030 reversed is 301.<\/li>\n<li> 1030 + 301 = 1331, so we have a palindromic number after two steps.<\/li>\n<\/ol>\n<p> You can play the same game in different number bases. For example, in base 8,  we can start with 013 (11 in base-10): in one reverse+add, it becomes 44 (36 in base-10).<\/p>\n<p> For most numbers, you get a palindrome in just a few steps. But for some numbers, in some number bases, you don&#8217;t. If you can&#8217;t ever get to a palindrome by doing reverse+add starting from a number, then that number is called a <em>Lychrel number<\/em>.<\/em><\/p>\n<p> The process of exploring Lychrel numbers has picked up a lot of devotees, who&#8217;ve developed a whole language for talking about it:<\/p>\n<dd>\n<dt>chain<\/dt>\n<dd>A <em>chain<\/em> is the sequence of numbers produced by reverse+add starting with some number, and possibly converging on a palindromic number.<\/dd>\n<dt>seed<\/dt>\n<dd>The <em>seed<\/em> of a chain is the smallest number that can start that chain. For example, in the example above, we looked at the chain [217, 1030, 1331]. 217 is the seed &#8211; even though you could start a chain at 1030, 1030 would never be considered a seed, because it can be the product of a reverse+add on a smaller number.<\/dd>\n<dt>kin<\/dt>\n<dd>Two numbers are <em>kin numbers<\/em> if they&#8217;re part of a chain that started at the same seed. If a number is a lychrel number, then (obviously) so are all of its kin.<\/dd>\n<\/dl>\n<p> The question that haunts Lychrel enthusiasts is, will you <em>always<\/em> eventually get a palindrome? That is, do Lychrel numbers actually exist?<\/p>\n<p> In base-2, we know the answer to that: not all numbers will produce a palindrome; there are base-2 Lychrel numbers. The smallest base-22 Lychrel number is 22 &#8211; or 10110 in binary. We can look at its reverse add sequence, and see intuitively why it will never produce a palindrome:<\/p>\n<ol>\n<li> 10110<\/li>\n<li> 100011<\/li>\n<li> 1010100<\/li>\n<li> 1101001<\/li>\n<li> 10110100 (10, 11, 01, 00)<\/li>\n<li> 11100001<\/li>\n<li> 101101000<\/li>\n<li>110010101<\/li>\n<li>1011101000 (10, 111, 01, 000)<\/li>\n<li>110010101<\/li>\n<li>1101000101<\/li>\n<li>10111010000<\/li>\n<li>0b11000101101<\/li>\n<li>0b101111010000 (10, 1111, 01, 0000)<\/li>\n<\/ol>\n<p> Starting at step 5, we start seeing a pattern in the sequence, where we have recurring values where that have a pattern of 10, followed by <img src='http:\/\/l.wordpress.com\/latex.php?latex=m&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='m' style='vertical-align:1%' class='tex' alt='m' \/>-1s, followed by 01, followed by <img src='http:\/\/l.wordpress.com\/latex.php?latex=m&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='m' style='vertical-align:1%' class='tex' alt='m' \/> 0s. We&#8217;ve got a sequence that&#8217;s building larger and larger numbers, in a way that will never converge into a palindrome.<\/p>\n<p> We can find similar sequences in any power-of-two base &#8211; base 4, 8, 16, etc. So in power-of-two bases, there are Lychrel numbers. But: are there Lychrel numbers in our familiar base-10?<\/p>\n<p> We think so, but we&#8217;re not sure. No one has been able to prove it either way. But we&#8217;ve got some numbers, which we call <em>Lychrel candidates<\/em>, that we <em>think<\/em> are probably Lychcrel numbers. The smallest one is 196 &#8211; which is why this whole discussion is sometimes called the 196 problem, or the 196 algorithm.<\/p>\n<p> People have written programs that follow the Lychrel thread from 196, trying to see if it reaches a palindrome. So far, the record for exploring the 196 Lychrel thread carries it through more than a billion iterations, producing a non-palindromic number with more than 6 million digits.<\/p>\n<p> That&#8217;s pretty impressive, given that the longest Lychrel thread for any number smaller than 196 is the thread of 24 steps, starting with 89 (which produces the palindromic number 8,813,200,023,188).<\/p>\n<p> From my perspective, one thing that interests me about this is its nature as a computational problem. As a problem, it&#8217;s really easy to implement. For example, here&#8217;s a complete implementation in Ratchet, a Scheme-based programming language. (I used ratchet because it features infinite-precision integers, which makes it easier to write.)<\/p>\n<pre>\n(define (reverse-number n)\n  (string->number (list->string (reverse (string->list (number->string n))))))\n\n(define (palindromic? n)\n  (equal? n (reverse-number n)))\n\n(define (reverse-add n)\n  (+ n (reverse-number n)))\n\n(define (find-palindrome seed)\n  (define (noisy-find-palindrome n count)\n    (if (palindromic? n)\n       n\n      (begin\n        (printf \"At iteration ~v, candidate=~v~n\" count n)\n                (noisy-find-palindrome (reverse-add n) (+ count 1)))))\n  (noisy-find-palindrome seed 0))\n<\/pre>\n<p> I literally threw that together in less than five minutes. In that sense, this is a really, really easy problem to solve. But in another sense, it&#8217;s a very hard problem: there&#8217;s no way to really speed it up.<\/p>\n<p> In modern computing, when we look at a problem that takes a lot of computation to solve, the way that we generally try to approach it is to throw more CPUs at it, and do it in parallel. For most problems that we come across, we can find some reasonable way to divide it into parts that can be run at the same time; then by having a bunch of computers work on those different parts, we can get a solution pretty quickly.<\/p>\n<p> For example, back in the early days of this blog, I did some writing about the Mandelbrot set, and one variant of it that I find particularly interesting, called the Buddhabrot. The Buddhabrot is interesting because it&#8217;s a fractal visualization which isn&#8217;t naively zoomable. In a typical Mandelbrot set visualization, you can pick a small region of it that you want to see in more detail, and focus your computation on just that part, to get a zoomed in view on that. In the Buddhabrot, due to the chaotic nature of the computation, you <em>can&#8217;t<\/em>. So you just compute the Buddhabrot at a massive size, and then you <em>compress<\/em> it. When you want to see a region in more detail, you un-compress. To make that work, buddhabrot&#8217;s are frequently computed at resolutions like 1 million by 1 million pixels. That translates to enough complex floating point computations to compute several trillion values. That&#8217;s a big task. But in modern environments, that&#8217;s routine enough that a friend of mine at Google wrote a program, just for kicks, to compute a big buddhabrot image, and ran it on an experimental cluster.<\/p>\n<p> If that kind of computational capability can be exploited just for kicks, why is it that the best effort at exploring the Lychrel thread for 196 only covers 6 million digits?<\/p>\n<p> The answer is that there&#8217;s a way of computing the Buddhabrot in parallel. You can throw 10,000 CPUs at it for a couple of days, and get an amazing Buddhabrot image. But for the Lychrel thread, there&#8217;s no good way to do it in parallel.<\/p>\n<p> For each additional number in the thread, you need to rearrange and add a couple of million digits. That&#8217;s a lot of work, but it&#8217;s not crazy. On any halfway decent computer, it won&#8217;t take long. To get a sense, I just whipped up a Python program that generated 1,000,000 random pairs of digits, and added them up. It took under 8 seconds &#8211; and that&#8217;s half-assed code written using a not-particularly fast interpreted language on a not-particularly-speedy laptop. A single iteration of the Lychrel thread computation even for a million-digit candidate doesn&#8217;t take that long &#8211; it&#8217;s on the order of seconds. <\/p>\n<p> The catch is that the process of searching a Lychrel thread is intrinsically serial: you can&#8217;t have different CPUs computing the next thread element for different values: you don&#8217;t know the next value until you&#8217;ve finished the previous one. So even if it took just 1 second to do the reverse+add for million digit numbers, it would takes a long time to actually explore the space. If you want to explore the next million candidates, at 2 seconds per iteration, that will take you around 3 weeks!<\/p>\n<p> Even if you don&#8217;t waste time by using a relatively slow interpreter like Python &#8211; even if you use carefully hand-optimized code using an efficient algorithm, it would take months at the very least to explore a space of billions of elements of the 196 thread! And to get beyond what anyone has done, you&#8217;ll probably need to end up doing years of computation, even with a very fast modern computer &#8211; because you can&#8217;t parallelize it.<\/p>\n<p> If you&#8217;re interested in this kind of thing, I recommend looking at <a href=\"http:\/\/www.p196.org\/\">Wade Van Landingham&#8217;s p196 site<\/a>. Wade is the guy who named them Lychrel numbers (based on a rough reversal of his girlfriend (now wife)&#8217;s name, Cheryl). He&#8217;s got all sorts of data, including tracking some of the longest running efforts to continue the 196 thread.<\/p>\n<p><em>A couple of edits were made to this, based on error pointed out by commenters.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first donor to the Chesapeake Math program asked me to write about the 196 algorithm, a problem also known as the mystery of the Lychrel numbers. To be completely honest, that&#8217;s something that people have asked for in the past, but I&#8217;ve never written about it, because I didn&#8217;t see what I could add. [&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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[43],"tags":[],"class_list":["post-3233","post","type-post","status-publish","format-standard","hentry","category-numbers"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-Q9","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/3233","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=3233"}],"version-history":[{"count":3,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/3233\/revisions"}],"predecessor-version":[{"id":3236,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/3233\/revisions\/3236"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=3233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=3233"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=3233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}