
    
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/combinatorics","feed_url":"https:\/\/mathspp.com\/blog\/tags\/combinatorics.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":"TIL #132 \u2013 Double factorial","date_published":"2025-09-25T11:47:00+02:00","id":"https:\/\/mathspp.com\/blog\/til\/double-factorial","url":"https:\/\/mathspp.com\/blog\/til\/double-factorial","content_html":"<p>Today I learned about the double factorial.<\/p>\n\n<p>The double factorial, <span class=\"mathjax mathjax--inline\">\\(n!!\\)<\/span>, is defined as the product of all positive integers less than or equal to <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> that have the same parity as <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span>.<\/p>\n<p>Some examples:<\/p>\n<ul>\n<li><span class=\"mathjax mathjax--inline\">\\(5!! = 5 \\times 3 \\times 1\\)<\/span><\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(8!! = 8 \\times 6 \\times 4 \\times 2\\)<\/span><\/li>\n<\/ul>\n<p>When reading about it in a book authored by a friend of mine, the identity <span class=\"mathjax mathjax--inline\">\\((2n)!! = 2^n n!\\)<\/span> was also presented, and I'll prove it now by induction.\nFor <span class=\"mathjax mathjax--inline\">\\(n = 1\\)<\/span>, we have <span class=\"mathjax mathjax--inline\">\\(2!! = 2 = 2^1 \\times 1!\\)<\/span>, which is true.\nNow, assuming the identity holds up to <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span>, we show it holds for <span class=\"mathjax mathjax--inline\">\\(n + 1\\)<\/span>:<\/p>\n<p class=\"mathjax mathjax--block\">\\[\n\\begin{align}\n(2(n + 1))!! &amp;= (2(n + 1)) \\times (2n)!! \\\\\n&amp;= (2(n + 1)) \\times 2^n n! \\\\\n&amp;= (n + 1) \\times 2^{n + 1} n! \\\\\n&amp;= 2^{n + 1} (n + 1)!\n\\end{align}\\]<\/p>\n<p>Done!<\/p>","summary":"Today I learned about the double factorial.","date_modified":"2025-10-20T22:34:56+02:00","tags":["combinatorics","arithmetic","induction","mathematics"],"image":"\/user\/pages\/02.blog\/04.til\/132.double-factorial\/thumbnail.webp"},{"title":"Biggest square","date_published":"2024-02-22T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/biggest-square","url":"https:\/\/mathspp.com\/blog\/biggest-square","content_html":"<p>How can you find the biggest free square in a 2D map with obstacles?<\/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>I just got a call from the President of Portugal.\nPortugal recently mapped out the Atlantic Ocean to the West of our coast.\nThey want to build a sea platform to study the ecosystems of the Ocean.\nThey need my help to figure out where to build that platform.<\/p>\n<p>To show you what I'm working with, here is a portion of the map they created:<\/p>\n<pre><code>..o..\n.o..o\n.....<\/code><\/pre>\n<p>Each character represents a small square section of the sea.\nThe dots <code>.<\/code> represent sections where the sea bed is uniform whereas the circles <code>o<\/code> represent sections where the sea bed is not uniform, where there are shipwrecks, corals, and other things.<\/p>\n<p>Their engineers said the platform must be square, so my job is to find the largest square region where the platform could be built.\nFor example, in the map below I'm marking the largest square region with crosses <code>x<\/code>:<\/p>\n<pre><code>..o..\n.oxxo\n..xx.<\/code><\/pre>\n<p>They also told me that if there are two or more square regions of the same size, they'll prefer the one that's closer to the top, and then to the left, of the map.<\/p>\n<p>So, if the mapped out region were<\/p>\n<pre><code>ooo.....\n..o..o..\n..ooo...\n..o.....\n..o.....<\/code><\/pre>\n<p>There would be plenty of possible locations:<\/p>\n<pre><code>oooxx.xx\nxxoxxoxx\nxxooo...\n..oxx.xx\n..oxx.xx<\/code><\/pre>\n<p>They'd prefer to build their platform here:<\/p>\n<pre><code>oooxx...\n..oxxo..\n..ooo...\n..o.....\n..o.....<\/code><\/pre>\n<h2 id=\"input-test-map\">Input test map<a href=\"#input-test-map\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>They're sending me the sea map in a file with the format shown below:<\/p>\n<pre><code>...........................\n....o......................\n............o..............\n...........................\n....o......................\n...............o...........\n...........................\n......o..............o.....\n..o.......o................<\/code><\/pre>\n<p>Save this map in a file and try to solve this problem.\nYou should be able to find this solution:<\/p>\n<pre><code>.....xxxxxxx...............\n....oxxxxxxx...............\n.....xxxxxxxo..............\n.....xxxxxxx...............\n....oxxxxxxx...............\n.....xxxxxxx...o...........\n.....xxxxxxx...............\n......o..............o.....\n..o.......o................<\/code><\/pre>\n<p>For bonus points, use the function below to generate more maps and test your solution on them:<\/p>\n<pre><code class=\"language-py\">import random\n\ndef create_map(map_path: str, width: int, height: int, obstacle_prob: float) -&gt; None:\n    \"\"\"Creates a map with the given size.\"\"\"\n    with open(map_path, \"w\") as file:\n        for _ in range(height):\n            for _ in range(width):\n                char = \"o\" if random.random() &lt; obstacle_prob else \".\"\n                file.write(char)\n            file.write(\"\\n\")<\/code><\/pre>\n<p>Also, can your solution handle maps of all sizes?\nWhat if the maps are completely empty?\nWhat if the maps are completely full?<\/p>\n<p>After you've solved the problem yourself you can keep reading to see how I did it.<\/p>\n<h2 id=\"solution\">Solution<a href=\"#solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The first thing I did was create a function that takes in a file path and retrieves the map from that file:<\/p>\n<pre><code class=\"language-py\">def read_map(pathname: str) -&gt; list[str]:\n    \"\"\"Reads the map from a file.\"\"\"\n    with open(pathname, \"r\") as file:\n        return [line.strip() for line in file]<\/code><\/pre>\n<p>We can do this by opening the file for reading and then using a <a href=\"\/blog\/pydonts\/list-comprehensions-101\">list comprehension<\/a> to go over the lines in the file and stripping them of the trailing newline.<\/p>\n<p>Now, we must think about the way in which we'll approach the problem.\nWhat I'll do is traverse the whole map and I'll look at each position as the possible...<\/p>","summary":"How can you find the biggest free square in a 2D map with obstacles?","date_modified":"2025-11-17T18:08:50+01:00","tags":["algorithms","combinatorics","programming","python"],"image":"\/user\/pages\/02.blog\/biggest-square\/thumbnail.webp"},{"title":"N queens problem","date_published":"2024-02-09T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/n-queens-problem","url":"https:\/\/mathspp.com\/blog\/n-queens-problem","content_html":"<p>This article shows how to solve the N queens problem in 20 lines of code.<\/p>\n\n<h2 id=\"the-problem-for-n-10\">The problem for N = 10<a href=\"#the-problem-for-n-10\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>(You can <a href=\"#the-solution\">skip to the solution<\/a> or just <a href=\"#the-code\">see the code<\/a>.)<\/p>\n<p>&ldquo;Do you understand what you have to do?&rdquo;, Queen #6 asks.<\/p>\n<p>&ldquo;Uh, I&ndash; I think so.&rdquo;<\/p>\n<p>The challenge sounded simple enough but I was a bit intimidated by the Ten Queens all looking at me.<\/p>\n<p>They stood tall in front of me.\nAn impeccable pose, worthy of a Queen.\nTimes ten.<\/p>\n<p>They looked rigid and cold, wearing their white and black dresses.\nBut if you looked carefully enough, they also looked...\nThey looked... hopeful!\nThey believed I would be able to help them.<\/p>\n<p>I wasn&rsquo;t feeling confident and Queen #8 picked up on that, so she decided to recap what they needed:\n&ldquo;Like my sister #6 said, we need to distribute ourselves on this 10 &times; 10 board.\nThe goal is to <em>count<\/em> in how many different ways this can be done.<\/p>\n<p>We like to have room to pace a bit, so no two of us can be on the same row, column, or diagonal.\nThis restriction is essential.&rdquo;<\/p>\n<p>I nodded along while she recapped.\nThen, I asked &ldquo;How on Earth am I supposed to compute this?\nI can&rsquo;t do this with pen and paper!&rdquo;.<\/p>\n<p>All Queens started laughing uncontrollably.\nQueen #1 managed to control herself for long enough to reply.<\/p>\n<p>&rdquo;You silly! You use Python, of course!&rdquo;\nShe waved.<\/p>\n<p>The two pawns at the entrance of the room we were in opened the massive doors.\nAs the huge doors creaked and opened slowly, four pawns came into the hall, carrying a computer.\nThe computer was already turned on.\nAs the computer moved closer, this is what I saw on the screen:<\/p>\n<pre><code>Python 3.12.0 (the Ten Queens build)\nType \"help\" for more information.\n&gt;&gt;&gt;<\/code><\/pre>\n<p>&ldquo;You have one week.\nYou can start now.&rdquo;<\/p>\n<p>I walked up to the computer and started working on the problem.<\/p>\n<h2 id=\"the-solution\">The solution<a href=\"#the-solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>I was thinking aloud while I was typing.<\/p>\n<p>&ldquo;We know that no two queens can be on the same row or column.\nAnd that's easy to enforce in my code.\nI'll traverse the columns and put one of you in each column while also not repeating rows.\nIt's the diagonals that I have to be careful about.&rdquo;<\/p>\n<p>I paused for a bit.\nThen I wrote this function:<\/p>\n<pre><code class=\"language-py\">def diagonally_safe(row, col, placements):\n    for qrow, qcol in enumerate(placements):\n        if row - col == qrow - qcol or row + col == qrow + qcol:\n            return False\n    return True<\/code><\/pre>\n<p>I proceeded to explain:<\/p>\n<p>&ldquo;I'll store a tuple called <code>placements<\/code> with the positions of some of you.\nThe index is the row you're in and the value itself represents the column.\nFor example, if <code>placements<\/code> is <code>(5, 0, 4)<\/code> that means that the row <code>0<\/code> has a queen in column <code>5<\/code>, the row <code>1<\/code> has a queen in column <code>0<\/code>, and the row <code>3<\/code> has a queen in column <code>4<\/code>.&rdquo;<\/p>\n<p>The queens...<\/p>","summary":"This article shows how to solve the N queens problem in 20 lines of code.","date_modified":"2025-09-27T16:50:04+02:00","tags":["algorithms","combinatorics","logic","programming","python","recursion"],"image":"\/user\/pages\/02.blog\/n-queens-problem\/thumbnail.webp"},{"title":"Problem #056 \u2013 tennis tournament","date_published":"2022-02-20T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/tennis-tournament","url":"https:\/\/mathspp.com\/blog\/problems\/tennis-tournament","content_html":"<p>How many matches does it take to find the winner of a tennis tournament?<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Valentin Balan on Unsplash.\" alt=\"\" src=\"\/images\/1\/2\/d\/5\/0\/12d504518e199064b7e669c58219b68473142ac6-thumbnail.png\"><figcaption class=\"\">Photo by Valentin Balan 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>Suppose that <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> players are going to play in a tennis tournament.\nThe players will be randomly assigned to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bracket_(tournament)\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">brackets<\/a>,\nand each bracket plays a match.\nThe winner of each match advances to the next bracket,\nuntil the two final players face each other in the final match,\nwhich determines the winner.<\/p>\n<p>As a function of the number of players <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span>,\nhow many matches are needed to determine the winner of the tournament?<\/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=\"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>Umberto M., Italy;<\/li>\n<li>Christ van W., The Netherlands;<\/li>\n<li>Thierry Z., Burkina Faso;<\/li>\n<li>Kishan M., India;<\/li>\n<li>Ioan E., Ukraine;<\/li>\n<li>Soliu, Nigeria;<\/li>\n<li>Francisco M., Mexico;<\/li>\n<li>Han A., Malaysia;<\/li>\n<li>Pavan J., India;<\/li>\n<li>David F., United States;<\/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#056%20%E2%80%93%20tennis%20tournament\" 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>There is a nice intuitive solution to this problem that means you don't need to do any calculations whatsoever!<\/p>\n<p>Each time two players face each other, one player leaves the tournament and the other player remains.\nOn top of that, determining the winner is the same as saying that all players have left the tournament,\nexcept for one.\nThus, if there are <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> players, we want to eliminate a total of <span class=\"mathjax mathjax--inline\">\\(n - 1\\)<\/span> players,\nwhich means we need to play <span class=\"mathjax mathjax--inline\">\\(n - 1\\)<\/span> matches.<\/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":"How many matches does it take to find the winner of a tennis tournament?","date_modified":"2025-07-23T16:49:02+02:00","tags":["combinatorics","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p056-tennis-tournament\/thumbnail.png"},{"title":"Problem #054 \u2013 imperfect compression","date_published":"2022-01-23T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/imperfect-compression","url":"https:\/\/mathspp.com\/blog\/problems\/imperfect-compression","content_html":"<p>Can you show that perfect compression is impossible?<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Rui Matayoshi on Unsplash.\" alt=\"\" src=\"\/images\/e\/c\/d\/8\/d\/ecd8df7fc67e7eb6dc340f7f49c7cf0403cbc855-thumbnail.png\"><figcaption class=\"\">Photo by Rui Matayoshi 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>Compression is great: it is what lets you take that giant folder you have and reduce its size to save some memory on your laptop.\nOf course, you only do these compressions happily because you know you don't lose information when you compress things.\nThe data is just... compressed!<\/p>\n<p>For compression to be useful, it has to be bidirectional: you must be able to recover the original data from the compressed version.\nThis is only possible if two different pieces of data never get compressed into the same thing.\n(In mathematical terms, we say that the compression must be injective.)<\/p>\n<p>Now, on top of that, we are interested in compression that actually works, right?\nThat is, in compression that reduces the size of things.\nRight?<\/p>\n<p>Right!\nNow, the challenge is for you to show that no compression mechanism is perfect.\nIn other words, show that if a compression mechanism is bidirectional and it manages\nto take some pieces of data and transform them into something smaller,\nthen, there are pieces of data that will become <strong>larger<\/strong> by the action of the compression mechanism.<\/p>\n<p>If it makes it easier for you,\nwe can suppose that the data we are talking about are just sequences of letters.\nSo, we are talking about compression mechanisms that take sequences of letters and try to build smaller\nsequences of letters, the compression.\nFor example, maybe the sequence <code>aaaaaa<\/code> gets compressed into <code>Aaab<\/code>,\nbut maybe the mechanism fails on <code>AAAAAA<\/code> because it &ldquo;compresses&rdquo; it into <code>arghfewtoen<\/code>.<\/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=\"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><li>David H., Taiwan;<\/li>\n<\/ul><p>Know how to solve this?\nJoin the list of solvers by <a href=\"mailto:rodrigo@mathspp.com?subject=Solution%20to%20Problem%20#054%20%E2%80%93%20imperfect%20compression\" 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>Let's assume that there <em>is<\/em> a perfect compression algorithm.\nFor the empty sequence, what does this algorithm do?\nIt has to compress the empty sequence into the empty sequence,\nbecause there is no shorter sequence to compress the sequence into.<\/p>\n<p>Now, let's think about sequences of length 1.\nNone of those can be compressed into the only sequence of length 0,\nbecause there is already a sequence compressed into that (itself).\nThus, all sequences of length 1 must map to other sequences of length 1.\nOf course they don't map to sequences of length 2 or greater,\notherwise the <em>compression<\/em> would actually make the sequences larger.<\/p>\n<p>Therefore, the sequences of length 1 all map to each other,\nand no two sequences can map to the same one,\nso the compression algorithm really only works as a shuffling of the sequences...<\/p>\n<p>Now, we can just repeat this train of thought indefinitely:\nfor the sequences of length 2,\nnone of them can map to sequences of length 0 or 1,\nbecause those are all...<\/p>","summary":"Can you show the perfection compression algorithm doesn&#039;t exist?","date_modified":"2025-07-23T16:49:02+02:00","tags":["combinatorics","induction","mathematics","pigeonhole principle"],"image":"\/user\/pages\/02.blog\/03.problems\/p054-imperfect-compression\/thumbnail.png"},{"title":"Problem #052 \u2013 chessboard domino","date_published":"2021-12-29T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/chessboard-domino","url":"https:\/\/mathspp.com\/blog\/problems\/chessboard-domino","content_html":"<p>Can you tile a chessboard with two missing squares?<\/p>\n\n<figure class=\"image-caption\"><img title=\"A chessboard with two domino tiles on top of it.\" alt=\"\" src=\"\/images\/2\/e\/d\/2\/6\/2ed2670aa39e389f82f4a696e6dda2886f2d26c4-thumbnail.webp\"><figcaption class=\"\">A chessboard with two domino tiles on top of it.<\/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>If you remove two opposite corners of a chessboard,\ncan you still tile it with 2 x 1 rectangles?<\/p>\n<p>In other words, can you cover a whole chessboard with 2 x 1 domino tiles while leaving two opposite corners uncovered and without overlapping domino tiles?<\/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>Kees de L., Netherlands;<\/li>\n<\/ul>\n<p>Know how to solve this?<\/p>\n<p>Join the list of solvers by <a href=\"mailto:rodrigo@mathspp.com?subject=Solution%20to%20Problem%20#052%20%E2%80%93%20chessboard%20domino\" 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>It is <em>impossible<\/em> to do this and the proof follows.<\/p>\n<p>We start by realising that whenever we place a 2x1 domino tile on a chessboard, we always cover two squares of opposite colours, regardless of where we place the domino tile and regardless of its orientation.<\/p>\n<p>Now, consider the colours of the squares of a chessboard with two opposite corners removed.\nOpposite corners are of the same colour, so if we remove two white corners, the chessboard remains with 30 white squares and 32 black squares.\nWhenever we place a domino tile on the board, each of the two quantities decreases by 1.\nSo, after we place 30 domino tiles, there will be 0 white squares left to cover and 2 black squares, which cannot be covered by a single domino tile.<\/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":"Can you tile a chessboard with two missing squares?","date_modified":"2025-07-23T16:49:02+02:00","tags":["chess","combinatorics","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p052-tiling-a-chessboard\/thumbnail.webp"},{"title":"Problem #051 \u2013 queens and knights","date_published":"2021-12-12T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/queens-and-knights","url":"https:\/\/mathspp.com\/blog\/problems\/queens-and-knights","content_html":"<p>How many queens and knights can you place on a chessboard?<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by \u1d0a\u1d00\u1d04\u029c\u028f\u1d0d \u1d0d\u026a\u1d04\u029c\u1d00\u029f on Unsplash.\" alt=\"A white chess queen and black chess knight's silhouettes against the sun.\" src=\"\/images\/0\/a\/8\/0\/9\/0a809b3a31933fb1cf2d8b2980b14846886cc54d-thumbnail.webp\"><figcaption class=\"\">Photo by \u1d0a\u1d00\u1d04\u029c\u028f\u1d0d \u1d0d\u026a\u1d04\u029c\u1d00\u029f 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>This is a harder variant of the <a href=\"\/blog\/problems\/8-queens\">8 queens<\/a> problem.\nAs a warm-up for <em>this<\/em> problem, consider solving that one first!<\/p>\n<p>For the \u201cqueens and knights\u201d problem,\nyou want to place as many queens and knights, as possible, on a chessboard.\nHowever, queens and knights must be placed in equal numbers,\nand no piece can attack any other piece.<\/p>\n<p>While you are at it, can you do it in multiple ways?<\/p>\n<p>For reference, here is a picture showing what squares a queen attacks:<\/p>\n<figure class=\"image-caption\"><img title=\"Squares under attack by a queen.\" alt=\"Chessboard with a queen and the squares under attack.\" src=\"\/user\/pages\/02.blog\/03.problems\/p051-queens-and-knights\/_queen_attack.webp\"><figcaption class=\"\">Squares under attack by a queen.<\/figcaption><\/figure>\n<p>Similarly, here is a picture showing what squares a knight attacks:<\/p>\n<figure class=\"image-caption\"><img title=\"Squares under attack by a knight.\" alt=\"Chessboard with a knight and the squares under attack.\" src=\"\/user\/pages\/02.blog\/03.problems\/p051-queens-and-knights\/_knight_attack.webp\"><figcaption class=\"\">Squares under attack by a knight.<\/figcaption><\/figure>\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 brought to my attention by the late <a href=\"https:\/\/rogerhui.rip\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Roger Hui<\/a>,\nso let my sharing this problem be a small tribute to him.<\/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<\/ul>\n<p>Know how to solve this?<\/p>\n<p>Join the list of solvers by <a href=\"mailto:rodrigo@mathspp.com?subject=Solution%20to%20Problem%20#051%20%E2%80%93%20queens%20and%20knights\" 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 start by showing that it is impossible to have 6 queens and 6 knights on the board.<\/p>\n<p>To place 6 queens on a board, they have to be in 6 different rows and 6 different columns, which means they leave a maximum of 4 squares (2 rows times 2 columns) free.\nWe cannot fit 6 knights into 4 squares.<\/p>\n<p>By finding a configuration with 5 queens and 5 knights, we prove that the maximum is 5, which is what the figure shows:<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p051-queens-and-knights\/_solution.webp\"><\/p>\n<p>In chess notation, we could place 5 queens at g1, b2, f3, c4, and a5, and the knights at h6, h7, e7, d7, and e8.<\/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":"How many queens and knights can you place on a chessboard?","date_modified":"2025-07-23T16:49:02+02:00","tags":["chess","combinatorics","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p051-queens-and-knights\/thumbnail.webp"},{"title":"Problem #050 \u2013 8 queens","date_published":"2021-11-28T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/8-queens","url":"https:\/\/mathspp.com\/blog\/problems\/8-queens","content_html":"<p>In how many ways can you place 8 queens on a chessboard?<\/p>\n\n<figure class=\"image-caption\"><img title=\"Chess queen and attacked positions on a photo by Nick Fewings on Unsplash.\" alt=\"A chessboard seen from above with a queen in the centre and lines showing the positions under attack.\" src=\"\/images\/c\/f\/3\/d\/3\/cf3d375c82a31695ad42e5325327bc9a88631cd0-thumbnail.webp\"><figcaption class=\"\">Chess queen and attacked positions on a photo by Nick Fewings 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>The image above shows you what positions a single queen attacks on a chessboard:\nall positions in the same vertical, horizontal, and diagonal lines.<\/p>\n<p>Your job is to place 8 queens on a chessboard, so that no two queens attack each other.\n(In how many different ways can you do this?)<\/p>\n<p>Is this too easy?\nDon't worry, the next one will be a harder variant of this puzzle.<\/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=\"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>Kees de L., Netherlands;<\/li>\n<li>B. Praveen R., India;<\/li>\n<li>Kishan M., India;<\/li>\n<li>Mario V., Guatemala;<\/li>\n<li>Pavel D., Czech Republic;<\/li>\n<\/ul>\n<p>Know how to solve this?<\/p>\n<p>Join the list of solvers by <a href=\"mailto:rodrigo@mathspp.com?subject=Solution%20to%20Problem%20#050%20%E2%80%93%208%20queens\" 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>Here is an example placement of 8 queens on a chessboard:<\/p>\n<figure class=\"image-caption\"><img title=\"Eight, non-attacking queens.\" alt=\"Queens in positions h7, g5, f3, e1, d6, c8, b2, a4.\" src=\"\/user\/pages\/02.blog\/03.problems\/p050-8-queens\/_solution.webp\"><figcaption class=\"\">Eight, non-attacking queens.<\/figcaption><\/figure>\n<p>This is a neat little puzzle, but I can't offer a better solution other than \u201cdo it by trial and error\u201d.<\/p>\n<p>As for the total number of solutions, there are 92.\nAgain, sadly I don't know of a nice way to compute that by hand,\nbut I can show you a Python program that computes all the solutions:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; from itertools import permutations\n&gt;&gt;&gt; for queens in permutations(range(8)):\n...     diags = {r - c for r, c in enumerate(queens)}\n...     antidiags = {r + c for r, c in enumerate(queens)}\n...     if len(diags) == len(antidiags) == 8:\n...         # print(list(enumerate(queens)))\n...         c += 1\n...\n&gt;&gt;&gt; c\n92<\/code><\/pre>\n<p>This is a Python snippet that solves this problem.\nBy using <code>itertools.permutations<\/code>, we manage to get all possible queen placements\nwhere no queen attacks another queen horizontally or vertically.\nThen, we are left with checking for the diagonals.<\/p>\n<p>For the diagonals (and representing both rows and columns as integers),\nwe have that each diagonal has an invariant.\nFor one set of diagonals, that invariant is the sum of the two coordinates,\nand for the other set of diagonals, it's the difference between the two coordinates.<\/p>\n<p>For example, letting the letters \u201cA\u201d through \u201cH\u201d represent the integers 1 through 8,\nhere is an image that shows how, along one diagonal,\nthe sum of the two coordinates is always 10 and, along another diagonal,\nthe difference of the two coordinates is always -2:<\/p>\n<figure class=\"image-caption\"><img title=\"Two diagonals, and their invariants, highlighted.\" alt=\"A chessboard with two highlighted diagonals showing their invariants.\" src=\"\/user\/pages\/02.blog\/03.problems\/p050-8-queens\/_diagonals.webp\"><figcaption class=\"\">Two diagonals, and their invariants, highlighted.<\/figcaption><\/figure>\n<p>Therefore, we can just check if all the invariants are different because,\nif they are, that's because the queens are not attacking each other diagonally.<\/p>\n<p>Now, was this easy?\nI have a <a href=\"\/blog\/problems\/queens-and-knights\">similar, but harder problem<\/a> waiting for you.<\/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":"In how many ways can you place 8 queens on a chessboard?","date_modified":"2025-07-23T16:49:02+02:00","tags":["chess","combinatorics","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p050-8-queens\/thumbnail.webp"},{"title":"Counting mosaics with APL","date_published":"2021-11-15T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/counting-mosaics-with-apl","url":"https:\/\/mathspp.com\/blog\/counting-mosaics-with-apl","content_html":"<p>This is a short article on how I quickly used APL to verify my combinatorics calculations.<\/p>\n\n<p><img alt=\"An image of two 3 by 3 grids with coloured cells\" src=\"\/user\/pages\/02.blog\/counting-mosaics\/thumbnail.webp\"><\/p>\n<h2 id=\"preamble\">Preamble<a href=\"#preamble\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>A local university runs a weekly challenge with mathematical problems\nfor everyone to solve.\nThis past week, the problem shared was a combinatorics problem that read as follows\n(I'm paraphrasing):<\/p>\n<p>&ldquo;Consider a 3 by 3 grid of squares composing a mosaic.\nIn how many ways can we colour that mosaic, if we have 3 different colours\nand if adjacent squares cannot share the same colour?&rdquo;<\/p>\n<p>In the problem statement, they clarify that &ldquo;adjacent&rdquo; means &ldquo;horizontally or vertically adjacent&rdquo;,\nand they even give a hint\n(skip to the next paragraph if you do not want <em>any<\/em> spoilers!):\n&ldquo;number the squares from 1 to 9, and then work with the colours of the even squares.\nThat shall determine the colours of the odd squares.&rdquo;.<\/p>\n<p>Combinatorics has never been my strongest suit, but I wanted to solve this problem.\nWith that in mind, I sat down and started drafting my solution.<\/p>\n<p>By the time I was done, I decided to quickly verify my solution with <a href=\"https:\/\/apl.wiki\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">APL<\/a>,\na very neat programming language that I have been learning for the past 2 years.<\/p>\n<p>This is the article of how it took me 30 seconds in APL to figure out my\nsolution had a problem.<\/p>\n<ol><li>I'll start by showing you my flawed proof (as I wrote it);<\/li>\n<li>then, I'll tell you what I did in APL to check my solution;<\/li>\n<li>afterwards, I'll show you the mistake I made initially; and finally,<\/li>\n<li>I'll work a bit more on the APL code to make it cleaner.<\/li>\n<\/ol><h2 id=\"my-initial-solution\">My initial solution<a href=\"#my-initial-solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The text that follows was my initial solution to the problem I described above.\nAs a challenge for you, try to find the flaws in my reasoning\/calculations.\nHere we go.<\/p>\n<p>&ldquo;As per the problem statement hint, we'll number the squares in the grid:<\/p>\n<pre><code class=\"language-txt\">1 2 3\n4 5 6\n7 8 9<\/code><\/pre>\n<p>Now we will study all the possible colourings as a function of the colours\nattributed to squares 2, 4, 6, and 8.<\/p>\n<p>The even squares can only use up to 2 colours,\notherwise square 5 won't have a possible colour.<\/p>\n<p>Let's suppose that the even squares have a single colour.\nIn that case, the odd squares can be coloured arbitrarily.\nThat makes up for a total of <span class=\"mathjax mathjax--inline\">\\(3 \\times 3^5 = 3^6\\)<\/span> mosaics.<\/p>\n<p>Now, let's assume that the even squares have 2 colours.\nLet's fix the colour of square number 2,\nwhich can be any one colour of the three available colours.\nNext, there's either one, two, or three,\nsquares with a colour that is different from square 2,\nbut equal among themselves.\nLet's consider these three cases separately.<\/p>\n<p>We start with assuming there's only one square with a colour different\nfrom the colour of square 2.\nThat colour can be any one of two colours.\nNext, we just have to figure out in how many ways we can colour the odd squares.\nThat will depend on the position...<\/p>","summary":"This is a short article on how I quickly used APL to verify my combinatorics calculations.","date_modified":"2025-07-23T16:49:02+02:00","tags":["apl","combinatorics","mathematics","programming"],"image":"\/user\/pages\/02.blog\/counting-mosaics\/thumbnail.webp"},{"title":"Problem #048 \u2013 trick or treat","date_published":"2021-10-31T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/problems\/trick-or-treat","url":"https:\/\/mathspp.com\/blog\/problems\/trick-or-treat","content_html":"<p>Can you help these kids trick or treat their entire neighbourhood in this Halloween special?<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Taylor Rooney on Unsplash.\" alt=\"A pumpkin patch with Halloween-themed carved pumpkins.\" src=\"\/user\/pages\/02.blog\/03.problems\/p048-trick-or-treat\/thumbnail.png\"><figcaption class=\"\">Photo by Taylor Rooney 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>It's Halloween night.\nJack, Frank, and their 98 friends are getting ready to trick or treat their entire neighbourhood.<\/p>\n<p>This year, the group of 100 friends decided they wanted to be very efficient...\nHence, tonight they won't be walking together as a huge group.\nInstead, they'll be having fun at the park, and each friend will visit one of the 100 houses of the neighborhood at a time.<\/p>\n<p>The 100 friends agreed on assigning one house per friend, so as to maximise the amount of candy they could get.\nJack is the first to go, but completely forgets the house he was assigned, so he decides to go trick or treating a random house instead.\nFrom there on, throughout the night, each friend would go to their assigned house, unless one of the other friends already went there.\nIf that's the case, the friend will just pick one of the remaining houses randomly.<\/p>\n<p>Frank is the last of the 100 friends to go.\nWhat is the probability that Frank will go trick-or-treating at the house he was assigned originally?<\/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=\"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><li>\n<a href=\"https:\/\/twitter.com\/m2u_84\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Matthias W.<\/a>, Germany;<\/li>\n<li>Kees L., Netherlands;<\/li>\n<li>David H., Taiwan;<\/li>\n<\/ul><p>Know how to solve this?<\/p>\n<p>Join the list of solvers by <a href=\"mailto:rodrigo@mathspp.com?subject=Solution%20to%20Problem%20#048%20%E2%80%93%20trick%20or%20treat\" 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>This problem is very neat, in my opinion.\nThat's because there is a very neat solution that does not involve many calculations.<\/p>\n<p>If you are like me, and hate calculations, you are going to love this solution.<\/p>\n<p>When Jack goes out and has to pick one of the 100 houses,\none of three things happens:<\/p>\n<ol><li>Jack picks his own house (that happens with a probability of 1\/100);<\/li>\n<li>Jack picks Frank's house (that happens with a probability of 1\/100); and<\/li>\n<li>Jack picks another friend's house (that happens with a probability of 98\/100).<\/li>\n<\/ol><p>If 1. happens, we know that Frank gets to go to his originally assigned house.<\/p>\n<p>If 2. happens, we know that Frank will <em>not<\/em> go to his originally assigned house.<\/p>\n<p>Notice how scenarios 1. and 2. are equally likely to occur.<\/p>\n<p>If 3. happens, Frank (not) going to his originally assigned house will depend\non what happens next.\nLet's say that Jack visits (by mistake) the house that was destined for friend <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span>,\nwhere <span class=\"mathjax mathjax--inline\">\\(i &lt; 100\\)<\/span>.\n(<span class=\"mathjax mathjax--inline\">\\(i = 100\\)<\/span>) is Frank, and that would mean we are in situation 2.<\/p>\n<p>After Jack visits the house that was assigned to his friend <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span>,\nthe friends 2, 3, ..., all the way to <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span>, go to their originally assigned houses.<\/p>\n<p>When it comes the time for friend <span class=\"mathjax mathjax--inline\">\\(i\\)<\/span> to go trick-or-treating,\nhow many houses are...<\/p>","summary":"Can you help these kids trick or treat their entire neighbourhood in this Halloween special?","date_modified":"2025-07-23T16:49:02+02:00","tags":["combinatorics","logic","mathematics","probability"],"image":"\/user\/pages\/02.blog\/03.problems\/p048-trick-or-treat\/thumbnail.png"}]}
