{"id":49,"date":"2010-08-05T04:52:19","date_gmt":"2010-08-05T04:52:19","guid":{"rendered":"http:\/\/morganredfield.com\/blog\/?p=49"},"modified":"2022-11-20T17:13:51","modified_gmt":"2022-11-20T17:13:51","slug":"how-dense-are-your-palindromes","status":"publish","type":"post","link":"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/","title":{"rendered":"How dense are your palindromes?"},"content":{"rendered":"<blockquote><p>A man, a plan, a canal, Panama.<br \/>\nBombard a drab mob.<br \/>\nDrab as a fool, aloof as a bard.<\/p><\/blockquote>\n<p>Palindromes are strings that can be read the same backwards as forwards. The most common palindromes are words or sentences, but palindromes can be made from any set of characters. Lately I&#8217;ve been interested in palindromic numbers, which are numbers (like 1221 and 5567655) where the the nth digit from the beginning is the same as the nth digit from the end.<\/p>\n<p>Palindromic numbers are pretty easy to generate. Just choose a length for the number, then choose some digits for the first half of the number. Mirror those, and you&#8217;ve got a number. They&#8217;re easier to make than palindromic sentences, because numbers don&#8217;t have to make any kind of logical sense. They&#8217;re kind of like my <a href=\"https:\/\/morganredfield.com\/blog\/?p=13\">symmetric egg basket<\/a> problem, where choosing one half of a solution defines the other half.<\/p>\n<p>In text, palindromes are kind of rare. Palindromes that actually make sense are rarer still. I wanted to see how rare palindromes are in the natural numbers. It turns out that the density of palindromic numbers decreases exponentially as the magnitude of the number increases.<\/p>\n<p>Every single one digit number is a palindrome. There are nine two digit palindromes (the multiples of 11). Three digit palindromes start to get a little more interesting. Any combination of two digits leads to a palindrome of three digits by copying the first digit as the third. This puts the number of three digit palindromes at 9*10 = 90. There are nine choices for the first digit (we exclude zero) and ten choices for the second digit. Four digit palindromes can be counted in the same way, since instead of copying the first digit you copy both first two.<\/p>\n<p>Continuing on in this way, we can find the percentage of numbers of each order of magnitude that are palindromes:<\/p>\n<p>1 digit: 100%<br \/>\n2 digits: 10% (9 palindromes in 90 numbers)<br \/>\n3 digits: 10% (90 palindromes in 900 numbers)<br \/>\n4 digits: 1% (90 palindromes in 9000 numbers)<br \/>\n5 digits: 1% (9*10*10=900 palindromes in 90000 numbers)<br \/>\n6 digits: .1% (900 palindromes in 900000 numbers)<br \/>\n7 digits: .1% (9*10*10*10=9000 palindromes in 9 million numbers)<\/p>\n<p>Every two orders of magnitude that we go up decreases the percentage of palindromes by an order of magnitude. The number of palindromes only increases with two jumps in the size of the number, but the total number of numbers increases with each jump in the size of the number. This is why the percentage of palindromes will slowly decrease.<\/p>\n<p>Just for fun, I also decided to compare palindromes in one base to palindromes in another. I wrote a python script that would compare decimal palindromes to their hex equivalent and report any hex values it found that were also palindromes.<\/p>\n<pre>#check decimal palindromes for hex palindromes\n\ndef convHex(decimal_string):\n    hex_string = str(hex(int(decimal_string)))[2:]\n    is_palindrome = True\n    for i in range(len(hex_string)):\n        if hex_string[i] != hex_string[-(1+i)]:\n            is_palindrome = False\n        #end if\n    #end for\n    if is_palindrome:\n        return [int(decimal_string)]\n    else:\n        return []\n    #end if\n#end def convHex(decimal_string)\n\n\ndouble_palindromes = []\nfor i in range(1,10000):\n    #even number of digits\n    palindrome = str(i) + str(i)[::-1]\n    double_palindromes.extend(convHex(palindrome))\n    #odd number of digits\n    palindrome = str(i) + str(i)[-2::-1]\n    double_palindromes.extend(convHex(palindrome))\n#end for\n\ndouble_palindromes.sort()\n\nfor i in double_palindromes:\n    print i, hex(i)\n<\/pre>\n<p>Again, all one digit numbers are palindromes for every base. Since hex has all the single digits that decimal does, all single digit decimal palindromes are palindromes in hex as well. After that, the numbers that are palindromes in both bases get far rarer.<br \/>\nHere&#8217;s the list of all numbers up under 9 digits long that are palindromes in both decimal and hex bases:<\/p>\n<p>1 0x1<br \/>\n2 0x2<br \/>\n3 0x3<br \/>\n4 0x4<br \/>\n5 0x5<br \/>\n6 0x6<br \/>\n7 0x7<br \/>\n8 0x8<br \/>\n9 0x9<br \/>\n11 0xb<br \/>\n353 0x161<br \/>\n626 0x272<br \/>\n787 0x313<br \/>\n979 0x3d3<br \/>\n1991 0x7c7<br \/>\n3003 0xbbb<br \/>\n39593 0x9aa9<br \/>\n41514 0xa22a<br \/>\n90209 0x16061<br \/>\n94049 0x16f61<br \/>\n96369 0x17871<br \/>\n98689 0x18181<br \/>\n333333 0x51615<br \/>\n512215 0x7d0d7<br \/>\n666666 0xa2c2a<br \/>\n749947 0xb717b<br \/>\n845548 0xce6ec<br \/>\n1612161 0x189981<br \/>\n2485842 0x25ee52<br \/>\n5614165 0x55aa55<br \/>\n6487846 0x62ff26<br \/>\n9616169 0x92bb29<br \/>\n67433476 0x404f404<br \/>\n90999909 0x56c8c65<br \/>\n94355349 0x59fbf95<br \/>\n94544549 0x5a2a2a5<\/p>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Palindromic_numbers\">Wikipedia<\/a> has a lot more information on palindromic numbers. Apparently they&#8217;re pretty common in mathematical riddles and puzzle challenges.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A man, a plan, a canal, Panama. Bombard a drab mob. Drab as a fool, aloof as a bard. Palindromes are strings that can be read the same backwards as forwards. The most common palindromes are words or sentences, but palindromes can be made from any set of characters. Lately I&#8217;ve been interested in palindromic &hellip; <a href=\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How dense are your palindromes?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11,10],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-palindromes","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How dense are your palindromes? - Theory and Practice<\/title>\n<meta name=\"description\" content=\"Palindromes are strings that can be read the same backwards as forwards. The most common palindromes are words or sentences, but palindromes can be made from any set of characters, including numbers.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How dense are your palindromes? - Theory and Practice\" \/>\n<meta property=\"og:description\" content=\"Palindromes are strings that can be read the same backwards as forwards. The most common palindromes are words or sentences, but palindromes can be made from any set of characters, including numbers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/\" \/>\n<meta property=\"og:site_name\" content=\"Theory and Practice\" \/>\n<meta property=\"article:published_time\" content=\"2010-08-05T04:52:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-20T17:13:51+00:00\" \/>\n<meta name=\"author\" content=\"morgan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"morgan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/\"},\"author\":{\"name\":\"morgan\",\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd\"},\"headline\":\"How dense are your palindromes?\",\"datePublished\":\"2010-08-05T04:52:19+00:00\",\"dateModified\":\"2022-11-20T17:13:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/\"},\"wordCount\":589,\"publisher\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd\"},\"keywords\":[\"palindromes\",\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/\",\"url\":\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/\",\"name\":\"How dense are your palindromes? - Theory and Practice\",\"isPartOf\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/#website\"},\"datePublished\":\"2010-08-05T04:52:19+00:00\",\"dateModified\":\"2022-11-20T17:13:51+00:00\",\"description\":\"Palindromes are strings that can be read the same backwards as forwards. The most common palindromes are words or sentences, but palindromes can be made from any set of characters, including numbers.\",\"breadcrumb\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/morganredfield.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How dense are your palindromes?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/morganredfield.com\/blog\/#website\",\"url\":\"https:\/\/morganredfield.com\/blog\/\",\"name\":\"Theory and Practice\",\"description\":\"Exploring life through science and art.\",\"publisher\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/morganredfield.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd\",\"name\":\"morgan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0da7fa0a0a07a8497d51de4300111d978b649e254fc1afa26dc67f3b83d24c38?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0da7fa0a0a07a8497d51de4300111d978b649e254fc1afa26dc67f3b83d24c38?s=96&d=mm&r=g\",\"caption\":\"morgan\"},\"logo\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/morganredfield.com\/blog\/author\/morgan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How dense are your palindromes? - Theory and Practice","description":"Palindromes are strings that can be read the same backwards as forwards. The most common palindromes are words or sentences, but palindromes can be made from any set of characters, including numbers.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/","og_locale":"en_US","og_type":"article","og_title":"How dense are your palindromes? - Theory and Practice","og_description":"Palindromes are strings that can be read the same backwards as forwards. The most common palindromes are words or sentences, but palindromes can be made from any set of characters, including numbers.","og_url":"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/","og_site_name":"Theory and Practice","article_published_time":"2010-08-05T04:52:19+00:00","article_modified_time":"2022-11-20T17:13:51+00:00","author":"morgan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"morgan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/#article","isPartOf":{"@id":"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/"},"author":{"name":"morgan","@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd"},"headline":"How dense are your palindromes?","datePublished":"2010-08-05T04:52:19+00:00","dateModified":"2022-11-20T17:13:51+00:00","mainEntityOfPage":{"@id":"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/"},"wordCount":589,"publisher":{"@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd"},"keywords":["palindromes","Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/","url":"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/","name":"How dense are your palindromes? - Theory and Practice","isPartOf":{"@id":"https:\/\/morganredfield.com\/blog\/#website"},"datePublished":"2010-08-05T04:52:19+00:00","dateModified":"2022-11-20T17:13:51+00:00","description":"Palindromes are strings that can be read the same backwards as forwards. The most common palindromes are words or sentences, but palindromes can be made from any set of characters, including numbers.","breadcrumb":{"@id":"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/morganredfield.com\/blog\/2010\/08\/how-dense-are-your-palindromes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/morganredfield.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How dense are your palindromes?"}]},{"@type":"WebSite","@id":"https:\/\/morganredfield.com\/blog\/#website","url":"https:\/\/morganredfield.com\/blog\/","name":"Theory and Practice","description":"Exploring life through science and art.","publisher":{"@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/morganredfield.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd","name":"morgan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0da7fa0a0a07a8497d51de4300111d978b649e254fc1afa26dc67f3b83d24c38?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0da7fa0a0a07a8497d51de4300111d978b649e254fc1afa26dc67f3b83d24c38?s=96&d=mm&r=g","caption":"morgan"},"logo":{"@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/image\/"},"url":"https:\/\/morganredfield.com\/blog\/author\/morgan\/"}]}},"_links":{"self":[{"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":8,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":657,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions\/657"}],"wp:attachment":[{"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}