
    
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/logic","feed_url":"https:\/\/mathspp.com\/blog\/tags\/logic.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":"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 #064 \u2013 infinite mathematicians with hats","date_published":"2024-01-02T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/infinite-mathematicians-with-hats","url":"https:\/\/mathspp.com\/blog\/problems\/infinite-mathematicians-with-hats","content_html":"<p>How can an infinite number of mathematicians figure out their own hat colours?<\/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>An infinite number of mathematicians are standing in a line.\nIn a couple of minutes, a disruption in the space-time continuum will cause a black or white hat to appear on the head of each mathematician.\nAfter that happens, the mathematicians will try to guess their own hat colour (they can't see it) based on the colours of the hats of all other mathematicians.<\/p>\n<p>What's the strategy that the mathematicians must agree on, before hand, so that only a <em>finite<\/em> number of mathematicians guesses wrong?<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought!<\/p>\n<\/div>\n<h2 id=\"solution\">Solution<a href=\"#solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>When we look at each matematician's hat we can create a sequence of <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>s and <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span>s.\nFor example, if everyone has a white hat except for the first three mathematicians, then we would have the sequence <span class=\"mathjax mathjax--inline\">\\((1,1,1,0,0,\\cdots)\\)<\/span>.<\/p>\n<p>Let us say two sequences are <em>alike<\/em> if they are the same except for a finite number of positions.\nAs an example, the sequence we get when all mathematicians have a white hat and the previous sequence are <em>alike<\/em> because they differ only in the first three positions: <span class=\"mathjax mathjax--inline\">\\((\\underline{0}, \\underline{0}, \\underline{0}, 0, 0,\\cdots)\\)<\/span>.<\/p>\n<p>For any sequence <span class=\"mathjax mathjax--inline\">\\(S\\)<\/span> we can think of all the other sequences with which <span class=\"mathjax mathjax--inline\">\\(S\\)<\/span> is <em>alike<\/em>.\nLet us call that group of <em>alike<\/em> sequences <span class=\"mathjax mathjax--inline\">\\([S]\\)<\/span>.\nWhat the mathematicians must do is: for every single group <span class=\"mathjax mathjax--inline\">\\([S]\\)<\/span> they must pick a random sequence from that group and memorise it.<\/p>\n<p>When they are given the hats, they must look at their colleagues and figure out in which group <span class=\"mathjax mathjax--inline\">\\([S]\\)<\/span> their hat sequence belongs.\nThen, they will recall the sequence they all memorized from that group and each mathematician will say that their hat is of the colour corresponding to their position on the memorized sequence.\nBy definition, only a finite number of mathematicians guesses wrong because the sequence they will recreate and the real sequence are <em>alike<\/em>, i.e. they differ only in a finite number of positions.<\/p>","summary":"How can an infinite number of mathematicians figure out their own hat colours?","date_modified":"2025-07-23T16:49:02+02:00","tags":["logic","mathematics","sequences"],"image":"\/user\/pages\/02.blog\/03.problems\/p064-infinite-mathematicians-with-hats\/thumbnail.webp"},{"title":"Problem #063 \u2013 arbitrarily many primes in arbitrarily big intervals","date_published":"2023-01-29T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/arbitrarily-many-primes-in-arbitrarily-big-intervals","url":"https:\/\/mathspp.com\/blog\/problems\/arbitrarily-many-primes-in-arbitrarily-big-intervals","content_html":"<p>Can you prove that there are arbitrarily many primes in arbitrarily big intervals?<\/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>The problem statement asks you to prove that for <em>any<\/em> positive integer number <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> you can think of,\nthere will be a certain lower bound <span class=\"mathjax mathjax--inline\">\\(n_0\\)<\/span> (that depends on <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span>) such that for <em>any<\/em> integer <span class=\"mathjax mathjax--inline\">\\(n \\geq n_0\\)<\/span>,\n<em>there is<\/em> an interval of length <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> that contains <em>exactly<\/em> <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> primes.\n(When we talk about the length of the interval, we are talking about how many integers it contains.)<\/p>\n<p>If the statement confused you a bit, that is ok.\nLet me rephrase it.<\/p>\n<p>You and I will play a game.\nI will think of a positive integer <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span>.\nNow, your job is to come up with another positive integer <span class=\"mathjax mathjax--inline\">\\(n_0\\)<\/span> such that,\nif I pick a number <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> greater than <span class=\"mathjax mathjax--inline\">\\(n_0\\)<\/span>,\nyou can always find an interval of size <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> that contains <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> primes.<\/p>\n<p>For example, if I thought of <span class=\"mathjax mathjax--inline\">\\(k = 5\\)<\/span>, you <em>could not<\/em> pick <span class=\"mathjax mathjax--inline\">\\(n_0 = 4\\)<\/span>.\nWhy not?\nBecause if I pick <span class=\"mathjax mathjax--inline\">\\(n = 4\\)<\/span>, there is <em>no<\/em> interval of length <span class=\"mathjax mathjax--inline\">\\(4\\)<\/span> that contains <span class=\"mathjax mathjax--inline\">\\(5\\)<\/span> prime numbers...\nEspecially because an interval of length <span class=\"mathjax mathjax--inline\">\\(4\\)<\/span> contains only <span class=\"mathjax mathjax--inline\">\\(4\\)<\/span> integers!<\/p>\n<p>This problem was posed to me by my mathematician cousin and I confess that worried me a bit.\nFunnily enough, the problem has a surprisingly simple solution.\n(I am not saying you will get there easily.\nI am just saying that once you do, you will realise the solution was not very complicated.)<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought!<\/p>\n<\/div>\n<p><strong>Remember<\/strong>:<\/p>\n<ul><li>there are infinitely many primes; however<\/li>\n<li>they become scarcer and scarcer the further you go down the number line.<\/li>\n<\/ul><h2 id=\"solvers\">Solvers<a href=\"#solvers\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Congratulations &#127881; to everyone who managed to solve this problem:\nCongratulations to you if you managed to solve this problem correctly!\nIf you did, feel free to<\/p>\n<ul><li>Rodrigo G. S., Portugal &#127477;&#127481;  (&lt;- example);<\/li>\n<\/ul><p>If <em>you<\/em> managed to solve this problem, you can <a href=\"https:\/\/github.com\/mathspp\/mathspp\/edit\/master\/pages\/02.blog\/02.problems\/p063-arbitrarily-many-primes-in-arbitrarily-big-intervals\/item.md\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">add your name to the list<\/a>!\nYou can also <a href=\"mailto:rodrigo@mathspp.com?subject=Solution%20to%20Problem%20#063%20%E2%80%93%20arbitrarily%20many%20primes%20in%20arbitrarily%20big%20intervals\" class=\"mailto\">email me<\/a> your solution and we can discuss it.<\/p>\n<h2 id=\"solution\">Solution<a href=\"#solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>A thing I like about this problem is that not only can you prove that interesting statement about the prime numbers, but you can also determine exactly what the lower bound <span class=\"mathjax mathjax--inline\">\\(n_0\\)<\/span> is.<\/p>\n<p>Let us say that <span class=\"mathjax mathjax--inline\">\\(p_k\\)<\/span> is the <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span>-th prime.\nThen, if we set <span class=\"mathjax mathjax--inline\">\\(n_0 = p_k\\)<\/span>, we are good to go.\nLet me show you why.<\/p>\n<p>Suppose that <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> is any integer <span class=\"mathjax mathjax--inline\">\\(n \\geq n_0\\)<\/span>.\nThen, the interval <span class=\"mathjax mathjax--inline\">\\([1, n]\\)<\/span> contains <span class=\"mathjax mathjax--inline\">\\(p_k\\)<\/span> in it.\nWhy?\nBecause <span class=\"mathjax mathjax--inline\">\\(p_k = n_0\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(n \\geq n_0\\)<\/span>.<\/p>\n<p>So, the interval <span class=\"mathjax mathjax--inline\">\\([1, n]\\)<\/span> contains <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> <em>or more<\/em> prime numbers.\nIf it contains <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> prime numbers, we just found our interval of length <span class=\"mathjax mathjax--inline\">\\(n\\)<\/span> that contains exactly <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> primes.\nIf it contains more than <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> primes, we must do something about it.<\/p>\n<p>If the interval <span class=\"mathjax mathjax--inline\">\\([1, n]\\)<\/span> contains more than <span class=\"mathjax mathjax--inline\">\\(k\\)<\/span> prime numbers, then we start sliding the interval to the right, like so:...<\/p>","summary":"Can you prove that there are arbitrarily many primes in arbitrarily big intervals? In this mathematical problem, which is just a logic challenge, the solution is surprisingly uncomplicated.","date_modified":"2025-07-23T16:49:02+02:00","tags":["logic","mathematics","number theory","primes"],"image":"\/user\/pages\/02.blog\/03.problems\/p063-arbitrarily-many-primes-in-arbitrarily-big-intervals\/thumbnail.webp"},{"title":"Problem #062 \u2013 sliding coins","date_published":"2022-06-12T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/problems\/sliding-coins","url":"https:\/\/mathspp.com\/blog\/problems\/sliding-coins","content_html":"<p>Can you align all of the coins on the right edge of the board?<\/p>\n\n<figure class=\"image-caption\"><img title=\"Diagram of the coin positions for the problem.\" alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p062-sliding-coins\/_setup.webp\"><figcaption class=\"\">Diagram of the coin positions for the problem.<\/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>The diagram above shows a 3 by 4 grid with 3 coins, one per row and one per column,\nsuch that the rightmost column of the grid is empty.<\/p>\n<p>You are allowed to slide the coins to the columns on their right,\nbut the coins can never leave the board (nor can they be moved back to the left).\nAlso, you always have to move two coins at a time.<\/p>\n<p>Thus, this is a legal move:<\/p>\n<figure class=\"image-caption\"><img title=\"A legal move sliding the top two coins one slot to the right.\" alt='\"The diagram showing an example of a legal move with some arrows.\"' src=\"\/user\/pages\/02.blog\/03.problems\/p062-sliding-coins\/_legal_move.webp\"><figcaption class=\"\">A legal move sliding the top two coins one slot to the right.<\/figcaption><\/figure><p>But moving a single coin, such as in the diagram below, is not allowed:<\/p>\n<figure class=\"image-caption\"><img title=\"An illegal move that slides only one coin to the right.\" alt='\"The diagram showing an example of an illegal move.\"' src=\"\/user\/pages\/02.blog\/03.problems\/p062-sliding-coins\/_illegal_move.webp\"><figcaption class=\"\">An illegal move that slides only one coin to the right.<\/figcaption><\/figure><p>Your objective is to reach this position:<\/p>\n<figure class=\"image-caption\"><img title=\"Target position.\" alt='\"The same 3 by 4 grid containing a coin per row, with all coins on the rightmost column.\"' src=\"\/user\/pages\/02.blog\/03.problems\/p062-sliding-coins\/_objective.webp\"><figcaption class=\"\">Target position.<\/figcaption><\/figure><p>Can you do it?<\/p>\n<p>What if you extend the board two columns to the left and two rows up,\nand add another two coins along the diagonal?<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought!<\/p>\n<\/div>\n<p>This is an adaptation of a problem from the <a href=\"https:\/\/amzn.to\/3NJUn5D\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">book &ldquo;Algorithmic Puzzles&rdquo;<\/a> by Anany Levitin and Maria Levitin.<sup id=\"fnref1:1\"><a href=\"#fn:1\" class=\"footnote-ref\">1<\/a><\/sup><\/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#062%20%E2%80%93%20sliding%20coins\" 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>You can move all coins to the right in 3 moves:<\/p>\n<ol><li>move the top and bottom coins (this aligns the top and middle coins while placing the bottom coin in the rightmost column);<\/li>\n<li>move the top and middle coins; and<\/li>\n<li>move the top and middle coins again.<\/li>\n<\/ol><p>This diagram shows the evolution of the grid:<\/p>\n<pre><code>X . . .  start\n. X . .\n. . X .\n\n. X . . &gt;\n. X . .\n. . . X &gt;\n\n. . X . &gt;\n. . X . &gt;\n. . . X\n\n. . . X &gt;\n. . . X &gt;\n. . . X<\/code><\/pre>\n<p>If we extend the board up and left, we'd have this starting configuration:<\/p>\n<pre><code>X . . . . .\n. X . . . .\n. . X . . .\n. . . X . .\n. . . . X .<\/code><\/pre>\n<p>Now, consider the total number of spaces to the right of the coins:<\/p>\n<pre><code>X 1 2 3 4 5\n. X 6 7 8 9\n. . X 0 1 2\n. . . X 3 4\n. . . . X 5<\/code><\/pre>\n<p>We see that there are 15 spaces on the grid to the right of coins.\nWe also know that each time we move two coins, the number of spaces to the right of the coins decreases by 2.\nNow, if we were to align all coins on the rightmost column, the number of spaces to the right of the coins would be 0.\nHowever, we can't...<\/p>","summary":"Can you align all of the coins on the right edge of the board?","date_modified":"2025-07-23T16:49:02+02:00","tags":["invariants","logic","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p062-sliding-coins\/thumbnail.webp"},{"title":"Problem #061 \u2013 flower garden","date_published":"2022-05-22T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/problems\/flower-garden","url":"https:\/\/mathspp.com\/blog\/problems\/flower-garden","content_html":"<p>Three mathematicians discuss a beautiful flower garden and the coloured flowers within.<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Norikio Yamamoto on Unsplash.\" alt=\"\" src=\"\/images\/1\/8\/e\/d\/4\/18ed401e500df1761a10827de6131b5af1dca164-thumbnail.png\"><figcaption class=\"\">Photo by Norikio Yamamoto 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>Three mathematicians were discussing a beautiful flower garden.\nThe first mathematician said:<\/p>\n<blockquote>\n<p>\u201cIt's a beautiful garden.\nAll flowers are either red, yellow, or blue...\nAnd whatever three flowers you pick, one of them is bound to be red.\u201d<\/p>\n<\/blockquote>\n<p>The second mathematician said:<\/p>\n<blockquote>\n<p>\u201cIt is indeed a beautiful garden!\nThere are red flowers, yellow flowers, and blue flowers!\nI also noticed that, whatever three flowers you pick, one of them is bound to be yellow.\u201d<\/p>\n<\/blockquote>\n<p>The third mathematician, who had never been to the garden, remarked:<\/p>\n<blockquote>\n<p>\u201cThat's interesting!\nIf that's true, I can say that, whatever three flowers I pick, one of them is bound to be blue!\u201d<\/p>\n<\/blockquote>\n<p>The first two mathematicians glanced at each other and smiled.<\/p>\n<p>Is the third mathematician right? Or wrong? Why?<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought!<\/p>\n<\/div>\n<p>This problem is from the amazing [book \u201cTo Mock a Mockingbird\u201d][mock-mockingbird] by the mathematician Raymond Smullyan.<sup id=\"fnref1:1\"><a href=\"#fn:1\" class=\"footnote-ref\">1<\/a><\/sup><\/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>Christ van W., The Netherlands;<\/li>\n<li>David R., Mexico;<\/li>\n<li>Micha\u0142 D., Poland;<\/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#061%20%E2%80%93%20flower%20garden\" 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>The third mathematician is right.<\/p>\n<p>The first sentence of each mathematician establishes that all flowers in the garden are one of three colours (red, yellow, blue) and that all colours are represented; that is, there is <em>at least<\/em> one red flower, <em>at least<\/em> one yellow flower, and <em>at least<\/em> one blue flower.<\/p>\n<p>Then, the first mathematician says that whatever three flowers you pick, one is bound to be red.\nThis means that the garden cannot contain three or more flowers that are not red.\nAfter all, if there were, say, two yellow flowers and one blue flower,\nyou could pick those three and there wouldn't be a red flower in there.<\/p>\n<p>At the same time, we know there has to be at least one yellow flower and one blue flower,\nso we conclude the garden has an unknown number of red flowers, one yellow flower, and one blue flower.<\/p>\n<p>Then, the second mathematician speaks!\nFrom their remark, we could make a similar independent conclusion:\nthe garden has an unknown number of yellow flowers, one red flower, and one blue flower.<\/p>\n<p>Thus, if we combine the two conclusions,\nwe get that the garden has three flowers only:<\/p>\n<ul>\n<li>one red flower;<\/li>\n<li>one yellow flower; and<\/li>\n<li>one blue flower.<\/li>\n<\/ul>\n<p>Therefore, the third mathematician is also right:\nwhatever three flowers you pick, one is bound to be blue.<\/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>\n<div class=\"footnotes\">\n<hr>\n<ol>\n<li id=\"fn:1\">\n<p>This is an Amazon Affiliate link and I may earn a commission if you purchase the book. This comes at no extra cost to you.\u00a0<a href=\"#fnref1:1\" rev=\"footnote\" class=\"footnote-backref\">\u21a9<\/a><\/p>\n<\/li>\n<\/ol>\n<\/div>","summary":"Three mathematicians discuss a beautiful flower garden and the coloured flowers within.","date_modified":"2025-07-23T16:49:02+02:00","tags":["logic","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p061-flower-garden\/thumbnail.png"},{"title":"Problem #060 \u2013 realtor commissions","date_published":"2022-05-08T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/problems\/realtor-commissions","url":"https:\/\/mathspp.com\/blog\/problems\/realtor-commissions","content_html":"<p>Two realtors discuss who's netting the award for highest average commission, but it isn't clear who the winner is...<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Dillon Kydd on Unsplash.\" alt=\"\" src=\"\/images\/a\/8\/6\/6\/4\/a866400e87cf5e1d99f342e7d749ed4cd06fa08a-thumbnail.png\"><figcaption class=\"\">Photo by Dillon Kydd 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>Alice and Bob are two realtors that work at the same premium real estate agency, WSH.\nBecause WSH really wants to focus on selling premium properties,\nthey instated a monetary prize that is awarded every two years to the realtor that has the highest average commission over the past two years.<\/p>\n<p>The award for the years 2020 and 2021 is being attributed soon.\nOne day, at the office, Alice overheard Bob gloating:<\/p>\n<blockquote>\n<p>&ldquo;I can't wait to get my money! I already know what I'll be doing with it!&rdquo;<\/p>\n<\/blockquote>\n<p>Alice turned to Bob and asked:<\/p>\n<blockquote>\n<p>&ldquo;How can you be so sure that you are the one netting the prize?&rdquo;<\/p>\n<\/blockquote>\n<p>Bob smirked and returned:<\/p>\n<blockquote>\n<p>&ldquo;I guess you haven't been paying attention, Alice!\nIn 2020 I was the realtor with the highest average commission over that year...\nAnd in 2021 I did it again!&rdquo;<\/p>\n<\/blockquote>\n<p>Alice smiled and returned to work.<\/p>\n<p>Who do you think is right?\nIs Bob definitely getting the award?\nOr is there a scenario in which Alice gets the prize?<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought!<\/p>\n<\/div>\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<li>Michael W., US;<\/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#060%20%E2%80%93%20realtor%20commissions\" 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>Bob is wrong because there is a scenario in which Alice can win the award...\nEven though Bob had the highest average commission both in 2020 and 2021.\nDoes this seem counterintuitive?\nBecause it is...\nThis is an instance of the <a href=\"\/blog\/til\/simpsons-paradox\">Simpson's Paradox<\/a>.<\/p>\n<p>To paint a clearer picture of how the Simpson's Paradox comes about,\nI will create a fairly extreme hypothetical scenario:<\/p>\n<p>Suppose that Alice and Bob are the <em>only<\/em> realtors working for WSH,\nso that we don't have to care about anyone else.<\/p>\n<p>Suppose that, in 2020, these were the commissions each one made:<\/p>\n<ul><li>Bob sold a single house and made a $10,005 commission.<\/li>\n<li>Alice sold a single house and made a $10,000 commission.<\/li>\n<\/ul><p>Furthermore, suppose that, in 2021, these were the commissions each one made:<\/p>\n<ul><li>Bob sold a single house and made a $20,005 commission.<\/li>\n<li>Alice sold 1000 houses and made a $20,000 commission <em>on each one<\/em>.<\/li>\n<\/ul><p>Here is a table summarising the average commission each one of them made for each year:<\/p>\n<table><thead><tr><th style=\"text-align: right;\"><\/th>\n<th style=\"text-align: right;\">2020<\/th>\n<th style=\"text-align: right;\">2021<\/th>\n<\/tr><\/thead><tbody><tr><td style=\"text-align: right;\">Alice<\/td>\n<td style=\"text-align: right;\"><span class=\"mathjax mathjax--inline\">\\(\\frac{1 \\times 10000}{1} = 10000\\)<\/span><\/td>\n<td style=\"text-align: right;\"><span class=\"mathjax mathjax--inline\">\\(\\frac{1000 \\times 20000}{1000} = 20000\\)<\/span><\/td>\n<\/tr><tr><td style=\"text-align: right;\">Bob<\/td>\n<td style=\"text-align: right;\"><span class=\"mathjax mathjax--inline\">\\(\\frac{1 \\times 10005}{1} = 10005\\)<\/span><\/td>\n<td style=\"text-align: right;\"><span class=\"mathjax mathjax--inline\">\\(\\frac{1 \\times 20005}{1} = 20005\\)<\/span><\/td>\n<\/tr><\/tbody><\/table><p>As we can see, Bob has a higher average commission in 2020...\nBut also in 2021!<\/p>\n<p>However, if we add a third column for 2020 and 2021 combined,\neverything becomes clear:<\/p>\n<table><thead><tr><th style=\"text-align: right;\"><\/th>\n<th style=\"text-align: right;\">2020<\/th>\n<th style=\"text-align: right;\">2021<\/th>\n<th style=\"text-align: right;\">2020 + 2021<\/th>\n<\/tr><\/thead><tbody><tr><td style=\"text-align: right;\">Alice<\/td>\n<td style=\"text-align: right;\"><span class=\"mathjax mathjax--inline\">\\(\\frac{1 \\times 10000}{1} = 10000\\)<\/span><\/td>\n<td style=\"text-align: right;\"><span class=\"mathjax mathjax--inline\">\\(\\frac{1000 \\times 20000}{1000} = 20000\\)<\/span><\/td>\n<td style=\"text-align: right;\"><span class=\"mathjax mathjax--inline\">\\(\\frac{1 \\times 10000 + 1000 \\times 20000}{1001} \\approx 19990\\)<\/span><\/td>\n<\/tr><tr><td style=\"text-align: right;\">Bob<\/td>\n<td style=\"text-align: right;\"><span class=\"mathjax mathjax--inline\">\\(\\frac{1 \\times 10005}{1} = 10005\\)<\/span><\/td>\n<td style=\"text-align: right;\"><span class=\"mathjax mathjax--inline\">\\(\\frac{1...<\/span><\/td><\/tr><\/tbody><\/table>","summary":"Two realtors discuss who&#039;s netting the award for highest average commission, but it isn&#039;t clear who the winner is...","date_modified":"2025-07-23T16:49:02+02:00","tags":["logic","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p060-realtor-commissions\/thumbnail.png"},{"title":"Problem #059 \u2013 marching pegs","date_published":"2022-04-17T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/problems\/marching-pegs","url":"https:\/\/mathspp.com\/blog\/problems\/marching-pegs","content_html":"<p>How can you swap the coloured pegs if they can only march forward?<\/p>\n\n<script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><figure class=\"image-caption\"><img title=\"Photo of the game that inspired this post.\" alt=\"\" src=\"\/images\/5\/7\/0\/2\/d\/5702d4535bacf7a8225d3d31c65de6e356e81d75-thumbnail.png\"><figcaption class=\"\">Photo of the game that inspired this post.<\/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>Imagine seven round slots:<\/p>\n<pre><code class=\"language-txt\">O O O O O O O<\/code><\/pre>\n<p>In the three left slots, you have Yellow pegs:<\/p>\n<pre><code class=\"language-txt\">Y Y Y O O O O<\/code><\/pre>\n<p>In the three right slots, you have Green pegs:<\/p>\n<pre><code class=\"language-txt\">Y Y Y O G G G<\/code><\/pre>\n<p>You have to swap the yellow pegs and the green ones,\nbut of course there are some restrictions to what you can do:<\/p>\n<ul><li>pegs can only move forward (that is, yellow pegs can only move to the right and green pegs can only move to the left); and<\/li>\n<li>pegs can only move:\n<ul><li>to the next (adjacent) slot if it is available;<\/li>\n<li>to the second next slot, if that means the peg &ldquo;jumps over&rdquo; a peg of another colour.<\/li>\n<\/ul><\/li>\n<\/ul><p>So, for example, in the configuration below, the green peg can move to the marked slot by jumping over a yellow peg:<\/p>\n<pre><code class=\"language-txt\">G Y O\n^   ^<\/code><\/pre>\n<p>As another example, in the situation below, no peg can move:<\/p>\n<pre><code class=\"language-txt\">O G G Y Y O<\/code><\/pre>\n<p>With these restrictions, can you swap all pegs?\nThat is, can you move the pegs so that all green pegs end up on the left and all yellow pegs end up on the right?<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought!<\/p>\n<p>You can try playing this out yourself: grab 6 coins and lay them in a straight line on a table.\n3 coins on the left with the tails face up and the other 3 coins on the right with the other face up.<\/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 is based off of the puzzle you can see in the thumbnail picture,\nwhich you can play at <a href=\"https:\/\/iconparkorlando.com\/attractions\/museum-of-illusions-orlando\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Icon Park's Museum of Illusions<\/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><li>David H., Taiwan;<\/li>\n<li>Michael W., US;<\/li>\n<li>Zech Z., US;<\/li>\n<li>Michael H., US;<\/li>\n<li>Tanishk S., India;<\/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#059%20%E2%80%93%20marching%20pegs\" 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>To solve this problem, my best suggestion is to go one move at a time,\nand to try and think ahead 2 or 3 moves each time you are about to do something.\nYou don't need to look ahead too much to be able to tell if a move is a bad idea or not.<\/p>\n<p>With that said, that can be easier for some and harder for others!<\/p>\n<p>I will share one possible solution below.\nEach line represents the positions of the pegs after one single move.\nBut first, let me show you a video shared on Twitter where one of you solved this problem with candy:<\/p>\n<blockquote class=\"twitter-tweet\" data-conversation=\"none\" data-theme=\"dark\">\n<p lang=\"en\" dir=\"ltr\">G1 forward, Y1 hop, Y2 forward, G1 hop, G2 hop, G3 forward, Y1 hop, Y2 hop, Y3 hop, G1 forward, G2 hop, G3 hop, Y2 forward, Y3 hop, G3 forward,...<\/p><\/blockquote>","summary":"How can you swap the coloured pegs if they can only march forward?","date_modified":"2025-07-23T16:49:02+02:00","tags":["logic","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p059-marching-pegs\/thumbnail.png"},{"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":"Solving Wordle with APL","date_published":"2022-03-28T15:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/solving-wordle-with-apl","url":"https:\/\/mathspp.com\/blog\/solving-wordle-with-apl","content_html":"<p>Join me in solving the word game Wordle in (Dyalog) APL.<\/p>\n\n<figure class=\"image-caption\"><img title=\"A screenshot of a session of Wordle I played.\" alt=\"\" src=\"\/images\/2\/f\/1\/8\/4\/2f1843fc9239532c53c2d0d2ca0b46632768f425-thumbnail.webp\"><figcaption class=\"\">A screenshot of a session of Wordle I played.<\/figcaption><\/figure><h2 id=\"introduction\">Introduction<a href=\"#introduction\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p><a href=\"https:\/\/www.nytimes.com\/games\/wordle\/index.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Wordle<\/a> is a conceptually simple game that you can play online.\n(For those of you who know it, it's like <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mastermind_(board_game)\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Mastermind<\/a> (the board game),\nbut with words.)\nThere is a secret word that you have to guess and you are given 6 tries\nto find out what word is the secret word.\nWhen you make a guess, you get some information back:<\/p>\n<ul><li>you are told which letters of the guess match letters of the secret word;<\/li>\n<li>you are told which letters of the guess exist in the secret word,\nbut are in the wrong position in the guess; and<\/li>\n<li>you are told which letters of the guess don't exist in the secret word.<\/li>\n<\/ul><p>For example, assume that the secret word is &ldquo;chess&rdquo;, and you guess &ldquo;caves&rdquo;:<\/p>\n<ul><li>the &ldquo;c&rdquo; and the &ldquo;s&rdquo; are in their correct positions when compared to the secret word;<\/li>\n<li>the letter &ldquo;e&rdquo; in &ldquo;caves&rdquo; exists in the secret word but it's in the wrong position; and<\/li>\n<li>the letters &ldquo;a&rdquo; and &ldquo;v&rdquo; do not exist in the secret word.<\/li>\n<\/ul><p>Here is how the game would represent this information:<\/p>\n<figure class=\"image-caption\"><img title=\"The visual representation of the letter information.\" alt=\"The word &ldquo;caves&rdquo; where the &ldquo;c&rdquo; and the &ldquo;s&rdquo; are highlighted in green, the &ldquo;e&rdquo; is in yellow, and the &ldquo;a&rdquo; and the &ldquo;v&rdquo; are shaded in grey.\" src=\"\/user\/pages\/02.blog\/solving-wordle-with-apl\/_caves.webp\"><figcaption class=\"\">The visual representation of the letter information.<\/figcaption><\/figure><p>Again, recall that:<\/p>\n<ul><li>the green squares around &ldquo;c&rdquo; and &ldquo;s&rdquo; tell you that those letters are in the correct place;<\/li>\n<li>the yellow square around &ldquo;e&rdquo; tells you that &ldquo;e&rdquo; exists in the secret word but in some other position; and<\/li>\n<li>the gray squares around &ldquo;a&rdquo; and &ldquo;v&rdquo; tell you that &ldquo;a&rdquo; and &ldquo;v&rdquo; are not in the secret word.<\/li>\n<\/ul><h2 id=\"objective\">Objective<a href=\"#objective\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>What we want to do is write an <a href=\"https:\/\/apl.wiki\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">APL<\/a> program that helps us play Wordle.\n(I have written <a href=\"\/blog\/solving-wordle-with-python\">a similar article<\/a> in the past, but using Python.)<\/p>\n<p>We will write a couple of functions:<\/p>\n<ul><li>a function <code>Score<\/code> that scores a given guess with respect to a given secret word; and<\/li>\n<li>a function <code>Filter<\/code> that returns the possible secret words with respect to a given guess and its score.<\/li>\n<\/ul><div class=\"notices yellow\">\n<p>All the code is available in <a href=\"https:\/\/github.com\/rodrigogiraoserrao\/WordleAPL\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this GitHub repository<\/a>.<\/p>\n<\/div>\n<p>As for the letter information, which I'll call the score of a guess, we will encode it in this way:<\/p>\n<ul><li>a <code>0<\/code> means the letter isn't in the secret word;<\/li>\n<li>a <code>1<\/code> means the letter is in the incorrect position; and<\/li>\n<li>a <code>2<\/code> means the letter is in the correct position.<\/li>\n<\/ul><p>Here are a couple of examples:<\/p>\n<table><thead><tr><th style=\"text-align: left;\">Secret word<\/th>\n<th style=\"text-align: left;\">Guess<\/th>\n<th style=\"text-align: left;\">Score<\/th>\n<\/tr><\/thead><tbody><tr><td style=\"text-align: left;\">chess<\/td>\n<td style=\"text-align: left;\">caves<\/td>\n<td style=\"text-align: left;\"><code>2 0 0 1 2<\/code><\/td>\n<\/tr><tr><td style=\"text-align: left;\">chess<\/td>\n<td style=\"text-align: left;\">swiss<\/td>\n<td style=\"text-align: left;\"><code>0 0 0 2 2<\/code><\/td>\n<\/tr><tr><td style=\"text-align: left;\">talon<\/td>\n<td style=\"text-align: left;\">fault<\/td>\n<td style=\"text-align: left;\"><code>0 2 0 1 1<\/code><\/td>\n<\/tr><\/tbody><\/table><p>Take a second to make sure you understand all of the scores above.<\/p>\n<h2 id=\"getting-some-data\">Getting some data<a href=\"#getting-some-data\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The first thing we will do is <a href=\"https:\/\/raw.githubusercontent.com\/rodrigogiraoserrao\/WordleAPL\/main\/WORD.LST\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">grab a list of words<\/a> to play with.\nIf I'm not mistaken, the list I linked to is a free list of the Scrabble dictionary.<\/p>\n<p>(If you are on a Unix system, you may also get away with just using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Words_(Unix)\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">\n  <code>words<\/code>\n<\/a> file.)<\/p>\n<p>Now that we have some data,...<\/p>","summary":"Join me in solving the word game Wordle in (Dyalog) APL.","date_modified":"2025-07-23T16:49:02+02:00","tags":["apl","game","logic","programming"],"image":"\/user\/pages\/02.blog\/solving-wordle-with-apl\/thumbnail.webp"},{"title":"Look-and-say sequence","date_published":"2022-03-18T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/look-and-say-sequence","url":"https:\/\/mathspp.com\/blog\/look-and-say-sequence","content_html":"<p>We discuss the look-and-say sequence, its behaviour, variations of it, and a Python implementation.<\/p>\n\n<figure class=\"image-caption\"><img title=\"The first few terms of the look-and-say sequence.\" alt=\"A screenshot of a Numberphile video on the look-and-say sequence displaying the first terms of the look-and-say sequence.\" src=\"\/user\/pages\/02.blog\/look-and-say-sequence\/thumbnail.webp\"><figcaption class=\"\">The first few terms of the look-and-say sequence.<\/figcaption><\/figure><h2 id=\"what-is-the-look-and-say-sequence\">What is the look-and-say sequence?<a href=\"#what-is-the-look-and-say-sequence\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The look-and-say sequence is a numerical sequence that typically starts with 1 and then has the following terms:<\/p>\n<ul><li>1<\/li>\n<li>11<\/li>\n<li>21<\/li>\n<li>1211<\/li>\n<li>111221<\/li>\n<li>312211<\/li>\n<li>...<\/li>\n<\/ul><p>(Can you guess the next one?)<\/p>\n<p>The way the sequence works is that you read the digits aloud to build the next term.\nFor example, how would you read the term <code>312211<\/code>?\nYou would read it as &ldquo;one 3, one 1, two 2s, and two 1s&rdquo;, giving <code>13112221<\/code>.<\/p>\n<p>The sequence is usually started with <code>1<\/code>,\nbut we can actually seed the sequence (start the sequence) with any other term we want.<\/p>\n<p>For example, what if we start the sequence with <code>312<\/code>?\nThen we would get<\/p>\n<ul><li>312<\/li>\n<li>131112<\/li>\n<li>11133112<\/li>\n<li>31232112<\/li>\n<li>13111213122112<\/li>\n<li>...<\/li>\n<\/ul><p>So, the look-and-say sequence is often associated with the sequence that starts with 1,\nbut the rule makes sense in other contexts as well.<\/p>\n<h2 id=\"conway-s-constant\">Conway's constant<a href=\"#conway-s-constant\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>A couple of days ago <a href=\"\/blog\/til\/038\">I learned<\/a> that <a href=\"https:\/\/en.wikipedia.org\/wiki\/John_Horton_Conway\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">John Conway<\/a> was able to determine by how much all these sequences grow!\nAs you progress through the sequence, the length of the previous term is supposed to be <span class=\"mathjax mathjax--inline\">\\(\\approx 30\\%\\)<\/span> greater than the length of the previous term.\nTo be more precise, the length of the previous term tends to be <span class=\"mathjax mathjax--inline\">\\(1.303577269\\cdots\\)<\/span> times the length of the previous one.\nThis is the value of Conway's constant, and it's <a href=\"\/blog\/til\/038#conways-constant\">the only positive real root of a polynomial of degree 71<\/a>.<\/p>\n<p>Like I said, this constant governs the growth of any of these sequences, regardless of the start point...\nAlmost!<\/p>\n<p>There is <em>just one<\/em> seed for which this doesn't hold true.\nCan you figure out which one?<\/p>\n<div class=\"notices blue\">\n<p>Think about it.\nYou can find the answer below.<\/p>\n<\/div>\n<h2 id=\"python-implementation\">Python implementation<a href=\"#python-implementation\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>After <a href=\"https:\/\/twitter.com\/mathsppblog\/status\/1503823606899519489\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">talking about this sequence on Twitter<\/a>, I wrote a Python function that applies the look-and-say rule to a list of digits.\nSo, before you keep reading, can you implement such a function?\nWrite a function that accepts a list of digits and returns the list with the digits of the next term of the sequence.<\/p>\n<p>Here is how I did it:<\/p>\n<p>First, how does the sequence work?\nThe next term is created by looking at the previous one and saying what you are seeing.\nThe key here is understanding that we want to look at groups of consecutive equal digits, right?<\/p>\n<p>That's how we go from 111221 to 312211:<\/p>\n<pre><code class=\"language-txt\"> 111 | 22 | 1\n   31   22  11<\/code><\/pre>\n<p>And from 312211 to 13112221:<\/p>\n<pre><code class=\"language-txt\"> 3 | 1 | 22 | 11\n 13  11   22  21<\/code><\/pre>\n<p>How can we do this in Python?\nThis grouping functionality is perfect for one tool from the <code>itertools<\/code> module,\n<code>itertools.groupby<\/code>.\n<code>groupby<\/code> returns consecutive keys and groups from an iterable;\nthe keys are the unique elements and the groups are the runs of unique elements.<\/p>\n<p>Here are some examples to show how <code>groupby<\/code> works:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; from...<\/code><\/pre>","summary":"We discuss the look-and-say sequence, its behaviour, variations of it, and a Python implementation.","date_modified":"2025-07-23T16:49:02+02:00","tags":["logic","mathematics","programming","python","sequences"],"image":"\/user\/pages\/02.blog\/look-and-say-sequence\/thumbnail.webp"}]}
