
    
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/binary","feed_url":"https:\/\/mathspp.com\/blog\/tags\/binary.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":"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 #058 \u2013 identifying light bulbs","date_published":"2022-03-28T23:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/problems\/identifying-light-bulbs","url":"https:\/\/mathspp.com\/blog\/problems\/identifying-light-bulbs","content_html":"<p>Please help me identify these 100 light bulbs by turning ON and OFF their switches.<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Juan Carlos Becerra on Unsplash.\" alt=\"\" src=\"\/images\/d\/0\/b\/f\/e\/d0bfea312425bb117d684efd6c81092dff57ff31-thumbnail.png\"><figcaption class=\"\">Photo by Juan Carlos Becerra on Unsplash.<\/figcaption><\/figure>\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>I have a very peculiar room in my house.\nIt's a simple room that doesn't have much decoration.\nHowever, I do have 100 light bulbs hanging from the ceiling because I thought it would look cool.\nWhen I installed the 100 light bulbs I wanted maximum freedom,\nso I also installed 100 independent switches:<\/p>\n<ul>\n<li>each switch controls exactly one light bulb; and<\/li>\n<li>each light bulb is controlled by exactly one switch.<\/li>\n<\/ul>\n<p>Of course I was completely silly, so I installed the switches in a room that is far from the room with the light bulbs <strong>and<\/strong> I completely forgot which light switch controls which light bulb.\nHow can I identify which switch controls which light bulb in the <em>least amount of trips<\/em> possible?<\/p>\n<p>For example, I could flip ON a switch and then go verify which light bulb turned ON,\nand I could do this for the 100 light bulbs...\nBut that would take me 100 trips.<\/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<p>This problem was adapted from <a href=\"https:\/\/puzzling.stackexchange.com\/q\/20447\/41687\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">here<\/a> and is licensed under <a href=\"https:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">CC BY-SA 3.0<\/a>.<\/p>\n<h2 id=\"solvers\">Solvers<a href=\"#solvers\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Congratulations to the ones that solved this problem correctly and, in particular, to the ones\nwho sent me their correct solutions:<\/p>\n<ul>\n<li>David H., Taiwan;<\/li>\n<li>Shubham S., India;<\/li>\n<li>Dan, USA;<\/li>\n<li>Jeena K., India;<\/li>\n<li>Frank X., Shenzhen, China;<\/li>\n<li>Wolfgang, Germany;<\/li>\n<li>Naveen K., India;<\/li>\n<li>Pedro G., Portugal;<\/li>\n<li>Dylan S., USA;<\/li>\n<li>Vladimir L., USA;<\/li>\n<li>Sean L., USA;<\/li>\n<\/ul>\n<p>Know how to solve this?\nJoin the list of solvers by <a href=\"mailto:rodrigo@mathspp.com?subject=Solution%20to%20Problem%20#058%20%E2%80%93%20identifying%20light%20bulbs\" class=\"mailto\">emailing me<\/a> your solution!<\/p>\n<h2 id=\"solution\">Solution<a href=\"#solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>We can devise a strategy that identifies <span class=\"mathjax mathjax--inline\">\\(2^n\\)<\/span> light bulbs in <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> trips making use of binary.<\/p>\n<p>In short, what we do is number each switch in binary.\nSuppose the longest binary number has <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> digits.\nThen, we do <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> trips and for each trip <span class=\"mathjax mathjax--inline\">\\(t\\)<\/span> we turn ON the switches whose binary expansions have a <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span> in the corresponding position.\nAfter the <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> trips, the times at which each light bulb was on will correspond to the binary expansion of its switch.<\/p>\n<p>As an example, suppose we have 4 light bulbs and 4 switches.\nWe number the four switches in binary:<\/p>\n<ol>\n<li><span class=\"mathjax mathjax--inline\">\\(00\\)<\/span><\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(01\\)<\/span><\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(10\\)<\/span><\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(11\\)<\/span><\/li>\n<\/ol>\n<p>This means that for the first trip we only turn ON switches 2 and 4, and for the second trip we only turn ON the switches 3 and 4.\nAs two example associations, the light bulb associated with the switch 4 is the light bulb that was turned ON both times and the light bulb associated with the switch 1 is the light bulb that was turned OFF both times.<\/p>\n<p>For 100 light bulbs, the largest binary number will be <span class=\"mathjax mathjax--inline\">\\(1100011\\)<\/span> (for 99), which means we'll need 7 trips.<\/p>\n<p><a href=\"\/subscribe\">Don't forget to subscribe to the newsletter<\/a> to get bi-weekly\nproblems sent straight to your inbox.<\/p>","summary":"Please help me identify these 100 light bulbs by turning ON and OFF their switches.","date_modified":"2025-07-23T16:49:02+02:00","tags":["binary","logic","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p058-identifying-light-bulbs\/thumbnail.png"},{"title":"TIL #017 \u2013 symmetrical indexing with bitwise inversion","date_published":"2021-11-28T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/til\/017","url":"https:\/\/mathspp.com\/blog\/til\/017","content_html":"<p>Today I learned about the symmetry in indexing from the beginning and end of a list with the bitwise invert operator.<\/p>\n\n<script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<p><img alt=\"The text \u201c~i\u201d inside a pair of square brackets in front of a highly geometrical building.\" src=\"\/images\/0\/9\/2\/1\/a\/0921a61cd64bd3cce27cd110bd6e728c46eefec6-thumbnail.webp\"><\/p>\n<h2 id=\"negative-indexing\">Negative indexing<a href=\"#negative-indexing\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>As you might be aware of, Python allows indexing with negative indices:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; s = \"Hello, there!\"\n&gt;&gt;&gt; s[-1]\n'!'<\/code><\/pre>\n<p>In case this is new to you, you can check out <a href=\"\/blog\/pydonts\/sequence-indexing#negative-indices\">this Pydon't on the subject<\/a>.<\/p>\n<p>One thing to note is that the index for the <code>n<\/code>th element is <code>n - 1<\/code>,\nbut the index for the <code>n<\/code>th element from the end is <code>-n<\/code>.\nThis is just \u201chow it works\u201d, but it is kind of a bummer because it is very asymmetrical:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; seq = \"ABCDCBA\"\n&gt;&gt;&gt; seq[0], seq[-1]     # 0 and -1\n('A', 'A')\n&gt;&gt;&gt; seq[1], seq[-2]     # 1 and -2\n('B', 'B')\n&gt;&gt;&gt; seq[2], seq[-3]     # 2 and -3\n('C', 'C')\n&gt;&gt;&gt; seq[3], seq[-4]     # 3 and -4\n('D', 'D')<\/code><\/pre>\n<p>By looking at the correspondences above,\nwe can see that the positive index <code>n<\/code> pairs up with the index <code>-n - 1<\/code>.<\/p>\n<h2 id=\"bitwise-invert\">Bitwise invert<a href=\"#bitwise-invert\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Python has a couple of bitwise operations, one of them being bitwise invert <code>~<\/code>.\nBitwise invert <code>~n<\/code> is defined as <code>-(n+1)<\/code>:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; n = 38\n&gt;&gt;&gt; ~n\n-39\n&gt;&gt;&gt; -(n+1)\n-39\n&gt;&gt;&gt; n = -73\n&gt;&gt;&gt; ~n\n72\n&gt;&gt;&gt; -(n+1)\n72<\/code><\/pre>\n<p>Now, maybe you can see where I'm going with this, but <code>-(n+1)<\/code> simplifies to <code>-n - 1<\/code>.<\/p>\n<h2 id=\"symmetrical-indexing-with-bitwise-inversion\">Symmetrical indexing with bitwise inversion<a href=\"#symmetrical-indexing-with-bitwise-inversion\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>If we put these two pieces of knowledge together,\nwe can see how we can use bitwise inversion <code>~<\/code> to index symmetrically from the beginning\n<em>and<\/em> end of a sequence, like a string or a list:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; seq = \"abccba\"\n&gt;&gt;&gt; seq[1], seq[~1]\n('b', 'b')\n&gt;&gt;&gt; for i in range(len(seq) \/\/ 2):\n...     print(seq[i], seq[~i])\n...\na a\nb b\nc c<\/code><\/pre>\n<p>Doesn't this look beautiful?<\/p>\n<p>I feel like this is one of those things that you really won't use that often,\nbut there will come a time in your life when you'll want to exploit this symmetry!\nAnd, you either remember what you just learned about <code>~<\/code>, or you'll be writing the uglier version with subtractions:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; seq = \"abccba\"\n&gt;&gt;&gt; seq[1], seq[~1]\n('b', 'b')\n&gt;&gt;&gt; for i in range(len(seq) \/\/ 2):\n...     print(seq[i], seq[-i - 1])\n...\na a\nb b\nc c<\/code><\/pre>\n<p>Thanks a lot to Tushar Sadhwani from bringing this to my attention:<\/p>\n<blockquote class=\"twitter-tweet\">\n<p lang=\"en\" dir=\"ltr\">\ud83d\udc0dPython Tip of the day:<br>You can use `~i` to index a list in python from behind, instead of `-1 - i`: <a href=\"https:\/\/t.co\/cVm4JE2aZA\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">pic.twitter.com\/cVm4JE2aZA<\/a><\/p>\u2014 Tushar Sadhwani (@sadhlife) <a href=\"https:\/\/twitter.com\/sadhlife\/status\/1464993896346181637?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">November 28, 2021<\/a>\n<\/blockquote>\n<p>That's it for now! <a href=\"\/subscribe\">Stay tuned<\/a> and I'll see you around!<\/p>","summary":"Today I learned about the symmetry in indexing from the beginning and end of a list with the bitwise invert operator.","date_modified":"2025-07-23T16:49:02+02:00","tags":["binary","mathematics","programming","python"],"image":"\/user\/pages\/02.blog\/04.til\/017.symmetrical-indexing-with-bitwise-inversion\/thumbnail.webp"},{"title":"Problem #029 - hidden key 2 \ud83d\udddd\ufe0f\ud83d\udddd\ufe0f","date_published":"2021-02-07T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/hidden-key-2","url":"https:\/\/mathspp.com\/blog\/problems\/hidden-key-2","content_html":"<p>This problem is a step up from <a href=\"\/blog\/problems\/hidden-key\">Problem #028 - hidden key<\/a>.\nCan you tackle this one?<\/p>\n\n<figure class=\"image-caption\"><img title=\"Original photograph from Aneta Pawlik on Unsplash.\" alt=\"Two keys.\" src=\"\/images\/e\/4\/e\/a\/6\/e4ea634220982548f7c99b7c89d8c05c9b5bde7d-thumbnail.webp\"><figcaption class=\"\">Original photograph from Aneta Pawlik 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 and your best friend are locked in jail for no reason at all, but you are\ngiven the opportunity to escape.\nYou are taken to a room that has four opaque boxes.\nThe key to your cell will be put inside one of the boxes, and then a (regular)\ncoin is placed on top of each box.\nYou may pick a single coin and reverse its face up,\nand then your friend will enter the room.<\/p>\n<p>When your friend enters the room you are not allowed to talk, and your friend\nmust open a box.\nIf your friend opens the box with the key, you are set free.\nOtherwise, you are locked for eternity...<\/p>\n<p>What is the strategy that you and your friend should agree upon, so that\nyour friend can always find the key?<\/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 solution I will be sharing is not the original solution I thought of,\nI decided to share with you the solution that someone [posted][reddit-sol]\nonline when I shared this puzzle on reddit.<\/p>\n<p>What we are going to do is imagine the four boxes are laid out in a two by two square:<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p029-hidden-key-2\/_boxes_1.webp\"><\/p>\n<p>The next thing we do is interpret the sides of the coins as zeroes and ones,\nbecause it is easier to do maths with binary numbers.\nSo a random configuration of the coins (of the zeroes and ones) and the\n(hidden) key could be:<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p029-hidden-key-2\/_boxes_2.webp\"><\/p>\n<p>The next thing we do is agree that each box can be represented by its coordinates,\nin the sense that we can identify each box by the row and column it is in.\nTo make things easier for us, we will start counting the rows and columns from zero,\nso that the top left box is in position <span class=\"mathjax mathjax--inline\">\\((0, 0)\\)<\/span>, the top right box is in position\n<span class=\"mathjax mathjax--inline\">\\((0, 1)\\)<\/span>, the bottom left box is in position <span class=\"mathjax mathjax--inline\">\\((1, 0)\\)<\/span> and the bottom right box\nis in position <span class=\"mathjax mathjax--inline\">\\((1, 1)\\)<\/span>:<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p029-hidden-key-2\/_boxes_3.webp\"><\/p>\n<p>In the example image above, the key is currently in box <span class=\"mathjax mathjax--inline\">\\((1, 0)\\)<\/span>.<\/p>\n<p>Now that we have settled all the important details, we can determine our strategy:<\/p>\n<ul><li>the parity of the sum of the first row of zeroes and ones\nwill encode the row the key is in; and<\/li>\n<li>the parity of the sum of the first column of zeroes and ones\nwill encode the column the key is in.<\/li>\n<\/ul><p>We are talking about the &ldquo;parity&rdquo; of the sum because if the row contains two <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span>s,\nthen we sum them and get <span class=\"mathjax mathjax--inline\">\\(2\\)<\/span>, which is <em>not<\/em> a valid row.\nLikewise for the columns.\nHence, if the first row sums to an even number, then the key is in the first row,\nand if the first row...<\/p>","summary":"Four boxes, four coins and a hidden key - can you find it?","date_modified":"2025-07-23T16:49:02+02:00","tags":["binary","combinatorics","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p029-hidden-key-2\/thumbnail.webp"},{"title":"Problem #028 - hidden key \ud83d\udddd\ufe0f","date_published":"2021-01-24T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/hidden-key","url":"https:\/\/mathspp.com\/blog\/problems\/hidden-key","content_html":"<p>There is a key hidden in one of three boxes and each box has a coin on top of it.\nCan you use the coins to let your friend know where the key is hiding?<\/p>\n\n<p><img alt=\"A photograph of a key, by Aneta Pawlik on Unsplash.\" src=\"\/images\/9\/8\/4\/a\/b\/984abb0db302c3b960d069b442570b66a3555cf5-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>You and your best friend are locked in jail for no reason at all, but you are\ngiven the opportunity to escape.\nYou are taken to a room that has three opaque boxes.\nThe key to your cell will be put inside one of the boxes, and then a (regular)\ncoin is placed on top of each box.\nYou may pick a single coin and reverse its face up,\nand then your friend will enter the room.<\/p>\n<p>When your friend enters the room you are not allowed to talk, and your friend\nmust open a box.\nIf your friend opens the box with the key, you are set free.\nOtherwise, you are locked for eternity...<\/p>\n<p>What is the strategy that you and your friend should agree upon, so that\nyour friend can always find the key?<\/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>There are multiple solutions to this problem, but the most simple and elegant one (that I know of) is as follows:\nflip a coin, so that the box with the key is under a coin whose face\nup is different from the other two coins.<\/p>\n<p>What is interesting is that this is always possible,\nregardless of how the coins are originally placed on top of the boxes.\nI invite you to have a go at demonstrating that the solution\nI just described actually works, before you keep on reading\nmy solution.<\/p>\n<p>If you number the boxes <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span> to <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span>, with the box holding the key being\nbox number <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span>, then there's <span class=\"mathjax mathjax--inline\">\\(8\\)<\/span> different ways the coins could be\nplaced on top of the three boxes.\nHowever, we don't care about the exact coin faces facing up!\nMuch like &ldquo;[Problem #021 - predicting coin tosses][p21]&rdquo;, we only\ncare about the relationships of the outcomes, which are three\nand can be identified by the number of boxes whose coin face up is the same\nas the coin face that is up on top of the box with the key.\nThat was a long sentence, reread it if you must!<\/p>\n<p>For example, if the box with the key has a coin with tails face up, then one\nof these three must be true:<\/p>\n<ol><li>all coins have tails face up.<\/li>\n<li>two coins have tails face up, one on top of the box with the key and another\non top of some other box.<\/li>\n<li>only the coin on top of the box with the key has tails face up.<\/li>\n<\/ol><p>If we are in situation 1., then we turn the coin on top of the box with the key,\nso that its face up is now heads (and it is the only coin with heads facing up).\nIf we are in...<\/p>","summary":"Three boxes, three coins and a hidden key - can you find it?","date_modified":"2025-07-23T16:49:02+02:00","tags":["binary","combinatorics","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p028-hidden-key\/thumbnail.jpg"},{"title":"Problem #024 - hats in a line","date_published":"2020-11-29T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/hats-in-a-line","url":"https:\/\/mathspp.com\/blog\/problems\/hats-in-a-line","content_html":"<p>Some people are standing quiet in a line, each person with a hat that has one of two\ncolours. How many people can guess their colour correctly?<\/p>\n\n<p><img alt=\"A picture with a couple of people in a line, with different-coloured hats\" src=\"\/images\/c\/4\/0\/a\/6\/c40a65ec23594e093b2f204e6c78f8b10976c4db-thumbnail.webp\"><\/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>Some people, let's say <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> people, are standing in a line.\n(Of course they are\nmore than 2m away from each other, social distancing has to be taken seriously by\nall of us.)<\/p>\n<p>Each person has a hat, like the picture above shows.\nEach hat is either light or dark, but no one knows the colour of their own hat\nand people can only look forward and <strong>cannot<\/strong> move at all.\n(Except perhaps to blink and to breath.)<\/p>\n<p>Assuming they got a chance to meet before they got placed in a line and\nreceived their hats, what strategy do they have to agree on so that\nthe <em>most<\/em> people can guess their own hat colour correctly?\nWe can pretend that people who fail to guess their hat colour are sent\nto prison, and of course we want to keep as many people out of prison as possible.\nThe only thing they know is that the hats will be distributed randomly,\nthey have no idea how many hats of each colour will be distributed.<\/p>\n<p>So your task is to devise the best possible strategy and to find out how many people\nthat strategy saves, on average.<\/p>\n<p>It is important to note that:<\/p>\n<ul><li>people cannot communicate with each other once they are in a line;<\/li>\n<li>they can try to guess the colours of their hats in any order you see fit;<\/li>\n<li>each person gets a single attempt;<\/li>\n<li>everyone hears everyone's guess, but only the people behind the person\nmaking a guess know if that person got it right.\nEveryone else has no idea if the guess was correct or not.<\/li>\n<\/ul><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<p>This problem was posed to me by my friend <a href=\"https:\/\/github.com\/LeafarCoder\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">LeafarCoder<\/a>.<\/p>\n<h2 id=\"solution\">Solution<a href=\"#solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Let us say there's <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> people standing in the line.\nWe will show that the best strategy always saves <span class=\"mathjax mathjax--inline\">\\(n - 1\\)<\/span> people and that the\n<span class=\"mathjax mathjax--inline\">\\(n^\\text{th}\\)<\/span> person is saved with a <span class=\"mathjax mathjax--inline\">\\(50\\%\\)<\/span> probability.<\/p>\n<p>In order to do this, we have to do two things:<\/p>\n<ol><li>Show there is no strategy with a better success rate.<\/li>\n<li>Show there is a strategy with this success rate that works.<\/li>\n<\/ol><p>In approaching problems of this type, where one has to &ldquo;find the best strategy\npossible&rdquo;, my approach is often one of a wishful thinker.\nIf someone poses a problem like this, <em>usually<\/em> it's because there is an amazing\nstrategy with a very good success rate.\nWhen that is the case, I always try to see if there can be a <strong>perfect<\/strong>\nstrategy.\nMore often than not, there is a perfect strategy or one that comes quite close\nto it.<\/p>\n<p>For this particular problem, it is easy to see there cannot be a truly perfect\nstrategy: the first person that guess their own hat will have <strong>no<\/strong>...<\/p>","summary":"In this problem you have to figure out how many people can guess the colour of their own hats.","date_modified":"2025-07-23T16:49:02+02:00","tags":["mathematics","game theory","binary"],"image":"\/user\/pages\/02.blog\/03.problems\/p024-hats-in-a-line\/thumbnail.webp"},{"title":"Twitter proof: size of the set of subsets of a set","date_published":"2020-08-06T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/subsets-of-a-set","url":"https:\/\/mathspp.com\/blog\/subsets-of-a-set","content_html":"<p>Let's prove that, if a set has size <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span>, then that same set has exactly <span class=\"mathjax mathjax--inline\">\\(2^n\\)<\/span> subsets.<\/p>\n\n<script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<p><img alt=\"|S| = n implies that |P(S)| = 2^n\" src=\"\/images\/4\/a\/7\/e\/f\/4a7eff7830da7f688b4a8903d5dc69bf9815b01d-subsets.webp\"><\/p>\n<blockquote class=\"twitter-tweet\">\n<p lang=\"en\" dir=\"ltr\">Twitter proof:<br>Take a set of size n. For any of its subsets, we can label items with 0\/1 depending on whether or not the item is in the subset or not, and to any such labelling corresponds a single subset. There are 2^n such labellings, hence 2^n subsets.<a href=\"https:\/\/t.co\/5uxKAi7D9T\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">https:\/\/t.co\/5uxKAi7D9T<\/a><\/p>\u2014 Mathspp (@mathsppblog) <a href=\"https:\/\/twitter.com\/mathsppblog\/status\/1291501878111502338?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">August 6, 2020<\/a>\n<\/blockquote>\n<p>Do you have an idea for a twitter proof? Let me know in the comments below!<\/p>","summary":"In this twitter proof I am going to prove the formula for the amount of subsets a given set has.","date_modified":"2025-07-23T16:49:02+02:00","tags":["binary","mathematics","set theory"],"image":"\/user\/pages\/02.blog\/subsets-of-a-set\/subsets.webp"},{"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 #005 - number me right","date_published":"2018-01-23T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/number-me-right","url":"https:\/\/mathspp.com\/blog\/problems\/number-me-right","content_html":"<p>This problem is a really interesting problem I solved two times. The first time I solved it I failed to prove exactly how it works... then some years later I remembered the problem statement and was able to solve it properly. Let's see how you do!<\/p>\n\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>Take a chessboard and extend it indefinitely upwards and to the right. In the bottom leftmost corner you put a <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>. For every other cell, you insert the smallest non-negative integer that hasn't been used neither in the same row, to the left of the cell, nor in the same column, below it. So, for example, the first row will have the numbers <span class=\"mathjax mathjax--inline\">\\(0, 1, 2, 3, \\cdots\\)<\/span>. What is the number that appears in the <span class=\"mathjax mathjax--inline\">\\(1997\\)<\/span>th row, <span class=\"mathjax mathjax--inline\">\\(2018\\)<\/span>th column?<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought... my best advice would be for you to create a grid in your piece of paper and start filling it out as stated by the rules. Can you find a pattern?<\/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 key here is to understand the way in which the board is filled. When I first solved the problem I started by filling a board on my own, to get a feel for the rules imposed. Doing so should make clear that, for example,<\/p>\n<ul><li>the number in the diagonal is always a <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>;<\/li>\n<li>the board is symmetric along the diagonal, i.e. the number in column <span class=\"mathjax mathjax--inline\">\\(j\\)<\/span> and row <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> is the same number as in row <span class=\"mathjax mathjax--inline\">\\(j\\)<\/span> and column <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span>.<\/li>\n<\/ul><p>The interested reader is left with trying to get a feel for the pattern involved in filling the board. We skip right to the full solution.<\/p>\n<p><strong>Claim:<\/strong> the number in the <span class=\"mathjax mathjax--inline\">\\(i+1\\)<\/span>th row and <span class=\"mathjax mathjax--inline\">\\(j+1\\)<\/span>th column is <span class=\"mathjax mathjax--inline\">\\(i \\hat{} j\\)<\/span>, where <span class=\"mathjax mathjax--inline\">\\(\\hat{}\\)<\/span> is the bitwise [XOR] operation.<\/p>\n<p>The bitwise XOR takes two bit representations and for each pair of corresponding bits returns <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span> if one and only one of the bits was <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span>. Otherwise the result is <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>. For example, <span class=\"mathjax mathjax--inline\">\\(1100_2 \\hat{} 1010_2 = 0110_2\\)<\/span>.<\/p>\n<p>Notice how this aligns with the two observations made:<\/p>\n<ul><li>the diagonal only has zeros; in the diagonal the row and the column have the same number and <span class=\"mathjax mathjax--inline\">\\(n \\hat{} n = 0\\)<\/span>;<\/li>\n<li>being in row <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> and column <span class=\"mathjax mathjax--inline\">\\(j\\)<\/span> is the same as being in column <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> and row <span class=\"mathjax mathjax--inline\">\\(j\\)<\/span>; <span class=\"mathjax mathjax--inline\">\\(i \\hat{} j = j \\hat{} i\\)<\/span>.<\/li>\n<\/ul><p>From now on we will renumber the rows and columns so that they start at <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> and we will let <span class=\"mathjax mathjax--inline\">\\(c(i,j)\\)<\/span> be the value in the <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span>th row, <span class=\"mathjax mathjax--inline\">\\(j\\)<\/span>th column. Let <span class=\"mathjax mathjax--inline\">\\(P(n)\\)<\/span> be the statement \"the bottom left square of the table with side <span class=\"mathjax mathjax--inline\">\\(2^n\\)<\/span> has, in every row, every single number from <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> to <span class=\"mathjax mathjax--inline\">\\(2^n -1\\)<\/span> <strong>and<\/strong><\/p>\n<p class=\"mathjax mathjax--block\">\\[\n    \\forall i,j \\leq 2^n-1: c(i,j) = i \\hat{} j\\ .\\]<\/p>\n<p>This final part means that the value of a specific cell is...<\/p>","summary":"This blog post concerns a really interesting problem that takes place on an infinite board!","date_modified":"2025-07-23T16:49:02+02:00","tags":["binary","induction","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p005-number-me-right\/nmr_1.webp"}]}
