
    
        
        
        
                
        
        
        
                
        
        
        
                
    
        
        
                
        
        
        
                
    
        
        
                
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/modular-arithmetic","feed_url":"https:\/\/mathspp.com\/blog\/tags\/modular-arithmetic.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"Reverse-engineering the \u201cChronospatial Computer\u201d","date_published":"2024-12-21T09:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/reverse-engineering-the-chronospatial-computer","url":"https:\/\/mathspp.com\/blog\/reverse-engineering-the-chronospatial-computer","content_html":"<p>Reverse-engineering the program from &ldquo;Chronospatial Computer&rdquo;, day 17 of Advent of Code 2024.<\/p>\n\n<p>The &ldquo;Chronospatial Computer&rdquo; is from <a href=\"https:\/\/adventofcode.com\/2024\/day\/17\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Advent of Code 2024, day 17<\/a>, a problem that entertained me for a couple of hours.<\/p>\n<h2 id=\"parsing-the-input\">Parsing the input<a href=\"#parsing-the-input\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>My input file looked like this:<\/p>\n<pre><code class=\"language-txt\">Register A: 64012472\nRegister B: 0\nRegister C: 0\n\nProgram: 2,4,1,7,7,5,0,3,1,7,4,1,5,5,3,0<\/code><\/pre>\n<p>To read the input and parse it I used a context manager and a couple of calls to <code>readline<\/code>:<\/p>\n<pre><code class=\"language-py\">with open(\"input.txt\", \"r\") as f:\n    register_a = int(f.readline().split()[-1])\n    register_b = int(f.readline().split()[-1])\n    register_c = int(f.readline().split()[-1])\n    _ = f.readline()\n    program = [int(num) for num in f.readline().split()[-1].split(\",\")]\n\nprint(program, register_a, register_b, register_c)<\/code><\/pre>\n<h2 id=\"solving-part-1-with-functional-programming\">Solving part 1 with functional programming<a href=\"#solving-part-1-with-functional-programming\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Part 1 required me to simulate a series of simple instructions that operate on three registers.\nWhen I read the problem statement, I decided I wanted to use some ideas from functional programming.\nSo, what I did was to separate each operator (there are 8) into three parts:<\/p>\n<ol><li>the part that performs some computation with the registers and\/or with the operand;<\/li>\n<li>the part that updates the state of the program, maybe by updating a register or by outputting a value; and<\/li>\n<li>the part that updates the pointer of the program, which controls the instruction that will run next.<\/li>\n<\/ol><p>By using <code>lambda<\/code> functions, <a href=\"\/blog\/pydonts\/dunder-methods\">dunder methods<\/a>, and <a href=\"\/blog\/functools-partial\">currying with <code>functools.partial<\/code><\/a>, each list below represents one of the three parts of each opcode.<\/p>\n<p>First, the computation part of each operation:<\/p>\n<pre><code class=\"language-py\">registers = [0, 1, 2, 3, register_A, register_B, register_C]\nA, B, C = 4, 5, 6  # Indices of the named registers in the list `registers`.\n\ncomputations = [\n    lambda o: registers[A] \/\/ pow(2, registers[o]),  # ADV\n    lambda o: registers[B] ^ o,                      # BXL\n    lambda o: registers[o] % 8,                      # BST\n    lambda o: ...,                                   # JNZ\n    lambda o: registers[B] ^ registers[C],           # BXC\n    lambda o: registers[o] % 8,                      # OUT\n    lambda o: registers[A] \/\/ pow(2, registers[o]),  # BDV\n    lambda o: registers[A] \/\/ pow(2, registers[o]),  # CDV\n]<\/code><\/pre>\n<p>In the lambda functions above, when we use <code>o<\/code> in isolation, we're using the operand as a literal operand, whereas the list <code>registers<\/code> maps an operand into its combo operand.\nBy using this list, we can map the numbers 0 through 3 to themselves and the indices 4, 5, and 6, to the registers A, B, and C, respectively, without having to use a conditional statement.<\/p>\n<p>The operation <code>JNZ<\/code> has a lambda function that does nothing because there is no proper computation for this operator.<\/p>\n<p>Then, I wrote a list with all the functions that update the state of the program:<\/p>\n<pre><code class=\"language-py\">from functools import partial\n\noutput = []\nstate_updates = [\n    partial(registers.__setitem__, A),\n    partial(registers.__setitem__, B),\n    partial(registers.__setitem__, B),\n    lambda v: ...,\n    partial(registers.__setitem__, B),\n    output.append,\n    partial(registers.__setitem__, B),\n    partial(registers.__setitem__, C),\n]<\/code><\/pre>\n<p>This uses the <a href=\"\/blog\/pydonts\/dunder-methods\">dunder method <code>__setitem__<\/code><\/a> and <a href=\"\/blog\/functools-partial\">the function <code>functools.partial<\/code><\/a> to create a function that accepts a single value and that writes that value to the correct register in the list <code>registers<\/code>.<\/p>\n<p>Finally, all operators move the program pointer by two positions except the operator <code>JNZ...<\/code><\/p>","summary":"Reverse-engineering the program from \u201cChronospatial Computer\u201d, day 17 of Advent of Code 2024.","date_modified":"2025-07-23T16:49:02+02:00","tags":["algorithms","mathematics","modular arithmetic","programming","python","recursion"],"image":"\/user\/pages\/02.blog\/reverse-engineering-the-chronospatial-computer\/thumbnail.webp"},{"title":"Base conversion in Python","date_published":"2024-01-07T10:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/base-conversion-in-python","url":"https:\/\/mathspp.com\/blog\/base-conversion-in-python","content_html":"<p>This article shows how to do base conversions in Python with the built-in int, how to write integer literals in other bases, and how to do base conversions in general.<\/p>\n\n<link rel=\"stylesheet\" href=\"https:\/\/unpkg.com\/@antonz\/codapi@0.12.2\/dist\/snippet.css\"><script async src=\"https:\/\/unpkg.com\/@antonz\/codapi@0.12.2\/dist\/snippet.js\"><\/script><p>In this article I'll assume you know what a number base is and how there are multiple number bases.\nMy focus will be on showing you the tools that Python provides to work with multiple number bases, an in particular with the binary, octal, decimal, and hexadecimal, number bases.<\/p>\n<h2 id=\"bases-change-the-representation-not-the-number\">Bases change the representation, not the number<a href=\"#bases-change-the-representation-not-the-number\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Before diving right into the tools that Python gives you to work with different number bases I want to stress that a number base doesn't change the underlying number we're talking about, only its <em>representation<\/em>.<\/p>\n<p>For example, many people around the world have three meals per day: breakfast, lunch, and dinner.\nIt doesn't matter whether I write &ldquo;three&rdquo; in English, &ldquo;tr&ecirc;s&rdquo; in Portuguese, &ldquo;3&rdquo; in the decimal number base, or &ldquo;11&rdquo; in the binary number base.\nWe're always talking about the same number of meals: breakfast, lunch, and dinner.<\/p>\n<p>This is very important!<\/p>\n<h2 id=\"built-in-bases-for-integer-literals\">Built-in bases for integer literals<a href=\"#built-in-bases-for-integer-literals\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Python (and most programming languages) let you write integer literals in the decimal number base, and we typically don't even think about it.\nPython also lets you write integer literals in three other bases:<\/p>\n<ol><li>binary;<\/li>\n<li>octal; and<\/li>\n<li>hexadecimal.<\/li>\n<\/ol><p>To write an integer literal in any base other than the decimal base, you always start your integer literal with a <code>0<\/code> followed by the letter that identifies the base.\nThis is summarised in the table below:<\/p>\n<table><thead><tr><th style=\"text-align: left;\">Base<\/th>\n<th style=\"text-align: left;\">Prefix<\/th>\n<\/tr><\/thead><tbody><tr><td style=\"text-align: left;\">binary<\/td>\n<td style=\"text-align: left;\"><code>0b<\/code><\/td>\n<\/tr><tr><td style=\"text-align: left;\">octal<\/td>\n<td style=\"text-align: left;\"><code>0o<\/code><\/td>\n<\/tr><tr><td style=\"text-align: left;\">hexadecimal<\/td>\n<td style=\"text-align: left;\"><code>0x<\/code><\/td>\n<\/tr><\/tbody><\/table><p>Thus, all four assignments below create the same integer literal:<\/p>\n<pre><code class=\"language-py\">svty_three = 73\nsvty_three_bin = 0b1001001\nsvty_three_oct = 0o111\nsvty_three_hex = 0x49<\/code><\/pre>\n<codapi-snippet sandbox=\"python\" editor=\"none\" id=\"variable_assignment\"><\/codapi-snippet><p>Because <a href=\"#bases-change-the-representation-not-the-number\">the base changes the representation but not the number<\/a>, printing any of the four variables will print <code>73<\/code>:<\/p>\n<pre><code class=\"language-py\">print(svty_three)  # 73\nprint(svty_three_bin)  # 73\nprint(svty_three_oct)  # 73\nprint(svty_three_hex)  # 73<\/code><\/pre>\n<codapi-snippet sandbox=\"python\" editor=\"basic\" depends-on=\"variable_assignment\"><\/codapi-snippet><p>In any of these bases, you can <a href=\"\/blog\/pydonts\/usages-of-underscore\">use the underscore <code>_<\/code> to group digits to make the literals more readable<\/a>.\nFor example, in the decimal base you can use the underscore <code>_<\/code> as the thousands separator:<\/p>\n<pre><code class=\"language-py\">huge_number = 17_532_546_253_000_000<\/code><\/pre>\n<h2 id=\"built-in-functions-for-base-conversion\">Built-in functions for base conversion<a href=\"#built-in-functions-for-base-conversion\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Python contains 3 built-ins that let you convert integers to <em>string<\/em> representations in the three other bases:<\/p>\n<table><thead><tr><th style=\"text-align: left;\">Base<\/th>\n<th style=\"text-align: left;\">Built-in<\/th>\n<\/tr><\/thead><tbody><tr><td style=\"text-align: left;\">binary<\/td>\n<td style=\"text-align: left;\"><code>bin<\/code><\/td>\n<\/tr><tr><td style=\"text-align: left;\">octal<\/td>\n<td style=\"text-align: left;\"><code>oct<\/code><\/td>\n<\/tr><tr><td style=\"text-align: left;\">hexadecimal<\/td>\n<td style=\"text-align: left;\"><code>hex<\/code><\/td>\n<\/tr><\/tbody><\/table><p>Here's example usages of all three:<\/p>\n<pre><code class=\"language-py\">print(bin(73))  # 0b1001001\nprint(oct(73))  # 0o111\nprint(hex(73))  # 0x49<\/code><\/pre>\n<codapi-snippet sandbox=\"python\" editor=\"basic\"><\/codapi-snippet><p>Notice that these converting functions include the base prefix in the converted representation!<\/p>\n<p>If you want to use these string representations to represent the number in its base, you can discard the prefix and then convert each digit.\nThe code below uses <a href=\"\/blog\/pydonts\/list-comprehensions-101\">a list comprehension<\/a> and the built-in <code>bin<\/code> to convert an integer to a list of its binary digits:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; [int(digit) for digit in bin(73)[2:]]\n[1, 0, 0, 1, 0, 0, 1]<\/code><\/pre>\n<h2 id=\"string-formatting-in-binary-octal-and-hexadecimal\">String formatting in binary, octal, and hexadecimal<a href=\"#string-formatting-in-binary-octal-and-hexadecimal\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The prefix letters for the integer literals can also be used as...<\/p>","summary":"This article shows how to do base conversions in Python with the built-in int, how to write integer literals in other bases, and how to do base conversions in general.","date_modified":"2025-07-23T16:49:02+02:00","tags":["binary","mathematics","modular arithmetic","programming","python"],"image":"\/user\/pages\/02.blog\/base-conversion-in-python\/thumbnail.webp"},{"title":"Problem #027 - pile of coconuts \ud83e\udd65","date_published":"2021-01-10T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/pile-of-coconuts","url":"https:\/\/mathspp.com\/blog\/problems\/pile-of-coconuts","content_html":"<p>Five sailors and their monkey were washed ashore on a desert island.\nThey decide to go get coconuts that they pile up.\nDuring the night, each of the sailors, suspicious the others wouldn't behave fairly,\nwent to the pile of coconuts take their fair share.\nHow many coconuts were there in the beginning..?<\/p>\n\n<p><img alt='A photograph of some coconuts, courtesy of user \"zibik\" from unsplash.com.' src=\"\/images\/5\/9\/b\/1\/8\/59b188dc180d840fce0c5d6e49864c9341c57563-thumbnail.jpg\"><\/p>\n<h2 id=\"problem-statement\">Problem statement<a href=\"#problem-statement\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>A group of five sailors and a monkey wash ashore on a desert island.\nAfter the initial shock subsided, they decided the best thing to do was to gather\nsupplies for the upcoming days.\nThey split up and when they got back together, the only thing they managed\nto find was coconuts, that they piled up, before deciding to go get some sleep.<\/p>\n<p>In the middle of the night, the first sailor -- who was suspicious the others wouldn't\nbe fair in the division of the coconuts -- went to the pile of coconuts to divide it\nevenly in five and get their fair share.\nWhen the sailor divided the pile, they realised there was an extra coconut that could\nnot go in any pile, so they gave it to the monkey, and then took a fifth of the\nremaining coconuts and went to sleep.<\/p>\n<p>A while later, the second sailor did the same.\nThey went to the coconut pile and divided it evenly in five parts, only to find out\nthere was one coconut too many.\nThe sailor gave that coconut to the monkey, took a fifth of the remaining coconuts and\nthen went to bed.<\/p>\n<p>This happened for the other three sailors as well: all of them woke up, went to the\ncoconut pile and divided it in five, gave a coconut in excess to the monkey and then\ntook exactly one fifth of the remaining coconuts before heading back to sleep.<\/p>\n<p>What is the minimum number of coconuts that there had to be in the pile in order\nfor this to be possible?<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought...<\/p>\n<\/div>\n<p>If you need any clarification whatsoever, feel free to ask in the comment section below.<\/p>\n<h2 id=\"solution\">Solution<a href=\"#solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The problem of determining the amount of coconuts that the sailors gathered\ncan be tackled with a bit of modular arithmetic.<\/p>\n<p>Let us say that after all sailors fiddled with the pile, there were <span class=\"mathjax mathjax--inline\">\\(x\\)<\/span> coconuts left.\nWe will use <span class=\"mathjax mathjax--inline\">\\(x\\)<\/span> to write an expression for the number of coconuts originally available,\nand then we solve for <span class=\"mathjax mathjax--inline\">\\(x\\)<\/span>.<\/p>\n<p>Recall that the fifth sailor gave a coconut to the monkey and took a fifth of the\ncoconuts available, leaving the pile with exactly <span class=\"mathjax mathjax--inline\">\\(x\\)<\/span> coconuts.\nSo, when the fifth sailor got to the pile, the pile had exactly these many coconuts:<\/p>\n<p class=\"mathjax mathjax--block\">\\[\n\\frac54 x + 1 ~ .\\]<\/p>\n<p>Repeating the process of multiplying by <span class=\"mathjax mathjax--inline\">\\(\\frac54\\)<\/span> and adding <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span>,\nwe see that the pile had<\/p>\n<p class=\"mathjax mathjax--block\">\\[\n\\frac54\\left(\\frac54 x + 1\\right)  + 1 = \\frac{25}{16}x + \\frac94\\]<\/p>\n<p>coconuts when the fourth sailor got to the pile, which means that when the third\nsailor got to the pile, it had<\/p>\n<p class=\"mathjax mathjax--block\">\\[...<\/p>","summary":"In this problem, we calculate how many coconuts a group of sailors found in a desert island.","date_modified":"2026-05-01T21:10:36+02:00","tags":["mathematics","modular arithmetic"],"image":"\/user\/pages\/02.blog\/03.problems\/p027-pile-of-coconuts\/thumbnail.jpg"},{"title":"Problem #018 - circle of hats","date_published":"2020-05-24T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/problems\/circle-of-hats","url":"https:\/\/mathspp.com\/blog\/problems\/circle-of-hats","content_html":"<p><span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> mathematicians with numbered party hats gather around in a circle... It is a matter of life or death!<\/p>\n\n<p><img alt=\"00-hats.jpg\" src=\"\/images\/9\/8\/9\/6\/d\/9896da82daf89716d24d8c3d5b530a693eb6b530-00-hats.jpg\"><\/p>\n<h2 id=\"foreword\">Foreword<a href=\"#foreword\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>I was challenged to solve this problem by Roger Hui, who wrote about it in [an article][roger-article] a couple of years ago.<\/p>\n<h2 id=\"problem-statement\">Problem statement<a href=\"#problem-statement\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Assume <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> mathematicians are in a circle, each mathematician with a hat in its head and facing the other <span class=\"mathjax mathjax--inline\">\\(n-1\\)<\/span> mathematicians. Each hat will be given a number from <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> to <span class=\"mathjax mathjax--inline\">\\(n-1\\)<\/span> and every mathematician will be able to see the numbers on the hats of all the other mathematicians. Of course no one will be able to see\/know its own number. (In case you haven't understood yet, numbers can show up repeated.)<\/p>\n<p>After some time, all mathematicians will write down, at the same time, a guess for the number on their own hat. If there is at least one person guessing it right, everyone lives. If no one guesses correctly, everyone dies!<\/p>\n<p>Your task is to find out what is the strategy that the mathematicians must employ so that they are sure to live through this ordeal. The mathematicians can discuss the strategy before receiving the numbers but after that they must remain silent and won't be able to communicate with each other.<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought...<\/p>\n<\/div>\n<p>If you need any clarification whatsoever, feel free to ask in the comment section below.<\/p>\n<h2 id=\"solution\">Solution<a href=\"#solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The answer is really interesting and can be formulated in mathematical terms in a rather simple way; after that I will walk you through what the mathematical formulation means with some doodles I drew.<\/p>\n<p>Let <span class=\"mathjax mathjax--inline\">\\(a_i\\)<\/span> be the number in the hat of the <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span>th mathematician and <span class=\"mathjax mathjax--inline\">\\(g_i\\)<\/span> be the guess the <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span>th mathematician writes down. Then <span class=\"mathjax mathjax--inline\">\\(g_i\\)<\/span> satisfies the equation<\/p>\n<p><span class=\"mathjax mathjax--inline\">\\(g_i + \\sum_{j \\neq i} a_j \\equiv i \\hspace{0.5cm} \\text{mod } n\\)<\/span><\/p>\n<p>What follows from here is that if <span class=\"mathjax mathjax--inline\">\\(k \\equiv \\sum_i a_i \\text{ mod } n\\)<\/span>, then the <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span>th mathematician will get its guess right and all other mathematicians will fail.<\/p>\n<h3 id=\"why-it-works-mathematically\">Why it works (mathematically)<a href=\"#why-it-works-mathematically\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>Let <span class=\"mathjax mathjax--inline\">\\(a_i, g_i\\)<\/span> be as above and let <span class=\"mathjax mathjax--inline\">\\(k = \\sum_i a_i \\text{ mod } n\\)<\/span>. Notice how<\/p>\n<p><span class=\"mathjax mathjax--inline\">\\(g_k + \\sum_{j \\neq k} a_j \\equiv k \\iff g_k \\equiv k - \\sum_{j \\neq k} a_j.\\)<\/span><\/p>\n<p>Of course, we defined <span class=\"mathjax mathjax--inline\">\\(k = \\sum_i a_i\\)<\/span> so the above becomes<\/p>\n<p><span class=\"mathjax mathjax--inline\">\\(g_k = \\sum_{j} a_j - \\sum_{j \\neq k} a_j = a_k \\hspace{0.5cm} \\text{mod } n,\\)<\/span><\/p>\n<p>hence the <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span>th mathematician will guess correctly.<\/p>\n<h3 id=\"explanation-of-the-solution\">Explanation of the solution<a href=\"#explanation-of-the-solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>Let us take <span class=\"mathjax mathjax--inline\">\\(n = 5\\)<\/span> and number the mathematicians from <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> to <span class=\"mathjax mathjax--inline\">\\(4\\)<\/span>, starting from the top and going in the clockwise direction. The numbers inside the circles represent the numbers in the hats, that I distributed <a href=\"https:\/\/xkcd.com\/221\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">randomly<\/a>.<\/p>\n<p><img alt=\"hat-configuration.webp\" src=\"\/user\/pages\/02.blog\/03.problems\/p018-circle-of-hats\/hat-configuration.webp\"><\/p>\n<p>Now we pretend we are the mathematician number <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>, and so we see the numbers <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span>, which give <span class=\"mathjax mathjax--inline\">\\(4\\)<\/span> when added up. Now, we are the mathematician <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> so our guess <span class=\"mathjax mathjax--inline\">\\(g_0\\)<\/span> is how much is left...<\/p>","date_modified":"2025-07-23T16:49:02+02:00","tags":["mathematics","modular arithmetic","apl"],"image":"\/user\/pages\/02.blog\/03.problems\/p018-circle-of-hats\/00-hats.jpg"},{"title":"The formula that plots itself","date_published":"2019-04-24T15:32:00+02:00","id":"https:\/\/mathspp.com\/blog\/the-formula-that-plots-itself","url":"https:\/\/mathspp.com\/blog\/the-formula-that-plots-itself","content_html":"<p>This post gives you the code to mess around with \"Tupper's self-referential formula\", a formula that plots itself.<\/p>\n\n<p>By the end of this blog post, I hope that you know how to make mathematical drawings and why the number<\/p>\n<p class=\"mathjax mathjax--block\">\\[\nN \\approx 4.85845063618971342358209596 \\times 10^{543}\\]<\/p>\n<p>is so special.<\/p>\n<p>Given a function <span class=\"mathjax mathjax--inline\">\\(f(x, y)\\)<\/span>, how can you use it to make a drawing? Well, we just imagine the whole plane as a white, clean grid, and then we fill with black the squares at the positions <span class=\"mathjax mathjax--inline\">\\((x,y)\\)<\/span> such that <span class=\"mathjax mathjax--inline\">\\(f(x,y) &gt; \\frac{1}{2}\\)<\/span>. In a way, it is as if the function <span class=\"mathjax mathjax--inline\">\\(f\\)<\/span> is telling us whether to use white or black, i.e., to leave the square empty (<span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>) or filled in (<span class=\"mathjax mathjax--inline\">\\(1\\)<\/span>).<\/p>\n<p>(More rigorously, we divide the plane into unit squares and assign each square the coordinates of its lower-left corner.)<\/p>\n<p>If we take, for example, <span class=\"mathjax mathjax--inline\">\\(f(x, y) = x + y\\)<\/span>, then square <span class=\"mathjax mathjax--inline\">\\((0,0)\\)<\/span> would be white because <span class=\"mathjax mathjax--inline\">\\(f(0, 0) = 0 &lt; \\frac{1}{2}\\)<\/span> but the squares <span class=\"mathjax mathjax--inline\">\\((0, 1)\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\((1, 0)\\)<\/span> would be black because <span class=\"mathjax mathjax--inline\">\\(f(0, 1) = f(1, 0) = 1 &gt; \\frac{1}{2}\\)<\/span>.<\/p>\n<p>As another example, take <span class=\"mathjax mathjax--inline\">\\(f\\)<\/span> to be this function:<\/p>\n<p class=\"mathjax mathjax--block\">\\[\nf(x, y) = \\left\\lfloor \\text{mod}\\left(\\left\\lfloor\\frac{y}{17} \\right\\rfloor 2^{-17\\lfloor x \\rfloor -  \\text{mod}(\\lfloor y \\rfloor, 17)}, 2\\right) \\right\\rfloor\\]<\/p>\n<p>where <span class=\"mathjax mathjax--inline\">\\(\\lfloor n \\rfloor\\)<\/span> denotes the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Floor_and_ceiling_functions\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">floor function<\/a> and <span class=\"mathjax mathjax--inline\">\\(\\text{mod}(a, b)\\)<\/span> denotes the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Modulo_operation\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">modulo operator<\/a>. This function looks way more interesting, doesn't it? Yes, it does! And if you look in the right place, this is what is plotted by this function:<\/p>\n<figure class=\"image-caption\"><img title=\"The formula, drawn.\" alt=\"A plot showing the Tupper formula written in black in a pixelated font.\" src=\"\/user\/pages\/02.blog\/the-formula-that-plots-itself\/_tupper_formula.webp\"><figcaption class=\"\">The formula, drawn.<\/figcaption><\/figure><p>What is going on here..? The function I just showed you, called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Tupper's_self-referential_formula\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Tupper's self-referential formula<\/a>, is a formula that plots itself! But you might be suspicious because I said <em>if you look in the <strong>right<\/strong> place<\/em>. What is this <em>place<\/em> then?<\/p>\n<p>Quite simply, define<\/p>\n<p class=\"mathjax mathjax--block\">\\[\nN=4858450636189713423582095962494202044581400587983244549483093085061934704708809928450644769865524364849997247024915119110411605739177407856919754326571855442057210445735883681829823754139634338225199452191651284348332905131193199953502413758765239264874613394906870130562295813219481113685339535565290850023875092856892694555974281546386510730049106723058933586052544096664351265349363643957125565695936815184334857605626529915320311182856118951164066401426579727262440270286409619368825536815027849325692956320299303330714849102058741137034502,\\]<\/p>\n<p>(a 544-digit-long number). Then, the image I showed you is the rectangular area with the lower-left corner <span class=\"mathjax mathjax--inline\">\\((0, N)\\)<\/span> and the upper-right corner <span class=\"mathjax mathjax--inline\">\\((105, N + 16)\\)<\/span> (so it is a <span class=\"mathjax mathjax--inline\">\\(106 \\times 17\\)<\/span> rectangle). Quite impressive, right? Self-references are always fun!<\/p>\n<p>But that is not all... If you look hard enough, you can find literally anything inside that <span class=\"mathjax mathjax--inline\">\\(106\\times17\\)<\/span> rectangle! For example, taking<\/p>\n<p class=\"mathjax mathjax--block\">\\[\nN=677269797063266389145771001639366162904443300634759368354244105189144417475687924080590138582401925400953401198762670070868017028632609067495842127259345485889052110555312844858658969250766978033911456684637024394115209279287448522343527514061700072005928325124098808483476326307953390156875355289624192978628506335125351370018499785193486797521350328711540234844414805471938182060305235921541912512179523099720166772353828125144439537587189530425493554812515912471900753848603802337280,\\]<\/p>\n<p>you can find the name of this site \"mathspp\" and a winking face \";)\":<\/p>\n<figure class=\"image-caption\"><img title=\"mathspp ;)\" alt='The text \"mathspp ;)\" drawn in black in a pixelated font.' src=\"\/user\/pages\/02.blog\/the-formula-that-plots-itself\/_tupper_mathspp.webp\"><figcaption class=\"\">mathspp ;)<\/figcaption><\/figure><p>Now the really important question is... How does one find such values for <span class=\"mathjax mathjax--inline\">\\(N\\)<\/span>? Well, you can watch <a href=\"https:\/\/www.youtube.com\/watch?v=_s5RFgd59ao\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this Numberphile YouTube video<\/a>, or I can tell you all about it...<\/p>\n<p>You start with a <span class=\"mathjax mathjax--inline\">\\(106\\times17\\)<\/span> grid and you colour it (with black\/white) the way you want. Then you will construct a bit sequence: you start on the top-right corner, and write down a <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span> if that square is black, <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> if that square is white. Then you go down one square, and repeat. You do this column by column, top to bottom, right to left. When you finish, you convert your bit sequence...<\/p>","summary":"This post gives you the code to mess around with \u201cTupper&#039;s self-referential formula\u201d, a formula that plots itself.","date_modified":"2025-07-23T16:49:02+02:00","tags":["binary","mathematics","modular arithmetic","programming","pygame","python","recursion","visualisation"],"image":"\/user\/pages\/02.blog\/the-formula-that-plots-itself\/thumbnail.webp"},{"title":"Problem #007 - binary multiples","date_published":"2018-02-26T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/binary-multiples","url":"https:\/\/mathspp.com\/blog\/problems\/binary-multiples","content_html":"<p>Is it true that every integer you can think of has a multiple written out only with <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>s and <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span>s?<\/p>\n\n<p><img alt=\"A screenshot of a black screen with some white 0s and 1s\" src=\"\/images\/5\/4\/5\/9\/7\/54597ea74d900fc21370f3f827ace7baac45d168-binaryimg.jpg\"><\/p>\n<h2 id=\"problem-statement\">Problem statement<a href=\"#problem-statement\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Let <span class=\"mathjax mathjax--inline\">\\(k \\in \\mathbb{Z}\\)<\/span> be an integer. Is there an integer <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> such that <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> is a multiple of <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> only has <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>s and <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span>s in its decimal expansion?<\/p>\n<p>As an example, if <span class=\"mathjax mathjax--inline\">\\(k = 2\\)<\/span> we could have <span class=\"mathjax mathjax--inline\">\\(n = 10\\)<\/span>.<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought...<\/p>\n<\/div>\n<p>If you need any clarification whatsoever, feel free to ask in the comment section below.<\/p>\n<h2 id=\"solution\">Solution<a href=\"#solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The answer is <em>yes<\/em>, any integer <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> has a \"binary multiple\" <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span>. To show this is true, we will build <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> starting from <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span>.<\/p>\n<p>Assume <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> is positive, and consider the following <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> integers:<\/p>\n<p class=\"mathjax mathjax--block\">\\[\n    \\big\\{ 1, 11, 111, \\cdots, \\underbrace{1\\cdots 1}_{k\\ 1\\text{s}} \\big\\}\\]<\/p>\n<p>(which can be formally written out as taking <span class=\"mathjax mathjax--inline\">\\(\\{c_i\\}_{i = 1}^k\\)<\/span> with <span class=\"mathjax mathjax--inline\">\\(c_1 = 1\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(c_{i+1} = 10*c_i + 1\\)<\/span>).<\/p>\n<p>Then only one of two things can happen. Either one of <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span> is a multiple of <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> (in which case all is good) or not. But if no <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span> is a multiple of <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span>, then we can consider the remainders of the <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span> modulo <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span>:<\/p>\n<p class=\"mathjax mathjax--block\">\\[\n    \\{ c_1\\ \\text{mod}\\ k, c_2\\ \\text{mod}\\ k, \\cdots, c_k\\ \\text{mod}\\ k \\} \\subseteq \\{ 1, \\cdots, k - 1 \\}\\]<\/p>\n<p>We say that the remainders of the <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span> are contained in the set to the right because none of the remainders is <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>, otherwise one of the <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span> would be a multiple of <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span>.<\/p>\n<p>Notice the left-hand set is built by taking the remainders of the <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> different <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span> but the right-hand set only has <span class=\"mathjax mathjax--inline\">\\(k - 1\\)<\/span> elements. The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pigeonhole_principle\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">pigeonhole principle<\/a> then says that there are at least two different <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(c_j\\)<\/span> being mapped to the same element in the right-hand set, i.e. <span class=\"mathjax mathjax--inline\">\\(c_i \\equiv c_j \\ \\text{mod}\\ k\\)<\/span>. Assume we have <span class=\"mathjax mathjax--inline\">\\(j &gt; i\\)<\/span>, meaning <span class=\"mathjax mathjax--inline\">\\(c_j &gt; c_i\\)<\/span> and, in particular:<\/p>\n<p class=\"mathjax mathjax--block\">\\[\n    \\begin{cases}\n        c_j - c_i \\equiv 0\\ \\text{mod}\\ k \\\\\n        c_j - c_i = \\underbrace{1\\cdots 1}_{j-i\\ 1\\text{s}} \\underbrace{0\\cdots 0}_{i\\ 0\\text{s}}\n    \\end{cases}\\]<\/p>\n<p>Thus <span class=\"mathjax mathjax--inline\">\\(n = c_j - c_i\\)<\/span> is a \"binary multiple\" of <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span>.<\/p>\n<p>If <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> is negative, we repeat the above for <span class=\"mathjax mathjax--inline\">\\(-k\\)<\/span>. If <span class=\"mathjax mathjax--inline\">\\(k = 0\\)<\/span>, then <span class=\"mathjax mathjax--inline\">\\(n = 0\\)<\/span>.<\/p>\n<h3 id=\"example\">Example<a href=\"#example\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>If <span class=\"mathjax mathjax--inline\">\\(k = 4\\)<\/span> we consider <span class=\"mathjax mathjax--inline\">\\(c_1 = 1\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(c_2 = 11\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(c_3 = 111\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(c_4 = 1111\\)<\/span> and realize none of these numbers is a multiple of <span class=\"mathjax mathjax--inline\">\\(4\\)<\/span>.<\/p>\n<p>Now we take the remainders:<\/p>\n<p class=\"mathjax mathjax--block\">\\[\n    \\begin{cases}\n        1 \\equiv 1\\ \\text{mod}\\ 4 \\\\\n        11 \\equiv 3\\ \\text{mod}\\ 4 \\\\\n        111 \\equiv 3\\ \\text{mod}\\ 4 \\\\\n        1111 \\equiv 3\\ \\text{mod}\\ 4\n    \\end{cases}\\]<\/p>\n<p>and see that, for example, <span class=\"mathjax mathjax--inline\">\\(c_3 \\equiv c_2\\ \\text{mod}\\ 4\\)<\/span>, implying that <span class=\"mathjax mathjax--inline\">\\(c_3 - c_2 = 100 \\equiv 0\\ \\text{mod}\\ 4\\)<\/span>.<\/p>","date_modified":"2025-07-23T16:49:02+02:00","tags":["mathematics","modular arithmetic","pigeonhole principle"],"image":"\/user\/pages\/02.blog\/03.problems\/p007-binary-multiples\/binary_img.jpg"},{"title":"Problem #004 - solvability of the water buckets","date_published":"2017-11-22T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/solvability-water-buckets","url":"https:\/\/mathspp.com\/blog\/problems\/solvability-water-buckets","content_html":"<!-- v -->\n<p>In <a href=\"\/blog\/water-buckets\">this post<\/a> I talked about the riddle of the water buckets. Now I challenge you to prove that in some situations it is <em>impossible<\/em> to solve it!<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Nils Schirmer on Unsplash\" alt=\"A grayscale image with 3 buckets\" src=\"\/images\/9\/1\/f\/3\/6\/91f36efb87d56de14102dd77ae7037c7d1669d9b-buckets.jpg\"><figcaption class=\"\">Photo by Nils Schirmer on Unsplash<\/figcaption><\/figure><!-- ^ --><h2 id=\"problem-statement\">Problem statement<a href=\"#problem-statement\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>You have <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> buckets, each bucket with capacity for <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span> litres of water, <span class=\"mathjax mathjax--inline\">\\(i = 1, \\cdots, n\\)<\/span>. You want to manipulate the buckets in such a way that one of them holds exactly <span class=\"mathjax mathjax--inline\">\\(t\\)<\/span> litres of water, knowing that the only moves you can do are:<\/p>\n<ul><li>completely fill up bucket <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> so that it holds <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span> litres of water;<\/li>\n<li>completely empty bucket <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> so that it now holds <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> litres of water;<\/li>\n<li>move water from bucket <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> to bucket <span class=\"mathjax mathjax--inline\">\\(j\\)<\/span>, until bucket <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> becomes empty or bucket <span class=\"mathjax mathjax--inline\">\\(j\\)<\/span> becomes full, whatever happens first.<\/li>\n<\/ul><p>Prove that, if <span class=\"mathjax mathjax--inline\">\\(t\\)<\/span> is not a multiple of the greatest common divisor of the <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(i = 1, \\cdots, n\\)<\/span> then it is impossible for a single bucket to hold exactly <span class=\"mathjax mathjax--inline\">\\(t\\)<\/span> litres of water.<\/p>\n<p>For example, if the buckets have capacities <span class=\"mathjax mathjax--inline\">\\(4\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(6\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(t = 3\\)<\/span>, then you can't perform the moves above to get exactly <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span> litres of water into one of the two buckets as the greatest common divisor of <span class=\"mathjax mathjax--inline\">\\(4\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(6\\)<\/span> is <span class=\"mathjax mathjax--inline\">\\(\\gcd(4, 6) = 2\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span> is <em>not<\/em> a multiple of <span class=\"mathjax mathjax--inline\">\\(2\\)<\/span>.<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought... and give it an actual shot! Take out a piece of paper and a pencil and get your brain working!<\/p>\n<\/div>\n<p>If you need any clarification whatsoever, feel free to ask in the comment section below.<\/p>\n<h2 id=\"solution\">Solution<a href=\"#solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>A possible solution is to consider a clever invariant that applies to the amount of water that each bucket is holding at any point in time. To make this easier, let's call <span class=\"mathjax mathjax--inline\">\\(d\\)<\/span> to the greatest common divisor of the <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(i = 1, \\cdots, n\\)<\/span> (<span class=\"mathjax mathjax--inline\">\\(d = \\gcd(c_1, \\cdots, c_n)\\)<\/span>). Let's also say the amount of water in bucket <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> is <span class=\"mathjax mathjax--inline\">\\(w_i\\)<\/span>. We will show that, regardless of the moves we make, <span class=\"mathjax mathjax--inline\">\\(w_i\\)<\/span> is <em>always<\/em> a multiple of <span class=\"mathjax mathjax--inline\">\\(d\\)<\/span> for all <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> (which we write <span class=\"mathjax mathjax--inline\">\\(d | w_i\\)<\/span> for _\"<span class=\"mathjax mathjax--inline\">\\(d\\)<\/span> divides <span class=\"mathjax mathjax--inline\">\\(w_i\\)<\/span>\"_).<\/p>\n<p>At the start all buckets are empty, so <span class=\"mathjax mathjax--inline\">\\(w_1 = \\cdots = w_n = 0\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> is a multiple of <span class=\"mathjax mathjax--inline\">\\(d\\)<\/span> so that is that. Now we show that the three moves above preserve this property that <span class=\"mathjax mathjax--inline\">\\(d | w_i\\ \\forall i\\)<\/span>.<\/p>\n<ul><li>Emptying bucket <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span>: this means <span class=\"mathjax mathjax--inline\">\\(w_i = 0\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(d | 0\\)<\/span> so everything is good;<\/li>\n<li>Filling bucket <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span>: this means <span class=\"mathjax mathjax--inline\">\\(w_i = c_i\\)<\/span> but, by definition, <span class=\"mathjax mathjax--inline\">\\(d\\)<\/span> is <em>a<\/em> divisor of <span class=\"mathjax mathjax--inline\">\\(c_i\\)<\/span> so certainly we have <span class=\"mathjax mathjax--inline\">\\(d | c_i\\)<\/span>;<\/li>\n<li>Moving water from bucket <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> to bucket <span class=\"mathjax mathjax--inline\">\\(j\\)<\/span> until either bucket <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> becomes empty or bucket <span class=\"mathjax mathjax--inline\">\\(j\\)<\/span> becomes full, whatever happens first: before we move water around we have that <span class=\"mathjax mathjax--inline\">\\(d | w_i\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(d | w_j\\)<\/span> so we can say that <span class=\"mathjax mathjax--inline\">\\(w_i = k_i d\\)<\/span> and...<\/li><\/ul>","summary":"This blog post contains a problem related to the riddle of the water buckets and a setting in which it is impossible to solve.","date_modified":"2025-07-23T16:49:02+02:00","tags":["invariants","mathematics","modular arithmetic"],"image":"\/user\/pages\/02.blog\/03.problems\/p004-solvability-of-the-water-buckets\/buckets.jpg"}]}
