[{"id":247468,"date":"2026-07-28T07:13:17","date_gmt":"2026-07-28T12:13:17","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=247468"},"modified":"2026-07-28T07:13:17","modified_gmt":"2026-07-28T12:13:17","slug":"keys-and-cards","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/28\/keys-and-cards\/","title":{"rendered":"Cryptographic Keys and Decks of Cards"},"content":{"rendered":"<p>The <a href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/hiding-data-in-permutations\/\">previous post<\/a> looked at the idea of storing a cryptographic key in the order of a deck of cards. A deck of 52 cards can store 225 bits of data because<\/p>\n<p style=\"padding-left: 40px;\">\u230alog<sub>2<\/sub>(52!)\u230b = 225.<\/p>\n<p>Here \u230a<em>x<\/em>\u230b is\u00a0<em>x<\/em> rounded down to the nearest integer.<\/p>\n<p>If we want to store bigger keys, we&#8217;re going to need a bigger deck of cards.<\/p>\n<h2>Bitcoin<\/h2>\n<p>A Bitcoin key has 256 bits, which would require a deck of 58 cards. There is a card game called Zwicker that uses a deck of 58 cards, the usual 52 cards plus six jokers. So you could store a Bitcoin key in the permutation of a Zwicker deck.<\/p>\n<h2>RSA<\/h2>\n<p>RSA key sizes vary, but 2048 and 3072 are common. A 2048-bit key would require a deck of 301 cards. Casinos often use a shoe of 312 cards, combining six decks of 52 cards, to deal Baccarat or Blackjack. However, casinos combine identical decks. If you were to combine six unique decks, you could store a 2048-bit key.<\/p>\n<p>Storing a 3072-bit key would require a deck of 422 cards. You could make a deck of 432 cards by combining 8 distinguishable packs of 54 cards (52 + 2 jokers).<\/p>\n<h2>ML-KEM<\/h2>\n<p>ML-KEM is a proposed quantum-resistant replacement for RSA. As with RSA, key sizes for ML-KEM vary, the smallest being ML-<strong>KEM-512<\/strong> with a key size of 1632 bytes, which equals 13056 bits. This would require a deck of 1442 cards. You could combine 28 distinct packs of 52 cards, but that&#8217;s unwieldy.<\/p>\n<p>This illustrates one of the difficult trade-offs with post-quantum cryptography: key sizes are much bigger. If you wanted to create a deck of 1442 cards, you&#8217;d probably want to make your &#8220;cards&#8221; something other than standard playing cards. You&#8217;d want to use permutations of something else.<\/p>\n<h2>Verification<\/h2>\n<p>The following Python code verifies the calculations above.<\/p>\n<pre>\r\nfrom math import log2, factorial, floor\r\n\r\ndef capacity(cards):\r\n    return floor(log2(factorial(cards)))\r\n\r\ndef verify(bits, cards):\r\n    return capacity(cards) &gt;= bits and capacity(cards-1) &lt; bits\r\n\r\nprint(verify(256, 58))\r\nprint(verify(2048, 301))\r\nprint(verify(3072, 422))\r\nprint(verify(1632*8, 1442))\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The previous post looked at the idea of storing a cryptographic key in the order of a deck of cards. A deck of 52 cards can store 225 bits of data because \u230alog2(52!)\u230b = 225. Here \u230ax\u230b is\u00a0x rounded down to the nearest integer. If we want to store bigger keys, we&#8217;re going to need [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[197,43],"class_list":["post-247468","post","type-post","status-publish","format-standard","hentry","category-math","tag-combinatorics","tag-cryptography"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"You can store cryptographic keys in the order of a deck of cards. How big a deck would you need to store Bitcoin, RSA, or ML-KEM keys?\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<meta name=\"keywords\" content=\"combinatorics,cryptography\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/28\/keys-and-cards\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Cryptographic Keys and Decks of Cards\" \/>\n\t\t<meta property=\"og:description\" content=\"You can store cryptographic keys in the order of a deck of cards. How big a deck would you need to store Bitcoin, RSA, or ML-KEM keys?\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/28\/keys-and-cards\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-28T12:13:17+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-28T12:13:17+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Cryptographic Keys and Decks of Cards\" \/>\n\t\t<meta name=\"twitter:description\" content=\"You can store cryptographic keys in the order of a deck of cards. How big a deck would you need to store Bitcoin, RSA, or ML-KEM keys?\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Cryptographic Keys and Decks of Cards","description":"You can store cryptographic keys in the order of a deck of cards. How big a deck would you need to store Bitcoin, RSA, or ML-KEM keys?","canonical_url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/28\/keys-and-cards\/","robots":"max-image-preview:large","keywords":"combinatorics,cryptography","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"Cryptographic Keys and Decks of Cards","og:description":"You can store cryptographic keys in the order of a deck of cards. How big a deck would you need to store Bitcoin, RSA, or ML-KEM keys?","og:url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/28\/keys-and-cards\/","article:published_time":"2026-07-28T12:13:17+00:00","article:modified_time":"2026-07-28T12:13:17+00:00","twitter:card":"summary","twitter:title":"Cryptographic Keys and Decks of Cards","twitter:description":"You can store cryptographic keys in the order of a deck of cards. How big a deck would you need to store Bitcoin, RSA, or ML-KEM keys?","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"247468","title":null,"description":"You can store cryptographic keys in the order of a deck of cards. How big a deck would you need to store Bitcoin, RSA, or ML-KEM keys?","keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"created":"2026-07-28 11:10:29","updated":"2026-07-28 12:13:17","ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/math\/\" title=\"Math\">Math<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tCryptographic Keys and Decks of Cards\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Math","link":"https:\/\/www.johndcook.com\/blog\/category\/math\/"},{"label":"Cryptographic Keys and Decks of Cards","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/28\/keys-and-cards\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=247468"}],"version-history":[{"count":1,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247468\/revisions"}],"predecessor-version":[{"id":247469,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247468\/revisions\/247469"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=247468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=247468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=247468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":247462,"date":"2026-07-27T18:08:59","date_gmt":"2026-07-27T23:08:59","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=247462"},"modified":"2026-07-27T18:26:03","modified_gmt":"2026-07-27T23:26:03","slug":"hiding-data-in-permutations","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/hiding-data-in-permutations\/","title":{"rendered":"Hiding data in permutations"},"content":{"rendered":"<p>The <a href=\"https:\/\/pagedout.institute\/download\/PagedOut_009.pdf\">latest issue<\/a> of Paged Out! has an article by Stephen Hewitt &#8220;An off-line backup of your cryptographic key using playing cards.&#8221; The idea is to use a deck of 52 to store a 128-bit cryptographic key. To erase the key, shuffle the deck. Hewitt gives his algorithm for embedding a key, one that can be carried out manually but isn&#8217;t maximally efficient.<\/p>\n<p>You could store a 225-bit key as a permutation of 52 cards because<\/p>\n<p style=\"padding-left: 40px;\">log<sub>2<\/sub>(52!) = 225.581.<\/p>\n<p>But then how would you number permutations so you could go from a number to a particular permutation and later decode the permutation to a number? Is this even practical? For a small number <em>n<\/em>, you could encode a number <em>k<\/em> &lt; <em>n<\/em> by enumerating the first <em>k<\/em> permutations of a set of <em>n<\/em> items, and you could decode by enumerating permutations until you find the one you have. But this is completely impractical for large <em>n<\/em>, such as <em>n<\/em> = 52.<\/p>\n<p>The process of mapping permutation to an integer is called <b>ranking<\/b>, and the mapping from an integer to a permutation is called <b>unranking<\/b>. How efficiently can rankings and unrankings be calculated?<\/p>\n<p>Let <em>n<\/em> be the number of symbols being permuted. Then there are simple algorithms for ranking and unranking with respect to lexicographical order that have complexity <em>O<\/em>(<em>n<\/em>\u00b2) and more sophisticated algorithms that have complexity <em>O<\/em>(<em>n<\/em> log <em>n<\/em>). There are also <em>O<\/em>(<em>n<\/em>) algorithms that do not preserve lexicographical order.<\/p>\n<p>The <code>Permutations<\/code> class in SymPy has methods <code>unrank_lex<\/code> and <code>rank<\/code> to unrank and rank permutations according to lexicographical order.<\/p>\n<p>The notation the <code>Permutations<\/code> class uses requires a little explanation. For example, suppose we unrank 2026.<\/p>\n<pre>&gt;&gt;&gt; from sympy.combinatorics import Permutation\r\n&gt;&gt;&gt; Permutation.unrank_lex(52, 2026)\r\nPermutation(45, 47, 51, 48, 46, 50)\r\n<\/pre>\n<p>The output is not a full list of 52 numbers in permuted order; it is only a cycle. The notation refers to the permutation that sends 45 to 47, 47 to 51, \u2026, 50 to 45 and leaves everything else fixed.<\/p>\n<p>If we rank the permutation given above, we get 2026 back.<\/p>\n<pre>&gt;&gt;&gt; Permutation.rank(Permutation(45, 47, 51, 48, 46, 50))\r\n2026\r\n<\/pre>\n<p>Note that we didn&#8217;t say how many elements (45, 47, 51, 48, 46, 50) is a permutation of. Because of lexicographical order, the rank would be the same whether we viewed this as a permutation of 52 objects or of more objects.<\/p>\n<p>Now let&#8217;s do something larger. Let&#8217;s generate a 220-bit number and encode it as a permutation.<\/p>\n<pre>&gt;&gt;&gt; n = random.getrandbits(225)\r\n&gt;&gt;&gt; a = Permutation.unrank_lex(52, n)\r\n&gt;&gt;&gt; n\r\n40234719030664563684489051530416964877785781669439875437823431388841\r\n&gt;&gt;&gt; a\r\nPermutation(0, 25, 32, 15, 8, 28)(1, 48, 34, 14, 10, 51, 38, 31, 21, 5, 42, 47, 29, 26, 46, 30, 50, 49, 37, 22, 18, 23)(2, 45, 17, 20, 36, 40, 11, 4, 7, 41, 33, 3, 43, 44, 19, 16, 35, 39, 12, 6, 9)\r\n&gt;&gt;&gt; Permutation.rank(a) == n\r\nTrue\r\n<\/pre>\n<p>Now just for fun, let&#8217;s display the permutation above applied to a standard (French) deck of 52 cards. As explained <a href=\"https:\/\/www.johndcook.com\/blog\/2024\/04\/30\/a-deck-of-cards\/\">here<\/a>, symbols associated with these cards have a range of Unicode values. By printing these values, we can visualize the permuted deck.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium\" src=\"https:\/\/www.johndcook.com\/shuffled_deck.png\" width=\"720\" height=\"366\" \/><\/p>\n<p>Here&#8217;s the code that made the image above.<\/p>\n<pre>spades = list(range(0x1F0A1, 0x1F0AF))\r\nspades.remove(0x1F0AC) # take out the knight\r\ncards = [s + 16*i for s in spades for i in range(4)]\r\n\r\na = Permutation.unrank_lex(52, n)\r\np = a(cards)\r\n\r\nfor i in range(4):\r\n    for j in range(13):\r\n        print(chr(p[13*i + j]), end=\"\")\r\n    print()\r\n<\/pre>\n<p>The code above is plenty fast, but Permutation has methods <code>rank_nonlex<\/code> and <code>unrank_nonlex<\/code> that run in <em>O<\/em>(<em>n<\/em>) time, which could be useful for <em>n<\/em> much larger than 52.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The latest issue of Paged Out! has an article by Stephen Hewitt &#8220;An off-line backup of your cryptographic key using playing cards.&#8221; The idea is to use a deck of 52 to store a 128-bit cryptographic key. To erase the key, shuffle the deck. Hewitt gives his algorithm for embedding a key, one that can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[197,43,142],"class_list":["post-247462","post","type-post","status-publish","format-standard","hentry","category-computing","tag-combinatorics","tag-cryptography","tag-sympy"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"The latest issue of Paged Out! has an article by Stephen Hewitt &quot;An off-line backup of your cryptographic key using playing cards.&quot; The idea is to use a deck of 52 to store a 128-bit cryptographic key. To erase the key, shuffle the deck. Hewitt gives his algorithm for embedding a key, one that can\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<meta name=\"keywords\" content=\"combinatorics,cryptography,sympy\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/hiding-data-in-permutations\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Hiding data in permutations\" \/>\n\t\t<meta property=\"og:description\" content=\"The latest issue of Paged Out! has an article by Stephen Hewitt &quot;An off-line backup of your cryptographic key using playing cards.&quot; The idea is to use a deck of 52 to store a 128-bit cryptographic key. To erase the key, shuffle the deck. Hewitt gives his algorithm for embedding a key, one that can\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/hiding-data-in-permutations\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-27T23:08:59+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-27T23:26:03+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Hiding data in permutations\" \/>\n\t\t<meta name=\"twitter:description\" content=\"The latest issue of Paged Out! has an article by Stephen Hewitt &quot;An off-line backup of your cryptographic key using playing cards.&quot; The idea is to use a deck of 52 to store a 128-bit cryptographic key. To erase the key, shuffle the deck. Hewitt gives his algorithm for embedding a key, one that can\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Hiding data in permutations","description":"The latest issue of Paged Out! has an article by Stephen Hewitt \"An off-line backup of your cryptographic key using playing cards.\" The idea is to use a deck of 52 to store a 128-bit cryptographic key. To erase the key, shuffle the deck. Hewitt gives his algorithm for embedding a key, one that can","canonical_url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/hiding-data-in-permutations\/","robots":"max-image-preview:large","keywords":"combinatorics,cryptography,sympy","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"Hiding data in permutations","og:description":"The latest issue of Paged Out! has an article by Stephen Hewitt &quot;An off-line backup of your cryptographic key using playing cards.&quot; The idea is to use a deck of 52 to store a 128-bit cryptographic key. To erase the key, shuffle the deck. Hewitt gives his algorithm for embedding a key, one that can","og:url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/hiding-data-in-permutations\/","article:published_time":"2026-07-27T23:08:59+00:00","article:modified_time":"2026-07-27T23:26:03+00:00","twitter:card":"summary","twitter:title":"Hiding data in permutations","twitter:description":"The latest issue of Paged Out! has an article by Stephen Hewitt &quot;An off-line backup of your cryptographic key using playing cards.&quot; The idea is to use a deck of 52 to store a 128-bit cryptographic key. To erase the key, shuffle the deck. Hewitt gives his algorithm for embedding a key, one that can","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"247462","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"created":"2026-07-27 19:47:26","updated":"2026-07-28 00:53:40","ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/computing\/\" title=\"Computing\">Computing<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tHiding data in permutations\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Computing","link":"https:\/\/www.johndcook.com\/blog\/category\/computing\/"},{"label":"Hiding data in permutations","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/hiding-data-in-permutations\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=247462"}],"version-history":[{"count":4,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247462\/revisions"}],"predecessor-version":[{"id":247467,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247462\/revisions\/247467"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=247462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=247462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=247462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":247457,"date":"2026-07-27T11:03:44","date_gmt":"2026-07-27T16:03:44","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=247457"},"modified":"2026-07-27T11:13:29","modified_gmt":"2026-07-27T16:13:29","slug":"counting-permutations-with-roots","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/counting-permutations-with-roots\/","title":{"rendered":"Counting permutations with roots"},"content":{"rendered":"<p>My post from <a href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/permutation-roots\/\">yesterday<\/a> on permutation roots ends with a Mathematica code for finding the probability that a permutation of <em>n<\/em> elements has a <em>k<\/em>th root. This is done by finding the coefficient of <em>x<\/em><sup><em>n<\/em><\/sup> in the generating function<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium\" src=\"https:\/\/www.johndcook.com\/expq11.svg\" alt=\"\\prod_{m=1}^\\infty \\exp_{\\text{gcd}(m, k)} \\left\\frac{x^m}{m}\\right)\" width=\"162\" height=\"54\" \/><\/p>\n<p>I wanted to say more about this, and look at implementing the same code in SymPy. I was curious how well SymPy would do because I&#8217;ve noticed that LLMs often generate SymPy code since it&#8217;s an open source <abbr title=\"computer algebra system\">CAS<\/abbr>.<\/p>\n<p>Wilf [1] describes the infinite product above as the exponential generating function (egf) of <em>f<\/em>(<em>n<\/em>, <em>k<\/em>), the number of permutations of <em>n<\/em> objects that have a <em>k<\/em>th root. Since egfs have a <em>n<\/em>! term in the denominator, this is also the ordinary generating function (ogf) of the <em>probability<\/em> that a randomly chosen permutation on <em>n<\/em> objects has a <em>k<\/em>th root.<\/p>\n<p>My first attempt at using Mathematica to probe the generating function was<\/p>\n<pre>expq[x_, q_] := MittagLefflerE[q, x^q]\t \r\np[n_, k_] :=  SeriesCoefficient[\t \r\n    Product[expq[x^m\/m, GCD[m, k]], {m, 1, Infinity}], {x, 0, n}]\r\n<\/pre>\n<p>This hung forever when I tried to use it on a small example. I realized, but apparently Mathematica did not, that <code>Infinity<\/code> could be replaced by <code>n<\/code> since terms higher than <em>n<\/em> do not contribute to the coefficient of <em>x<\/em><sup><em>n<\/em><\/sup>. With that change, the code ran quickly.<\/p>\n<p>This morning I tried converting the Mathematica code to Sympy; Claude did this in one shot. I also reproduced the table of <em>f<\/em>(<em>n<\/em>, <em>k<\/em>) values on page 150 of [1] to test the code. Since Wilf tabulated <em>f<\/em>(<em>n<\/em>, <em>k<\/em>), not <em>f<\/em>(<em>n<\/em>, <em>k<\/em>)\/<em>n<\/em>!, I multiplied the results by <em>n<\/em>!.<\/p>\n<p>Here is the output:<\/p>\n<pre>k = 2 [1, 1, 3, 12, 60, 270, 1890, 14280, 128520, 1096200]\r\nk = 3 [1, 2, 4, 16, 80, 400, 2800, 22400, 181440, 1814400]\r\nk = 4 [1, 1, 3, 12, 60, 270, 1890, 13020, 117180, 1039500]\r\nk = 5 [1, 2, 6, 24, 96, 576, 4032, 32256, 290304, 2612736]\r\nk = 6 [1, 1, 1, 4, 40, 190, 1330, 8680, 52920, 340200]\r\nk = 7 [1, 2, 6, 24, 120, 720, 4320, 34560, 311040, 3110400]\r\n<\/pre>\n<p>and here is the SymPy code. I edited the main but the rest is verbatim from Claude.<\/p>\n<pre>from sympy import symbols, gcd, factorial, Rational, S\r\n\r\nx = symbols('x')\r\n\r\ndef expq_coeffs(m, q, n):\r\n    \"\"\"\r\n    Truncated (degree &lt;= n) series coefficients of\r\n        expq(x**m\/m, q) = MittagLefflerE(q, (x**m\/m)**q)\r\n    Since q is a positive integer:\r\n        E_q(y^q) = sum_j y^(q*j) \/ (q*j)!\r\n    with y = x**m\/m, so the term of degree m*q*j has coefficient\r\n        1 \/ ( m**(q*j) * (q*j)! ).\r\n    Returns a list c[0..n] of coefficients.\r\n    \"\"\"\r\n    c = [S.Zero] * (n + 1)\r\n    j = 0\r\n    while m * q * j &lt;= n:\r\n        deg = m * q * j\r\n        c[deg] += Rational(1, m**(q * j) * factorial(q * j))\r\n        j += 1\r\n    return c\r\n\r\ndef poly_mult_trunc(a, b, n):\r\n    \"\"\"Multiply two series (lists of coeffs, index = degree) truncated to degree n.\"\"\"\r\n    c = [S.Zero] * (n + 1)\r\n    for i, ai in enumerate(a):\r\n        if ai == 0:\r\n            continue\r\n        max_j = n - i\r\n        for j2 in range(max_j + 1):\r\n            bj = b[j2]\r\n            if bj != 0:\r\n                c[i + j2] += ai * bj\r\n    return c\r\n\r\ndef p(n, k):\r\n    \"\"\"\r\n    SymPy equivalent of:\r\n        expq[x_, q_] := MittagLefflerE[q, x^q]\r\n        p[n_, k_] := SeriesCoefficient[\r\n            Product[expq[x^m\/m, GCD[m, k]], {m, 1, n}], {x, 0, n}]\r\n    \"\"\"\r\n    result = [S.Zero] * (n + 1)\r\n    result[0] = S.One\r\n    for m in range(1, n + 1):\r\n        q = gcd(m, k)\r\n        factor = expq_coeffs(m, q, n)\r\n        result = poly_mult_trunc(result, factor, n)\r\n    return result[n]\r\n\r\n# example\r\nif __name__ == \"__main__\":\r\n    for k in range(2, 8):\r\n        print(\"k =\", k, [factorial(n)*p(n, k) for n in range(1,11)])\r\n<\/pre>\n<p>[1] Herbert Wilf. Generatingfunctionology. Available online <a href=\"https:\/\/www2.math.upenn.edu\/~wilf\/DownldGF.html\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My post from yesterday on permutation roots ends with a Mathematica code for finding the probability that a permutation of n elements has a kth root. This is done by finding the coefficient of xn in the generating function I wanted to say more about this, and look at implementing the same code in SymPy. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[197,87,142],"class_list":["post-247457","post","type-post","status-publish","format-standard","hentry","category-math","tag-combinatorics","tag-mathematica","tag-sympy"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"Counting permutations that have kth roots. A trick to make Mathematica work and porting the code to SymPy.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<meta name=\"keywords\" content=\"combinatorics,mathematica,sympy\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/counting-permutations-with-roots\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Counting permutations with roots\" \/>\n\t\t<meta property=\"og:description\" content=\"Counting permutations that have kth roots. A trick to make Mathematica work and porting the code to SymPy.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/counting-permutations-with-roots\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-27T16:03:44+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-27T16:13:29+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Counting permutations with roots\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Counting permutations that have kth roots. A trick to make Mathematica work and porting the code to SymPy.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Counting permutations with roots","description":"Counting permutations that have kth roots. A trick to make Mathematica work and porting the code to SymPy.","canonical_url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/counting-permutations-with-roots\/","robots":"max-image-preview:large","keywords":"combinatorics,mathematica,sympy","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"Counting permutations with roots","og:description":"Counting permutations that have kth roots. A trick to make Mathematica work and porting the code to SymPy.","og:url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/counting-permutations-with-roots\/","article:published_time":"2026-07-27T16:03:44+00:00","article:modified_time":"2026-07-27T16:13:29+00:00","twitter:card":"summary","twitter:title":"Counting permutations with roots","twitter:description":"Counting permutations that have kth roots. A trick to make Mathematica work and porting the code to SymPy.","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"247457","title":null,"description":"Counting permutations that have kth roots. A trick to make Mathematica work and porting the code to SymPy.","keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"created":"2026-07-27 15:38:01","updated":"2026-07-28 00:53:40","ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/math\/\" title=\"Math\">Math<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tCounting permutations with roots\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Math","link":"https:\/\/www.johndcook.com\/blog\/category\/math\/"},{"label":"Counting permutations with roots","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/counting-permutations-with-roots\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=247457"}],"version-history":[{"count":2,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247457\/revisions"}],"predecessor-version":[{"id":247461,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247457\/revisions\/247461"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=247457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=247457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=247457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":247449,"date":"2026-07-27T10:34:05","date_gmt":"2026-07-27T15:34:05","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=247449"},"modified":"2026-07-27T15:35:18","modified_gmt":"2026-07-27T20:35:18","slug":"float-binary","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/float-binary\/","title":{"rendered":"Printing floating point numbers in binary"},"content":{"rendered":"<p>It&#8217;s well known that you can convert the base 16 (hex) representation of an integer to the base 2 (binary) representation by simply converting each digit from hex to binary. For example,<\/p>\n<p style=\"padding-left: 40px;\">CAFE<sub>hex<\/sub> = 1100 1010 1111 1110<sub>two<\/sub><\/p>\n<p>I imagine it&#8217;s less well known that you can do the same thing with floating point numbers.<\/p>\n<p>I wanted to find the binary representation of a floating point number using Python, and discovered that it has no function to do this. However, there is a method on floats to show a hex representation. For example, here&#8217;s the hex representation of \u03c0.<\/p>\n<pre>&gt;&gt;&gt; import math\r\n&gt;&gt;&gt; (math.pi).hex()\r\n'0x1.921fb54442d18p+1'\r\n<\/pre>\n<p>Curiously, the p+<em>k<\/em> part at the end is an exponent of 2, not an exponent of 16. So after we convert 1.921fb54442d18 to binary, we&#8217;ll need to multiply by 2, i.e. move the fractional point one space to the right.<\/p>\n<p>So first we convert 1.921fb54442d18<sub>hex<\/sub> to binary by converting 1, 9, 2, etc. each to binary.<\/p>\n<p style=\"padding-left: 40px;\">1.1001 0010 0001 1111 1011 0101 0100 0100 0100 0010 1101 0001 1000<sub>two<\/sub><\/p>\n<p>Then after shifting the fraction point to account for the <code>p+1<\/code> part we have<\/p>\n<p style=\"padding-left: 40px;\">\u03c0 = 11.001001000011111101101010100010001000010110100011000<sub>two<\/sub><\/p>\n<p>You could use Python&#8217;s bin() function to convert the fractional part, interpreted as an integer, to hex, though you may need to pad with 0 bits. For example,<\/p>\n<pre>&gt;&gt;&gt; (1.03).hex()\r\n'0x1.07ae147ae147bp+0\r\n\r\n&gt;&gt;&gt; bin(0x7ae147ae147)\r\n'0b1111010111000010100011110101110000101000111'\r\n<\/pre>\n<p>The binary representation of 1.03<sub>ten<\/sub> is<\/p>\n<p style=\"padding-left: 40px;\">1.000001111010111000010100011110101110000101000111<sub>two<\/sub><\/p>\n<p>We added a total of five zero bits, four for the 0 after the fractional point and one for converting 7 to 0111<sub>two<\/sub>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s well known that you can convert the base 16 (hex) representation of an integer to the base 2 (binary) representation by simply converting each digit from hex to binary. For example, CAFEhex = 1100 1010 1111 1110two I imagine it&#8217;s less well known that you can do the same thing with floating point numbers. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5,9],"tags":[271],"class_list":["post-247449","post","type-post","status-publish","format-standard","hentry","category-computing","category-math","tag-number-systems"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"Python has no method to show floating point numbers in binary, but it does have a method for showing them in hex. How to make the former out of the latter.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<meta name=\"keywords\" content=\"number systems\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/float-binary\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Printing floating point numbers in binary\" \/>\n\t\t<meta property=\"og:description\" content=\"Python has no method to show floating point numbers in binary, but it does have a method for showing them in hex. How to make the former out of the latter.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/float-binary\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-27T15:34:05+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-27T20:35:18+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Printing floating point numbers in binary\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Python has no method to show floating point numbers in binary, but it does have a method for showing them in hex. How to make the former out of the latter.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Printing floating point numbers in binary","description":"Python has no method to show floating point numbers in binary, but it does have a method for showing them in hex. How to make the former out of the latter.","canonical_url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/float-binary\/","robots":"max-image-preview:large","keywords":"number systems","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"Printing floating point numbers in binary","og:description":"Python has no method to show floating point numbers in binary, but it does have a method for showing them in hex. How to make the former out of the latter.","og:url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/float-binary\/","article:published_time":"2026-07-27T15:34:05+00:00","article:modified_time":"2026-07-27T20:35:18+00:00","twitter:card":"summary","twitter:title":"Printing floating point numbers in binary","twitter:description":"Python has no method to show floating point numbers in binary, but it does have a method for showing them in hex. How to make the former out of the latter.","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"247449","title":null,"description":"Python has no method to show floating point numbers in binary, but it does have a method for showing them in hex. How to make the former out of the latter.","keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"created":"2026-07-27 00:05:27","updated":"2026-07-28 00:53:40","ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/computing\/\" title=\"Computing\">Computing<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tPrinting floating point numbers in binary\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Computing","link":"https:\/\/www.johndcook.com\/blog\/category\/computing\/"},{"label":"Printing floating point numbers in binary","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/27\/float-binary\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=247449"}],"version-history":[{"count":7,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247449\/revisions"}],"predecessor-version":[{"id":247463,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247449\/revisions\/247463"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=247449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=247449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=247449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":247440,"date":"2026-07-26T15:32:02","date_gmt":"2026-07-26T20:32:02","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=247440"},"modified":"2026-07-27T10:45:23","modified_gmt":"2026-07-27T15:45:23","slug":"permutation-roots","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/permutation-roots\/","title":{"rendered":"Permutation roots"},"content":{"rendered":"<p>Let \u03c3 be a permutation on <em>n<\/em> elements. If there is a permutation \u03c4 such that applying \u03c4 twice has the same effect on the list of elements as applying \u03c3 once, we say \u03c3 = \u03c4\u00b2 and \u03c4 is a square root of \u03c3.<\/p>\n<p>If we let our <em>n<\/em> elements be the integers 0 through <em>n<\/em> \u2212 1, then we can represent permutations by what they do to this list of numbers. In Python as a tuple of length <em>n<\/em> and compose permutations with the following function:<\/p>\n<pre>import itertools\r\n\r\ndef compose(sigma, tau):\r\n    \"Return the composition \u03c3 \u2218 \u03c4 (apply \u03c4 first, then \u03c3).\"\r\n    return tuple(sigma[j] for j in tau)\r\n<\/pre>\n<p>We can always construct permutations that have square roots by squaring a permutation. If we run the following code<\/p>\n<pre>tau = (3, 1, 4, 5, 2, 0)\r\nsigma = compose(tau, tau)\r\n<\/pre>\n<p>we find \u03c3 = (5, 1, 2, 0, 4, 3), and by construction (3, 1, 4, 5, 2, 0) is a square root of &amp;sigma, though it&#8217;s not the only one.<\/p>\n<p>The following code shows that \u03c3 has four roots.<\/p>\n<pre>import itertools\r\n\r\ndef numroots(sigma):\r\n    n = len(sigma)\r\n    c = 0\r\n    for tau in itertools.permutations(range(n)):\r\n        if sigma == compose(tau, tau):\r\n            c += 1\r\n    return c\r\n\r\nprint( numroots(sigma) )\r\nprint( numroots( (1, 2, 3, 4, 5, 0) ) )\r\n<\/pre>\n<p>It also shows that the rotation (1, 2, 3, 4, 5. 0) has no roots.<\/p>\n<p>The function <code>numroots<\/code> has runtime proportional to <em>n<\/em>! and so it&#8217;s not practical for large permutations. There is a theorem that says a permutation \u03c3 has a square root if and only if the number of cycles it has of every even length is even. See [1].<\/p>\n<p>We can also define cubes and cube roots of permutations, and higher powers and roots.<\/p>\n<p>How common is it for permutations to have square roots, or cube roots, etc.? If you pick a random permutation on <em>n<\/em> elements, what is the probability that it has a <em>k<\/em>th root?<\/p>\n<p>This is a hard question in general, but it is equivalent to finding the coefficient of <em>x<\/em><sup><em>k<\/em><\/sup> in the infinite product<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium\" src=\"https:\/\/www.johndcook.com\/expq11.svg\" alt=\"\\prod_{m=1}^\\infty \\exp_{\\text{gcd}(m, k)} \\left(\\frac{x^m}{m}\\right)\" width=\"162\" height=\"54\" \/><\/p>\n<p>This is theorem 4.8.3 in [1]. This theorem was the motivation for writing about exp<sub><em>q<\/em><\/sub> in the <a href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/exp-q\/\">previous post<\/a>.<\/p>\n<p>Although the product is infinite, there&#8217;s no need to compute terms in the product that only contribute powers of <em>x<\/em> higher than you&#8217;re interested in. The following Mathematica code will compute the probability that a permutation on <em>n<\/em> elements has a <em>k<\/em>th root.<\/p>\n<pre>expq[x_, q_] := MittagLefflerE[q, x^q]\t \r\np[n_, k_] :=  SeriesCoefficient[\t \r\n    Product[expq[x^m\/m, GCD[m, k]], {m, 1, n}], {x, 0, n}]\r\n<\/pre>\n<p>So, for example, the probability that a permutation of 10 elements has a square root is 29\/96.<\/p>\n<p>[1] Herbert Wilf. Generatingfunctionology. Available online <a href=\"https:\/\/www2.math.upenn.edu\/~wilf\/DownldGF.html\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let \u03c3 be a permutation on n elements. If there is a permutation \u03c4 such that applying \u03c4 twice has the same effect on the list of elements as applying \u03c3 once, we say \u03c3 = \u03c4\u00b2 and \u03c4 is a square root of \u03c3. If we let our n elements be the integers 0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-247440","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"Let \u03c3 be a permutation on n elements. If there is a permutation \u03c4 such that applying \u03c4 twice has the same effect on the list of elements as applying \u03c3 once, we say \u03c3 = \u03c4\u00b2 and \u03c4 is a square root of \u03c3. If we let our n elements be the integers 0\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/permutation-roots\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Permutation roots\" \/>\n\t\t<meta property=\"og:description\" content=\"Let \u03c3 be a permutation on n elements. If there is a permutation \u03c4 such that applying \u03c4 twice has the same effect on the list of elements as applying \u03c3 once, we say \u03c3 = \u03c4\u00b2 and \u03c4 is a square root of \u03c3. If we let our n elements be the integers 0\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/permutation-roots\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-26T20:32:02+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-27T15:45:23+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Permutation roots\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Let \u03c3 be a permutation on n elements. If there is a permutation \u03c4 such that applying \u03c4 twice has the same effect on the list of elements as applying \u03c3 once, we say \u03c3 = \u03c4\u00b2 and \u03c4 is a square root of \u03c3. If we let our n elements be the integers 0\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Permutation roots","description":"Let \u03c3 be a permutation on n elements. If there is a permutation \u03c4 such that applying \u03c4 twice has the same effect on the list of elements as applying \u03c3 once, we say \u03c3 = \u03c4\u00b2 and \u03c4 is a square root of \u03c3. If we let our n elements be the integers 0","canonical_url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/permutation-roots\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"Permutation roots","og:description":"Let \u03c3 be a permutation on n elements. If there is a permutation \u03c4 such that applying \u03c4 twice has the same effect on the list of elements as applying \u03c3 once, we say \u03c3 = \u03c4\u00b2 and \u03c4 is a square root of \u03c3. If we let our n elements be the integers 0","og:url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/permutation-roots\/","article:published_time":"2026-07-26T20:32:02+00:00","article:modified_time":"2026-07-27T15:45:23+00:00","twitter:card":"summary","twitter:title":"Permutation roots","twitter:description":"Let \u03c3 be a permutation on n elements. If there is a permutation \u03c4 such that applying \u03c4 twice has the same effect on the list of elements as applying \u03c3 once, we say \u03c3 = \u03c4\u00b2 and \u03c4 is a square root of \u03c3. If we let our n elements be the integers 0","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"247440","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"created":"2026-07-26 19:13:37","updated":"2026-07-28 00:53:40","ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/uncategorized\/\" title=\"Uncategorized\">Uncategorized<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tPermutation roots\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Uncategorized","link":"https:\/\/www.johndcook.com\/blog\/category\/uncategorized\/"},{"label":"Permutation roots","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/permutation-roots\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=247440"}],"version-history":[{"count":8,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247440\/revisions"}],"predecessor-version":[{"id":247459,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247440\/revisions\/247459"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=247440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=247440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=247440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":247432,"date":"2026-07-26T13:20:36","date_gmt":"2026-07-26T18:20:36","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=247432"},"modified":"2026-07-26T15:32:54","modified_gmt":"2026-07-26T20:32:54","slug":"exp-q","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/exp-q\/","title":{"rendered":"exp_q"},"content":{"rendered":"<p>The function exp<sub><em>q<\/em><\/sub>(<em>x<\/em>) is defined by taking the power series for exp(<em>x<\/em>) and keeping only the terms whose index is a multiple of <em>q<\/em>. For example, exp<sub>2<\/sub>(<em>x<\/em>) keeps only the even-numbered terms in the exponential power series and so equals cosh(<em>x<\/em>).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/expqx.svg\" alt=\"\\exp_2(x) = 1 + \\frac{x^2}{2!} + \\frac{x^4}{4!} + \\frac{x^6}{6!} + \\cdots = \\cosh(x)\" width=\"359\" height=\"44\" \/><\/p>\n<p>In general,<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/expq2.svg\" alt=\"\\exp_q(x) = \\sum_{n=0}^\\infty [q \\mid n] \\frac{x^n}{n!} = \\sum_{n=0}^\\infty \\frac{x^{nq}}{(nq)!}\" width=\"285\" height=\"54\" \/><\/p>\n<p>The first sum uses <a href=\"https:\/\/www.johndcook.com\/blog\/2023\/07\/01\/activation-functions\/\">Iverson&#8217;s bracket notation<\/a>: a Boolean expression in brackets denotes the function that returns 1 when the expression is true and zero when it is false. Here the bracket equals 1 when <em>q<\/em> divides <em>n<\/em> and is zero otherwise.<\/p>\n<h2>Closed forms<\/h2>\n<p>Let \u03c9 = exp(2\u03c0<em>i<\/em> \/ <em>q<\/em>). Then<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/expq3.svg\" alt=\"\\exp_q(x) = \\frac{1}{q}\\sum_{k=0}^{q-1} \\exp(\\omega^k x)\" width=\"209\" height=\"60\" \/><\/p>\n<p>This lets us find closed-form expressions for exp<sub><em>q<\/em><\/sub>(<em>x<\/em>). For example, when <em>q<\/em> = 4, \u03c9 = <em>i<\/em> and<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/expq4.svg\" alt=\"\\exp_4(x) = \\frac{1}{2}\\left( \\cosh(x) + \\cos(x) \\right)\" width=\"257\" height=\"40\" \/><\/p>\n<p>Here&#8217;s a proof of the identity above:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/expq5.svg\" alt=\"\\begin{align*} \\frac{1}{q} \\sum_{k=0}^{q-1} \\exp(\\omega^k x) &amp;= \\frac{1}{q} \\sum_{k=0}^{q-1} \\sum_{n=0}^\\infty \\frac{\\omega^{kn}x^n}{n!} \\\\ &amp;= \\sum_{n=0}^\\infty \\left( \\frac{1}{q} \\sum_{k=0}^{q-1} \\omega^{kn}\\right) \\frac{x^n}{n!} \\\\ &amp;= \\sum_{n=0}^\\infty [q \\mid n] \\frac{x^n}{n!} \\\\ &amp;= \\exp_q(x) \\end{align*} \" width=\"296\" height=\"230\" \/><\/p>\n<p>In the proof we used the identity<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/expq6.svg\" alt=\"\\frac{1}{q} \\sum_{k=0}^{q-1} \\omega^{kn} = [q \\mid n]\" width=\"150\" height=\"60\" \/><\/p>\n<p>which is important in deriving the properties of the discrete Fourier transform.<\/p>\n<h2>Differential equations<\/h2>\n<p>The first time I saw the function exp<sub><em>q<\/em><\/sub>(<em>x<\/em>) was in differential equations, though I didn&#8217;t know at the time the function had a name.<\/p>\n<p>When a course in differential equations gets to power series solutions, a common example or homework problem is to solve<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/expq7.svg\" alt=\"y^{(k)}(x) = y(x)\" width=\"117\" height=\"23\" \/><\/p>\n<p>for <em>k<\/em> = 3 or 4, i.e. to find a function that equals its third or fourth derivative.<\/p>\n<p>If the initial conditions are<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/expq8.svg\" alt=\"y(0) = 0\" width=\"70\" height=\"18\" \/><\/p>\n<p>and<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/expq9.svg\" alt=\"y^\\prime(0) = y^{\\prime\\prime}(0) = \\cdots = y^{(k-1)}(0) = 0\" width=\"285\" height=\"23\" \/><\/p>\n<p>the unique solution to<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/expq7.svg\" alt=\"y^{(k)}(x) = y(x)\" width=\"117\" height=\"23\" \/><\/p>\n<p>is <em>y<\/em>(<em>x<\/em>) = exp<sub><em>k<\/em><\/sub>(<em>x<\/em>).<\/p>\n<h2>Mathematica and Mittag-Leffler<\/h2>\n<p>Mathematica does not have a built-in function implementing exp<sub><em>q<\/em><\/sub>(<em>x<\/em>), but it does have an implementation of the <a href=\"https:\/\/www.johndcook.com\/blog\/2016\/07\/17\/mittag-leffler-function-and-probability-distribution\/\">Mittag-Leffler function<\/a>, and so thanks to a relation between this function and exp<sub><em>q<\/em><\/sub>(<em>x<\/em>) you can implement the latter as<\/p>\n<pre>expq[x_, q_] := MittagLefflerE[q, x^q]<\/pre>\n<h2>Combinatorics<\/h2>\n<p>The first time I saw the <em>notation<\/em> exp<sub><em>q<\/em><\/sub>(<em>x<\/em>) was in combinatorics. I had intended to include an application from that book here, but I make that the topic for the <a href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/permutation-roots\/\">next post<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The function expq(x) is defined by taking the power series for exp(x) and keeping only the terms whose index is a multiple of q. For example, exp2(x) keeps only the even-numbered terms in the exponential power series and so equals cosh(x). In general, The first sum uses Iverson&#8217;s bracket notation: a Boolean expression in brackets [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[47,129],"class_list":["post-247432","post","type-post","status-publish","format-standard","hentry","category-math","tag-differential-equations","tag-special-functions"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"The function exp_q(x) is a generalization of the exponential function. It comes up in numerous applications, such as differential equations and combinatorics.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<meta name=\"keywords\" content=\"differential equations,special functions\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/exp-q\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"exp_q(x) keeps every qth term in the power series for exp(x)\" \/>\n\t\t<meta property=\"og:description\" content=\"The function exp_q(x) is a generalization of the exponential function. It comes up in numerous applications, such as differential equations and combinatorics.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/exp-q\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-26T18:20:36+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-26T20:32:54+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"exp_q(x) keeps every qth term in the power series for exp(x)\" \/>\n\t\t<meta name=\"twitter:description\" content=\"The function exp_q(x) is a generalization of the exponential function. It comes up in numerous applications, such as differential equations and combinatorics.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"exp_q(x) keeps every qth term in the power series for exp(x)","description":"The function exp_q(x) is a generalization of the exponential function. It comes up in numerous applications, such as differential equations and combinatorics.","canonical_url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/exp-q\/","robots":"max-image-preview:large","keywords":"differential equations,special functions","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"exp_q(x) keeps every qth term in the power series for exp(x)","og:description":"The function exp_q(x) is a generalization of the exponential function. It comes up in numerous applications, such as differential equations and combinatorics.","og:url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/exp-q\/","article:published_time":"2026-07-26T18:20:36+00:00","article:modified_time":"2026-07-26T20:32:54+00:00","twitter:card":"summary","twitter:title":"exp_q(x) keeps every qth term in the power series for exp(x)","twitter:description":"The function exp_q(x) is a generalization of the exponential function. It comes up in numerous applications, such as differential equations and combinatorics.","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"247432","title":"exp_q(x) keeps every qth term in the power series for exp(x)","description":"The function exp_q(x) is a generalization of the exponential function. It comes up in numerous applications, such as differential equations and combinatorics.","keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"created":"2026-07-26 16:38:25","updated":"2026-07-28 00:53:40","ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/math\/\" title=\"Math\">Math<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\texp_q\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Math","link":"https:\/\/www.johndcook.com\/blog\/category\/math\/"},{"label":"exp_q","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/26\/exp-q\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=247432"}],"version-history":[{"count":9,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247432\/revisions"}],"predecessor-version":[{"id":247446,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247432\/revisions\/247446"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=247432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=247432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=247432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":247423,"date":"2026-07-25T08:37:30","date_gmt":"2026-07-25T13:37:30","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=247423"},"modified":"2026-07-26T08:05:57","modified_gmt":"2026-07-26T13:05:57","slug":"excel-column-numbering","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/25\/excel-column-numbering\/","title":{"rendered":"Excel column numbering"},"content":{"rendered":"<p>I was working with a wide spreadsheet from a client the other day and I had to convert between Excel column labels and column numbers. I had never paid attention to how Excel labels columns and implicitly thought it was base 26 using letters rather than digits. But then I realized that&#8217;s not right.<\/p>\n<p>Excel labels columns A through Z, then AA through AZ, then BA through BZ, etc. If this is base 26, then does A correspond to 0? That could work for A through Z, but then what about AA? Then you&#8217;d have to say the first A corresponds to 26 but the second A corresponds to 0.<\/p>\n<p>Does Z correspond to 0? If so then the column numbers would be 1 through 25, followed by 0, then 27. And it would mean that columns ZA through ZZ are the same as A through Z.<\/p>\n<p>In fact nothing in Excel column labeling corresponds to 0. The labels cannot be interpreted as a positional number system.<\/p>\n<p>There&#8217;s a name for this kind of number system: <strong>bijective base 26<\/strong>. The concept extends generally to bijective base\u00a0<em>b<\/em> for any positive integer\u00a0<em>b<\/em>. The idea is ancient, but the name was coined recently. It has also been called <em>k<\/em>-adic numbering. For most of history it didn&#8217;t have a name.<\/p>\n<p>The motivation behind the name bijective base\u00a0<em>b<\/em> is that there is a bijection (a one-to-one correspondence) between these symbols and positive integers; there&#8217;s no possibility of leading zeros that would keep the mapping from being a bijection, unlike say 7 and 07 representing the same number.<\/p>\n<h2>Excel limits<\/h2>\n<p>Before 2007, an Excel file could have a maximum of 2<sup>8<\/sup> = 256 columns, and so the largest column label was IV.<\/p>\n<p>Then in 2007 the column limit was increased to 2<sup>14<\/sup> = 16,384 and the largest column label is XFD.<\/p>\n<h2>Conversion code<\/h2>\n<p>Converting from column labels to integers is easy; going the other way is a little more complicated.<\/p>\n<pre>letter_to_ordinal = lambda c: ord(c) - ord('A') + 1\r\nordinal_to_letter = lambda n: chr(ord('A') + n - 1)\r\n\r\ndef label_to_num(label):\r\n    label = label.upper()\r\n    n = 0\r\n    for c in label:\r\n        n = n*26 + letter_to_ordinal(c)\r\n    return n\r\n\r\ndef num_to_label(n):\r\n    letters = []\r\n    while n &gt; 0:\r\n        n, remainder = divmod(n - 1, 26)\r\n        letters.append(ordinal_to_letter(remainder + 1))\r\n    return ''.join(reversed(letters))\r\n<\/pre>\n<p>Here&#8217;s an <a href=\"https:\/\/www.johndcook.com\/excel_labels.html\">online calculator<\/a> based on the code above.<\/p>\n<h2>Tests<\/h2>\n<p>The following code verifies the assertions above about the maximum number of Excel columns over time.<\/p>\n<pre>assert(num_to_label(256) == \"IV\")\r\nassert(label_to_num(\"IV\") == 256)\r\n\r\nassert(num_to_label(2**14) == \"XFD\")\r\nassert(label_to_num(\"XFD\") == 2**14)\r\n<\/pre>\n<p>The conversion routines are not limited to actual Excel labels but work for arbitrarily large integers and bijective base 26 representations. For example, the following code shows that the bijective base 26 representation of Avogadro&#8217;s number is MUAEKAUDYDXEWOSDD.<\/p>\n<pre>avogadro = 602_214_076_000_000_000_000_000\r\nassert(label_to_num(num_to_label(avogadro)) == avogadro)\r\nprint(num_to_label(avogadro))\r\n<\/pre>\n<h2>Related posts<\/h2>\n<ul>\n<li class=\"link\"><a href=\"https:\/\/www.johndcook.com\/radix_conversion.html\">Radix conversion calclator<\/a><\/li>\n<li class=\"link\"><a href=\"https:\/\/www.johndcook.com\/blog\/2020\/03\/16\/round-trip-radix-conversion\/\">When is floating point radix conversion exact?<\/a><\/li>\n<li class=\"link\"><a href=\"https:\/\/www.johndcook.com\/blog\/2019\/09\/07\/excel-r-bom\/\">Excel, R, and Unicode<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I was working with a wide spreadsheet from a client the other day and I had to convert between Excel column labels and column numbers. I had never paid attention to how Excel labels columns and implicitly thought it was base 26 using letters rather than digits. But then I realized that&#8217;s not right. Excel [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[271],"class_list":["post-247423","post","type-post","status-publish","format-standard","hentry","category-math","tag-number-systems"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"Excel labels are sort like base 26 numbers, but not really. What kind of numbers are they? How do you convert between column labels and column numbers?\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<meta name=\"keywords\" content=\"number systems\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/25\/excel-column-numbering\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Excel column numbering\" \/>\n\t\t<meta property=\"og:description\" content=\"Excel labels are sort like base 26 numbers, but not really. What kind of numbers are they? How do you convert between column labels and column numbers?\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/25\/excel-column-numbering\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-25T13:37:30+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-26T13:05:57+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Excel column numbering\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Excel labels are sort like base 26 numbers, but not really. What kind of numbers are they? How do you convert between column labels and column numbers?\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Excel column numbering","description":"Excel labels are sort like base 26 numbers, but not really. What kind of numbers are they? How do you convert between column labels and column numbers?","canonical_url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/25\/excel-column-numbering\/","robots":"max-image-preview:large","keywords":"number systems","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"Excel column numbering","og:description":"Excel labels are sort like base 26 numbers, but not really. What kind of numbers are they? How do you convert between column labels and column numbers?","og:url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/25\/excel-column-numbering\/","article:published_time":"2026-07-25T13:37:30+00:00","article:modified_time":"2026-07-26T13:05:57+00:00","twitter:card":"summary","twitter:title":"Excel column numbering","twitter:description":"Excel labels are sort like base 26 numbers, but not really. What kind of numbers are they? How do you convert between column labels and column numbers?","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"247423","title":null,"description":"Excel labels are sort like base 26 numbers, but not really. What kind of numbers are they? How do you convert between column labels and column numbers?","keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"created":"2026-07-25 12:09:26","updated":"2026-07-26 19:13:30","ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/math\/\" title=\"Math\">Math<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tExcel column numbering\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Math","link":"https:\/\/www.johndcook.com\/blog\/category\/math\/"},{"label":"Excel column numbering","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/25\/excel-column-numbering\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=247423"}],"version-history":[{"count":6,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247423\/revisions"}],"predecessor-version":[{"id":247431,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247423\/revisions\/247431"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=247423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=247423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=247423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":247418,"date":"2026-07-23T09:04:34","date_gmt":"2026-07-23T14:04:34","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=247418"},"modified":"2026-07-23T12:27:59","modified_gmt":"2026-07-23T17:27:59","slug":"an-almost-periodic-function","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/23\/an-almost-periodic-function\/","title":{"rendered":"An almost periodic function"},"content":{"rendered":"<p>This post takes a more abstract view of the <a href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/22\/degrees-radians\/\">previous post<\/a>. That post looked at the concrete question of whether a number ever has the same sine in radians as in degrees. The relation between radians and degrees is irrelevant except that \u03c0\/180 is an irrational number.<\/p>\n<p>Suppose \u03b1 and \u03b2 are two positive numbers such that \u03b1\/\u03b2 is irrational. In the previous post, \u03b1 = 1 and \u03b2 = \u03c0\/180. Then the function<\/p>\n<p style=\"padding-left: 40px;\"><em>f<\/em>(<em>x<\/em>) = sin(\u03b1<em>x<\/em>) \u2212 sin(\u03b2<em>x<\/em>)<\/p>\n<p>is almost periodic: it is not periodic, but it comes close to being periodic, as close as you&#8217;d like provided you&#8217;re willing to look over a sufficiently long range of <em>x<\/em>&#8216;s.<\/p>\n<p>The identity<\/p>\n<p style=\"padding-left: 40px;\">sin(\u03b1<em>x<\/em>) \u2212 sin(\u03b2<em>x<\/em>) = 2 cos((\u03b1 + \u03b2)<em>x<\/em>\/2) sin((\u03b1 \u2212 \u03b2)<em>x<\/em>\/2)<\/p>\n<p>shows that <em>f<\/em>(<em>x<\/em>) is the product of two periodic functions but is not periodic itself. The periods of the cosine and sine above never coincide because the ratio of their frequencies is irrational.<\/p>\n<p>The zeros of <em>f<\/em> are not periodic, though they can be divided into two subsequences that are periodic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post takes a more abstract view of the previous post. That post looked at the concrete question of whether a number ever has the same sine in radians as in degrees. The relation between radians and degrees is irrelevant except that \u03c0\/180 is an irrational number. Suppose \u03b1 and \u03b2 are two positive numbers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-247418","post","type-post","status-publish","format-standard","hentry","category-math"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"An abstract look at a concrete problem.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/23\/an-almost-periodic-function\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"An almost periodic function\" \/>\n\t\t<meta property=\"og:description\" content=\"An abstract look at a concrete problem.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/23\/an-almost-periodic-function\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-23T14:04:34+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-23T17:27:59+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"An almost periodic function\" \/>\n\t\t<meta name=\"twitter:description\" content=\"An abstract look at a concrete problem.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"An almost periodic function","description":"An abstract look at a concrete problem.","canonical_url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/23\/an-almost-periodic-function\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"An almost periodic function","og:description":"An abstract look at a concrete problem.","og:url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/23\/an-almost-periodic-function\/","article:published_time":"2026-07-23T14:04:34+00:00","article:modified_time":"2026-07-23T17:27:59+00:00","twitter:card":"summary","twitter:title":"An almost periodic function","twitter:description":"An abstract look at a concrete problem.","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"247418","title":null,"description":"An abstract look at a concrete problem.","keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"created":"2026-07-23 13:53:27","updated":"2026-07-25 15:27:32","ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/math\/\" title=\"Math\">Math<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tAn almost periodic function\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Math","link":"https:\/\/www.johndcook.com\/blog\/category\/math\/"},{"label":"An almost periodic function","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/23\/an-almost-periodic-function\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=247418"}],"version-history":[{"count":3,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247418\/revisions"}],"predecessor-version":[{"id":247421,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247418\/revisions\/247421"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=247418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=247418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=247418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":247407,"date":"2026-07-22T07:17:11","date_gmt":"2026-07-22T12:17:11","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=247407"},"modified":"2026-07-22T09:29:26","modified_gmt":"2026-07-22T14:29:26","slug":"degrees-radians","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/22\/degrees-radians\/","title":{"rendered":"When sine of x degrees equals sine of x radians"},"content":{"rendered":"<p>Ordinarily the sine of <em>x<\/em> radians and the sine of <em>x<\/em> degrees are very different numbers. Having your calculator in radian mode when it should be in degree mode, or vice versa, results in a major error.<\/p>\n<p>But sometimes it doesn&#8217;t matter. A trivial example is when <em>x<\/em> = 0. A more interesting example is<\/p>\n<p style=\"padding-left: 40px;\"><em>x<\/em> = 180\u03c0\/(180 + \u03c0) = 3.08770208\u2026.<\/p>\n<p>For that value of <em>x<\/em>,<\/p>\n<p style=\"padding-left: 40px;\">sin(<em>x<\/em>) = sin(<em>x<\/em>\u00b0).<\/p>\n<p>In this article I&#8217;ll use the common convention of using radians by default and denoting degrees with \u00b0 as above.<\/p>\n<p>Note that<\/p>\n<p style=\"padding-left: 40px;\"><em>x<\/em><em>\u00a0= <\/em>\u03c0<em>x<\/em><em>\u00b0<\/em>\/180<\/p>\n<p>and so we are interested in solutions to the equation<\/p>\n<p style=\"padding-left: 40px;\">sin(<em>x<\/em>) = sin(\u03c0<em>x<\/em>\/180)<\/p>\n<p>Now two angles\u00a0<em>A<\/em> and\u00a0<em>B<\/em> have the same sine if they differ by a multiple of 2\u03c0, or if they&#8217;re supplementary (i.e. <em>A<\/em> = \u03c0 \u2212 <em>B<\/em>), or both. To put it another way, if\u00a0<em>A<\/em> and\u00a0<em>B<\/em> have the same sine, they are either equal mod 2\u03c0 or supplementary mod 2\u03c0. This means that<\/p>\n<p style=\"padding-left: 40px;\">sin(<em>x<\/em>) = sin(\u03c0<em>x<\/em>\/180)<\/p>\n<p>if and only if<\/p>\n<p style=\"padding-left: 40px;\"><em>x<\/em> = \u03c0<em>x<\/em>\/180 + 2\u03c0<em>k<\/em><\/p>\n<p>or<\/p>\n<p style=\"padding-left: 40px;\"><em>x<\/em> = \u03c0 \u2212 \u03c0<em>x<\/em>\/180 + 2\u03c0<em>k<\/em><\/p>\n<p>for some integer\u00a0<em>k<\/em>.<\/p>\n<p>Therefore all solutions have the form<\/p>\n<p style=\"padding-left: 40px;\"><em>x<\/em> = 360\u03c0<em>k<\/em>\/(180 \u2212 \u03c0)<\/p>\n<p>or<\/p>\n<p style=\"padding-left: 40px;\"><em>x<\/em> = 180\u03c0(2<em>k<\/em> + 1)\/(180 + \u03c0).<\/p>\n<h2>Alternative solution<\/h2>\n<p>The derivation above is correct, but it occurred to me later that a simpler argument would be to use the identity<\/p>\n<p style=\"padding-left: 40px;\">sin(<em>A<\/em>) \u2212 sin(<em>B<\/em>) = 2 cos((<em>A<\/em> +\u00a0<em>B<\/em>)\/2) sin((<em>A<\/em> \u2212 <em>B<\/em>)\/2).<\/p>\n<p>Thus <em>A<\/em> and\u00a0<em>B<\/em> have the same sine if<\/p>\n<p style=\"padding-left: 40px;\">cos((<em>A<\/em> +\u00a0<em>B<\/em>)\/2) = 0<\/p>\n<p>or if<\/p>\n<p style=\"padding-left: 40px;\">sin((<em>A<\/em> \u2212 <em>B<\/em>)\/2) = 0.<\/p>\n<p>These two possibilities correspond to the two families of solutions above.<\/p>\n<h2>Density<\/h2>\n<p>When reduced modulo 2\u03c0, both families are dense in [0, 2\u03c0]. This means that for every\u00a0<em>y<\/em> in [\u22121, 1], there is a number\u00a0<em>x<\/em> such that<\/p>\n<p style=\"padding-left: 40px;\">sin(<em>x<\/em>) = sin(<em>x<\/em>\u00b0) \u2248 <em>y<\/em><\/p>\n<p>and we can make the approximation as good as we&#8217;d like.<\/p>\n<h3>Example 1<\/h3>\n<p>For example, today is July 22, so let&#8217;s set\u00a0<em>y<\/em> = 0.722. We&#8217;d like to find a value of\u00a0<em>x<\/em> such that the sine of\u00a0<em>x<\/em> radians and the sine of <em>x<\/em> degrees both approximately equal 0.722. And let&#8217;s say our approximation tolerance is \u03b5 = 0.0001.<\/p>\n<p>We can search for a value of\u00a0<em>x<\/em> in the first family of solutions by looking for a value of\u00a0<em>k<\/em> with<\/p>\n<p style=\"padding-left: 40px;\">| sin(360\u03c0<em>k<\/em>\/(180 \u2212 \u03c0)) \u2212 0.722 | &lt; 0.0001<\/p>\n<p>and the smallest such\u00a0<em>k<\/em> is 96343 and so<\/p>\n<p style=\"padding-left: 40px;\"><em>x<\/em> = 360\u00d796343 \u03c0\/(180 \u2212 \u03c0) = 616093.78713621\u2026<\/p>\n<p>will do, and sin(<em>x<\/em>) = 0.72191\u2026<\/p>\n<h3>Example 2<\/h3>\n<p>Now let&#8217;s set\u00a0<em>y<\/em> = 0.2026 and look for a solution in the other family of solutions, and this time let&#8217;s set \u03b5 = 10<sup>\u22126<\/sup>. The smallest value of\u00a0<em>k<\/em> such that<\/p>\n<p style=\"padding-left: 40px;\">| sin(180\u03c0(2<em>k<\/em> + 1)\/(180 + \u03c0)) \u2212 0.2026 | &lt; 10<sup>\u22126<\/sup><\/p>\n<p>is\u00a0<em>k<\/em> = 741141. Then<\/p>\n<p style=\"padding-left: 40px;\">sin( 4576848.310950611 ) = sin( 4576848.310950611\u00b0 ) = 0.202600139\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ordinarily the sine of x radians and the sine of x degrees are very different numbers. Having your calculator in radian mode when it should be in degree mode, or vice versa, results in a major error. But sometimes it doesn&#8217;t matter. A trivial example is when x = 0. A more interesting example is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[94],"class_list":["post-247407","post","type-post","status-publish","format-standard","hentry","category-math","tag-number-theory"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"There are infinitely many values of x such that the sine of x degrees equals the sine of x radians. And for every \u22121 \u2264 y \u2264 1 there such an x with sin(x) \u2248 y.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<meta name=\"keywords\" content=\"number theory\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/22\/degrees-radians\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"When sine of x degrees equals sine of x radians\" \/>\n\t\t<meta property=\"og:description\" content=\"There are infinitely many values of x such that the sine of x degrees equals the sine of x radians. And for every \u22121 \u2264 y \u2264 1 there such an x with sin(x) \u2248 y.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/22\/degrees-radians\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-22T12:17:11+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-22T14:29:26+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"When sine of x degrees equals sine of x radians\" \/>\n\t\t<meta name=\"twitter:description\" content=\"There are infinitely many values of x such that the sine of x degrees equals the sine of x radians. And for every \u22121 \u2264 y \u2264 1 there such an x with sin(x) \u2248 y.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"When sine of x degrees equals sine of x radians","description":"There are infinitely many values of x such that the sine of x degrees equals the sine of x radians. And for every \u22121 \u2264 y \u2264 1 there such an x with sin(x) \u2248 y.","canonical_url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/22\/degrees-radians\/","robots":"max-image-preview:large","keywords":"number theory","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"When sine of x degrees equals sine of x radians","og:description":"There are infinitely many values of x such that the sine of x degrees equals the sine of x radians. And for every \u22121 \u2264 y \u2264 1 there such an x with sin(x) \u2248 y.","og:url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/22\/degrees-radians\/","article:published_time":"2026-07-22T12:17:11+00:00","article:modified_time":"2026-07-22T14:29:26+00:00","twitter:card":"summary","twitter:title":"When sine of x degrees equals sine of x radians","twitter:description":"There are infinitely many values of x such that the sine of x degrees equals the sine of x radians. And for every \u22121 \u2264 y \u2264 1 there such an x with sin(x) \u2248 y.","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"247407","title":null,"description":"There are infinitely many values of x such that the sine of x degrees equals the sine of x radians. And for every \u22121 \u2264 y \u2264 1 there such an x with sin(x) \u2248 y.","keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"created":"2026-07-22 11:05:06","updated":"2026-07-22 21:49:00","ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/math\/\" title=\"Math\">Math<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tWhen sine of x degrees equals sine of x radians\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Math","link":"https:\/\/www.johndcook.com\/blog\/category\/math\/"},{"label":"When sine of x degrees equals sine of x radians","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/22\/degrees-radians\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=247407"}],"version-history":[{"count":9,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247407\/revisions"}],"predecessor-version":[{"id":247416,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247407\/revisions\/247416"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=247407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=247407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=247407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":247395,"date":"2026-07-21T10:05:04","date_gmt":"2026-07-21T15:05:04","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=247395"},"modified":"2026-07-21T17:18:37","modified_gmt":"2026-07-21T22:18:37","slug":"forensic-accounting-in-python","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/21\/forensic-accounting-in-python\/","title":{"rendered":"Forensic accounting in Python"},"content":{"rendered":"<p>I recently had a project in which I had to reverse engineer a data analysis. There was some ambiguity regarding which of several possibilities someone chose for several of the variables, something analogous to the following example.<\/p>\n<p>Suppose you have three numbers with uncertain values with a known, or at least purported, sum. The first number could be 31, 41, or 59; the second could be either 26 or 53; the last could be 58, 97, 93, or 23.<\/p>\n<p>The following code enumerates all 3 &times; 2 &times; 4 = 24 possibilities and prints their sums.<\/p>\n<pre>\r\nfrom itertools import product\r\n\r\n# Example input\r\npossibilities = [(31, 41, 59), (26, 53), (58, 97, 93, 23)]\r\n\r\nfor combo in product(*possibilities):\r\n    total = sum(combo) \r\n    print(f\"Combination {combo} sums to: {total}\")\r\n<\/pre>\n<p>In this example all the sums are unique, though of course that might not happen in practice. If, for example, you know the sum is 187, you know the three numbers were 41, 53, and 93. If the reported sum is 200, you know some assumption has been violated because none of the possible choices add up to 200.<\/p>\n<h2>More forensics posts<\/h2>\n<ul>\n<li class='link'><a href='https:\/\/www.johndcook.com\/blog\/2009\/09\/18\/make-up-your-own-rules-of-probability\/'>Make up your own rules of probability<\/a><\/li>\n<li class='link'><a href='https:\/\/www.johndcook.com\/blog\/2023\/04\/01\/identifiable-to-man-or-machine\/'>Identifiable to man or machine?<\/a><\/li>\n<li class='link'><a href='https:\/\/www.johndcook.com\/blog\/2024\/02\/13\/photo-metadata\/'>Metadata in photos<\/a><\/li>\n<li class='link'><a href='https:\/\/www.johndcook.com\/blog\/2026\/05\/05\/changing-one-character-in-a-pdf\/'>Changing one character in a PDF<\/a><\/li>\n<li class='link'><a href='https:\/\/www.johndcook.com\/blog\/2018\/03\/02\/bits-of-information-in-age-birthday-and-birthdate\/'>Bits of information in age or birthday<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I recently had a project in which I had to reverse engineer a data analysis. There was some ambiguity regarding which of several possibilities someone chose for several of the variables, something analogous to the following example. Suppose you have three numbers with uncertain values with a known, or at least purported, sum. The first [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-247395","post","type-post","status-publish","format-standard","hentry","category-statistics"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"Writing Python code to reverse engineer a data analysis, iterating over possibilities to try to make the numbers add up.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/21\/forensic-accounting-in-python\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Forensic statistics in Python\" \/>\n\t\t<meta property=\"og:description\" content=\"Writing Python code to reverse engineer a data analysis, iterating over possibilities to try to make the numbers add up.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2026\/07\/21\/forensic-accounting-in-python\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-21T15:05:04+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-21T22:18:37+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Forensic statistics in Python\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Writing Python code to reverse engineer a data analysis, iterating over possibilities to try to make the numbers add up.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Forensic statistics in Python","description":"Writing Python code to reverse engineer a data analysis, iterating over possibilities to try to make the numbers add up.","canonical_url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/21\/forensic-accounting-in-python\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"Forensic statistics in Python","og:description":"Writing Python code to reverse engineer a data analysis, iterating over possibilities to try to make the numbers add up.","og:url":"https:\/\/www.johndcook.com\/blog\/2026\/07\/21\/forensic-accounting-in-python\/","article:published_time":"2026-07-21T15:05:04+00:00","article:modified_time":"2026-07-21T22:18:37+00:00","twitter:card":"summary","twitter:title":"Forensic statistics in Python","twitter:description":"Writing Python code to reverse engineer a data analysis, iterating over possibilities to try to make the numbers add up.","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"247395","title":"Forensic statistics in Python","description":"Writing Python code to reverse engineer a data analysis, iterating over possibilities to try to make the numbers add up.","keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"created":"2026-07-21 02:37:46","updated":"2026-07-21 22:18:58","ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/statistics\/\" title=\"Statistics\">Statistics<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tForensic accounting in Python\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Statistics","link":"https:\/\/www.johndcook.com\/blog\/category\/statistics\/"},{"label":"Forensic accounting in Python","link":"https:\/\/www.johndcook.com\/blog\/2026\/07\/21\/forensic-accounting-in-python\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=247395"}],"version-history":[{"count":6,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247395\/revisions"}],"predecessor-version":[{"id":247406,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/247395\/revisions\/247406"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=247395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=247395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=247395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}]