
    
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/arithmetic","feed_url":"https:\/\/mathspp.com\/blog\/tags\/arithmetic.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"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":"divmod for unit conversions","date_published":"2023-11-27T19:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/divmod-for-unit-conversions","url":"https:\/\/mathspp.com\/blog\/divmod-for-unit-conversions","content_html":"<p>This article shows how to use the Python built-in <code>divmod<\/code> for unit conversions.<\/p>\n\n<p>The built-in <code>divmod<\/code> is the built-in that tells me that 45813 seconds is the same as 12 hours, 43 minutes, and 33 seconds.\nBut how?<\/p>\n<p>The built-in <code>divmod<\/code> gets its name from two arithmetic operations you probably know:<\/p>\n<ol><li>DIVision; and<\/li>\n<li>MODulo.<\/li>\n<\/ol><p>So, the built-in <code>divmod<\/code> takes two arguments and returns two numbers.\nThe first one is the division of the arguments and the second one is the module of the arguments.\nSounds simple, right?\nThat's because it is!<\/p>\n<pre><code class=\"language-pycon\">&gt;&gt;&gt; x, y = 23, 10\n&gt;&gt;&gt; divmod(x, y)\n(2, 3)\n&gt;&gt;&gt; (x \/\/ y, x % y)\n(2, 3)\n\n&gt;&gt;&gt; x, y = 453, 19\n&gt;&gt;&gt; divmod(x, y)\n(23, 16)\n&gt;&gt;&gt; (x \/\/ y, x % y)\n(23, 16)<\/code><\/pre>\n<p>So, when can <code>divmod<\/code> be useful?\nLiterally when you want to divide two numbers and also know what's the remainder of that division.<\/p>\n<p>In practice, <code>divmod<\/code> shows up a lot when converting one small unit to more convenient units.\nThat's a common use case, at least.<\/p>\n<h2 id=\"example-with-time-units\">Example with time units<a href=\"#example-with-time-units\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>45813 seconds is A LOT of seconds and most people just prefer to use the units of hours and minutes to talk about that many seconds!\nSo, how do we convert 45813 into its corresponding number of hours and minutes?<\/p>\n<p>We can use <code>divmod<\/code> to divide 45813 by 60, which will convert 45813 seconds to minutes, and also let us know how many seconds couldn't be converted to minutes:<\/p>\n<pre><code class=\"language-pycon\">&gt;&gt;&gt; divmod(45813, 60)\n(763, 33)<\/code><\/pre>\n<p>Then, we take the total number of minutes and repeat the same process to figure out how many hours we have.<\/p>\n<pre><code class=\"language-pycon\">&gt;&gt;&gt; divmod(763, 60)\n(12, 43)<\/code><\/pre>\n<p>If you put everything together, it looks like this:<\/p>\n<pre><code class=\"language-pycon\">&gt;&gt;&gt; seconds = 45813\n&gt;&gt;&gt; minutes, seconds = divmod(seconds, 60)\n&gt;&gt;&gt; hours, minutes = divmod(minutes, 60)\n&gt;&gt;&gt; hours, minutes, seconds\n(12, 43, 33)<\/code><\/pre>\n<p>Is this making sense?\nTry to convert 261647 seconds into days, hours, minutes, and seconds!<\/p>\n<h2 id=\"example-with-grid-click\">Example with grid click<a href=\"#example-with-grid-click\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>I have another example that you might enjoy.\nThere's this weird game I play sometimes called &ldquo;meta tic-tac-toe&rdquo;.\n(At least, that's what we called it in uni.)\nIt's 9 tic-tac-toe games embedded in a larger tic-tac-toe game.<\/p>\n<p>The board is shown below:<\/p>\n<figure class=\"image-caption\"><img title=\"The board of a meta tic-tac-toe game.\" alt=\"A meta tic-tac-toe game board which is a large tic-tac-toe board which contains nine smaller tic-tac-toe boards, one in each cell of the bigger board.\" src=\"\/user\/pages\/02.blog\/divmod-for-unit-conversions\/_tictactoe.webp\"><figcaption class=\"\">The board of a meta tic-tac-toe game.<\/figcaption><\/figure><p>The rules aren't SUPER important now (although it's a pretty fun game).\nWhat matters now is that I wanted to implement this game in Python.<\/p>\n<p>To do that, when a user clicks on the board, I need to know what's the purple (outer) cell that is being clicked.\nI also need to know what's the orange (inner) cell that is being clicked!\nThe question is:\nhow would I compute that?<\/p>\n<p>Suppose the board is a 900 pixel square and everything is evenly spaced.\nIf I click at position <code>(423, 158)<\/code>, what are the outer and inner cells I clicked?<\/p>\n<figure class=\"image-caption\"><img title=\"A click on the board at position (423, 158).\" alt=\"A meta tic-tac-toe board with a ruler showing that the board is a 900 pixel square with the outer cells being 300 pixels by 300 pixels, while the inner cells of the inner boards are 100 pixel squares.\" src=\"\/user\/pages\/02.blog\/divmod-for-unit-conversions\/_tictactoe_play.webp\"><figcaption class=\"\">A click on the board at position (423, 158).<\/figcaption><\/figure><p>We can figure out the outer and inner cells with...<\/p>","summary":"This article shows how to use the Python built-in divmod for unit conversions.","date_modified":"2025-07-23T16:49:02+02:00","tags":["arithmetic","mathematics","programming","python"],"image":"\/user\/pages\/02.blog\/divmod-for-unit-conversions\/thumbnail.webp"},{"title":"Overloading arithmetic operators with dunder methods | Pydon&#039;t \ud83d\udc0d","date_published":"2023-07-31T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/pydonts\/overloading-arithmetic-operators-with-dunder-methods","url":"https:\/\/mathspp.com\/blog\/pydonts\/overloading-arithmetic-operators-with-dunder-methods","content_html":"<p>This article shows you how to overload the arithmetic operators in Python with dunder methods.<\/p>\n\n<h2 id=\"introduction\">Introduction<a href=\"#introduction\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Python lets you override the arithmetic operators like <code>+<\/code> for addition or <code>*<\/code> for multiplication through <a href=\"\/blog\/pydonts\/dunder-methods\">dunder methods<\/a>.\n<a href=\"\/blog\/pydonts\/dunder-methods\">Dunder methods<\/a> are special methods whose name starts and ends with a double underscore (hence, &ldquo;dunder&rdquo;), and some <a href=\"\/blog\/pydonts\/dunder-methods\">dunder methods<\/a> are specific to arithmetic operations.<\/p>\n<p>In this Pydon't, you will learn:<\/p>\n<ul><li>how to implement unary arithmetic operators:\n<ul><li>negation (<code>-p<\/code>);<\/li>\n<li>(<code>+p<\/code>);<\/li>\n<li>absolute value (<code>abs(p)<\/code>); and<\/li>\n<li>inverse (<code>~<\/code>).<\/li>\n<\/ul><\/li>\n<li>how to implement binary arithmetic operators:\n<ul><li>addition (<code>+<\/code>);<\/li>\n<li>subtraction (<code>-<\/code>);<\/li>\n<li>multiplication (<code>*<\/code>);<\/li>\n<li>division (<code>\/<\/code>);<\/li>\n<li>floor division (<code>\/\/<\/code>);<\/li>\n<li>modulo (<code>%<\/code>);<\/li>\n<li>(<code>divmod<\/code>); and<\/li>\n<li>exponentiation (<code>pow<\/code>).<\/li>\n<\/ul><\/li>\n<li>how to implement binary arithmetic operators for bitwise operations:\n<ul><li>left shift (<code>&lt;&lt;<\/code>);<\/li>\n<li>right shift (<code>&gt;&gt;<\/code>);<\/li>\n<li>bitwise and (<code>&amp;<\/code>);<\/li>\n<li>bitwise or (<code>|<\/code>);<\/li>\n<li>bitwise xor (<code>^<\/code>);<\/li>\n<\/ul><\/li>\n<li>what the reflected arithmetic dunder methods are;<\/li>\n<li>what the augmented arithmetic dunder methods are;<\/li>\n<li>what <code>NotImplemented<\/code> is and how it differs from <code>NotImplementedError<\/code>; and<\/li>\n<li>how Python determines which arithmetic dunder method to call.<\/li>\n<\/ul><p>We will start by explaining how dunder methods work and we will give a couple of examples by implementing the unary operators.\nThen, we introduce the mechanics behind the binary arithmetic operators, basing the examples on the binary operator <code>+<\/code>.<\/p>\n<p>After we introduce all the concepts and mechanics that Python uses to handle binary arithmetic operators, we will provide an example of a class that implements all the arithmetic dunder methods that have been mentioned above.<\/p>\n<!--v-->\n<div class=\"notices blue\">\n<p>You can get all the Pydon'ts as a <a href=\"\/books\/pydonts\">free ebook with over +400 pages and hundreds of tips<\/a>. <a href=\"\/books\/pydonts\">Download the ebook &ldquo;Pydon'ts &ndash; write elegant Python code&rdquo; here<\/a>.<\/p>\n<\/div>\n<!--^-->\n<h2 id=\"the-example-we-will-be-using\">The example we will be using<a href=\"#the-example-we-will-be-using\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The example we will be using throughout this article will be that of a <code>Vector<\/code>.\nA <code>Vector<\/code> will be a class for geometrical vectors, like vectors in 2D, or 3D, and it will provide operations to deal with vectors.\nFor example, by the end of this article, you will have an implementation of <code>Vector<\/code> that lets you do things like these:<\/p>\n<pre><code class=\"language-pycon\">&gt;&gt;&gt; from vector import Vector\n&gt;&gt;&gt; v = Vector(3, 2)\n&gt;&gt;&gt; v + Vector(4, 10)\nVector(7, 12)\n&gt;&gt;&gt; 3 * v\n(9, 6)\n&gt;&gt;&gt; -v\n(-3, -2)<\/code><\/pre>\n<p>Let us go ahead and start!<\/p>\n<p>This is the starting vector for our class <code>Vector<\/code>:<\/p>\n<pre><code class=\"language-py\"># vector.py\nclass Vector:\n    def __init__(self, *coordinates):\n        self.coordinates = coordinates\n\n    def __repr__(self):\n        return f\"Vector{self.coordinates}\"\n\nif __name__ == \"__main__\":\n    print(Vector(3, 2))<\/code><\/pre>\n<p>Running this code will show this output:<\/p>\n<pre><code>Vector(3, 2)<\/code><\/pre>\n<p>This starting vector also shows two dunder methods that we are using right off the bat:<\/p>\n<ol><li>we use <a href=\"\/blog\/object-initialisation-with-__init__\">the dunder method <code>__init__<\/code><\/a> to initialise our <code>Vector<\/code> instance; and<\/li>\n<li>we use <a href=\"\/blog\/pydonts\/str-and-repr\">the dunder method <code>__repr__<\/code><\/a> to provide a string representation of our <code>Vector<\/code> objects.<\/li>\n<\/ol><p>This shows that dunder methods are <strong>not<\/strong> magical.\nThey look funny because of the...<\/p>","summary":"This article shows you how to overload the arithmetic operators in Python with dunder methods.","date_modified":"2025-11-14T12:33:53+01:00","tags":["arithmetic","dunder methods","programming","python"],"image":"\/user\/pages\/02.blog\/02.pydonts\/overloading-arithmetic-operators-with-dunder-methods\/thumbnail.webp"},{"title":"TIL #007 \u2013 math.nextafter","date_published":"2021-09-29T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/til\/007","url":"https:\/\/mathspp.com\/blog\/til\/007","content_html":"<p>Today I learned about the <code>math.nextafter<\/code> method.<\/p>\n\n<script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><p><img alt=\"Code snippet showing the `nextafter` method.\" src=\"\/images\/5\/b\/f\/4\/4\/5bf441dc2debb882c1a98c9179986c961401ef7a-thumbnail.webp\"><\/p>\n<h2 id=\"math-nextafter\"><code>math.nextafter<\/code><a href=\"#math-nextafter\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>0 is a neat number, isn't it?<\/p>\n<p>Perhaps one of the greatest discoveries of mankind.<\/p>\n<p>But what's the number that comes after 0?\nThat would be the smallest number in the set <span class=\"mathjax mathjax--inline\">\\(]0, +\\inf[\\)<\/span>,\nif you're familiar with the mathematical notation for sets.<\/p>\n<p>In short, <span class=\"mathjax mathjax--inline\">\\([a, b]\\)<\/span> is the contiguous set of numbers <span class=\"mathjax mathjax--inline\">\\(x\\)<\/span> that satisfy\nthe restriction <span class=\"mathjax mathjax--inline\">\\(a \\leq x \\leq b\\)<\/span>.\nNotice how <span class=\"mathjax mathjax--inline\">\\(a\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(b\\)<\/span> are included inside <span class=\"mathjax mathjax--inline\">\\([a, b]\\)<\/span> because the brackets\nare closed.\nIf the brackets are open, then that number is not included.<\/p>\n<p>For the intervals below, <span class=\"mathjax mathjax--inline\">\\(x\\)<\/span> belongs to it if...<\/p>\n<ul><li><span class=\"mathjax mathjax--inline\">\\([a, b]\\)<\/span> &rarr; <span class=\"mathjax mathjax--inline\">\\(a \\leq x \\leq b\\)<\/span>;<\/li>\n<li><span class=\"mathjax mathjax--inline\">\\([a, b[\\)<\/span> &rarr; <span class=\"mathjax mathjax--inline\">\\(a \\leq x &lt; b\\)<\/span>;<\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(]a, b]\\)<\/span> &rarr; <span class=\"mathjax mathjax--inline\">\\(a &lt; x \\leq b\\)<\/span>; and<\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(]a, b[\\)<\/span> &rarr; <span class=\"mathjax mathjax--inline\">\\(a &lt; x &lt; b\\)<\/span>.<\/li>\n<\/ul><p>So, in <span class=\"mathjax mathjax--inline\">\\(]0, +\\infty[\\)<\/span>, nor 0, nor <span class=\"mathjax mathjax--inline\">\\(+\\infty\\)<\/span> are included.\nThus, what's the minimum element of this interval?\nWell, there isn't any!<\/p>\n<p>Mathematically speaking, there is no minimum in the interval <span class=\"mathjax mathjax--inline\">\\(]0, +\\infty[\\)<\/span>.\nWhy not?\nWhatever you pick as a potential minimum <span class=\"mathjax mathjax--inline\">\\(m\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(m\/2\\)<\/span> will be smaller than <span class=\"mathjax mathjax--inline\">\\(m\\)<\/span>\nand still be greater than <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span>, that is, <span class=\"mathjax mathjax--inline\">\\(0 &lt; m\/2 &lt; m &lt; +\\infty\\)<\/span>,\nand so <span class=\"mathjax mathjax--inline\">\\(m\/2\\)<\/span> is in <span class=\"mathjax mathjax--inline\">\\(]0, +\\infty[\\)<\/span>.<\/p>\n<p>That's interesting, right?<\/p>\n<p>But this is a whole other story if we go into the programming real!\nBecause of how floats are represented,\nPython <em>has<\/em> a number that comes immediately after <code>0<\/code>.\nSo, what is it?<\/p>\n<p>Here it is:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; import math\n&gt;&gt;&gt; math.nextafter(0, 1)\n5e-324<\/code><\/pre>\n<p>That's <span class=\"mathjax mathjax--inline\">\\(5 \\times 10^{-324}\\)<\/span>, it's freakishly small!<\/p>\n<p>(Your result may differ from mine, although I'm not sure if it will.\nLeave a comment below if it does!)<\/p>\n<p>So, what's the role of the <code>math.nextafter<\/code> method?<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; help(math.nextafter)\nHelp on built-in function nextafter in module math:\n\nnextafter(x, y, \/)\n    Return the next floating-point value after x towards y.<\/code><\/pre>\n<p>Hence, <code>nextafter<\/code> looks at <code>x<\/code> and then checks what's the float that's immediately next to <code>x<\/code>,\nif you walk in the direction of <code>y<\/code>.\nIf I set <code>x<\/code> to zero and <code>y<\/code> to one, I get the smallest float that Python can represent on my machine.<\/p>\n<p>So, what's the next float that Python can represent after <code>1<\/code>?<\/p>\n<p>Give it some thought.<\/p>\n<p>Here it is:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; math.nextafter(1, 999)\n1.0000000000000002<\/code><\/pre>\n<p>I'll be honest, for a second I thought it should've been <code>1 + 5e-324<\/code>,\nbut it makes sense it wasn't that.\nFloating point numbers have limited precision, right?\nAnd one thing that's limited is the size of the mantissa:\nthe stuff that comes after the decimal point.<\/p>\n<p>Above, we can see that the mantissa has 16 digits,\nand that's the size of the mantissa in Python.<\/p>\n<p>So, what's the next number after <code>10<\/code>?<\/p>\n<p>Give it some thought.<\/p>\n<p>Here it is:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; math.nextafter(10, 999)\n10.000000000000002<\/code><\/pre>\n<p>If you count, now there's only 15 digits to the right of the decimal point...\nBut...<\/p>","summary":"Today I learned about the `math.nextafter` method.","date_modified":"2025-07-23T16:49:02+02:00","tags":["arithmetic","mathematics","programming","python"],"image":"\/user\/pages\/02.blog\/04.til\/007.nextafter\/thumbnail.webp"},{"title":"TIL #001 \u2013 ceiling division in Python","date_published":"2021-09-15T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/til\/001","url":"https:\/\/mathspp.com\/blog\/til\/001","content_html":"<p>Today I learned how to do ceiling division in Python just with <code>\/\/<\/code>.<\/p>\n\n<script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<p><img alt=\"\" src=\"\/images\/3\/3\/4\/9\/e\/3349e66c1d2ea661669ba20ac090cf200d731b28-thumbnail.webp\"><\/p>\n<h2 id=\"floor-division\">Floor division <code>\/\/<\/code>\n<a href=\"#floor-division\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>I recently published a tweet telling people about the floor division operator in Python, <code>\/\/<\/code>:<\/p>\n<blockquote class=\"twitter-tweet\">\n<p lang=\"en\" dir=\"ltr\">Are you familiar with the `\/\/` operator in Python \ud83d\udc0d?<br><br>`\/\/` is the \u201cfloor division\u201d operation, which is equivalent to dividing and then rounding down.<br><br>`q = n \/\/ m` is always an integer, and the value of `q` is equivalent to `q = floor(n \/ m)`.<br><br>How many years fit in 10_000 days? <a href=\"https:\/\/t.co\/ecRQoz3qkM\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">pic.twitter.com\/ecRQoz3qkM<\/a><\/p>\u2014 Rodrigo \ud83d\udc0d\ud83d\udcdd (@mathsppblog) <a href=\"https:\/\/twitter.com\/mathsppblog\/status\/1437890406021206028?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">September 14, 2021<\/a>\n<\/blockquote>\n<p>This operator is equivalent to doing regular division and then flooring down:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; # How many years in 10_000 days?\n&gt;&gt;&gt; from math import floor; floor(10_000 \/ 365)\n27\n&gt;&gt;&gt; 10_000 \/\/ 365\n27<\/code><\/pre>\n<p>Then, someone asked if Python also had a built-in for ceiling division,\nthat is, an operator that divided the operands and then rounded up.<\/p>\n<p>While there is no direct built-in operator for that,\nsomeone replied saying that we can use a couple of minus signs and floor division to do that.<\/p>\n<p>Ceiling division with <code>a<\/code> and <code>b<\/code> would be equivalent to <code>ceil(a \/ b)<\/code>.\nAnd they showed that we can do it with <code>-(-a \/\/ b)<\/code>:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; from math import ceil\n&gt;&gt;&gt; a, b = 10, 3\n&gt;&gt;&gt; ceil(a \/ b)\n4\n&gt;&gt;&gt; -(-a \/\/ b)\n4<\/code><\/pre>\n<p>Why does this work?<\/p>\n<p><code>floor<\/code> rounds down and <code>ceil<\/code> rounds up.\nBy using <code>-a<\/code> in the division, it's as if you flip <code>a<\/code> upside down,\nso \u201cits ceiling is now on the floor\u201d, so you can use <code>-a \/\/ b<\/code>.\nThen, you just need to put everything back in place,\nusing a final negation: <code>-(-a \/\/ b)<\/code>.<\/p>\n<p>At first, I thought this would fail for some combination of positive\/negative values for <code>a<\/code> and <code>b<\/code>,\nbut it most certainly doesn't.<\/p>\n<p>For one, the explanation works regardless of the sign of <code>a<\/code> and\/or <code>b<\/code>.\nSecondly, one can always test it:<\/p>\n<pre><code class=\"language-py\">&gt;&gt;&gt; for a, b in [(10, 3), (10, -3), (-10, 3), (-10, -3)]:\n...     assert ceil(a \/ b) == -(-a \/\/ b)\n...\n&gt;&gt;&gt;<\/code><\/pre>\n<p>Here's the original tweet that taught me this:<\/p>\n<blockquote class=\"twitter-tweet\">\n<p lang=\"en\" dir=\"ltr\">You can do ceiling division with '\/\/' and some unary '-' signs, since '\/\/' truncates to the next *lowest* number. If the divisor is negative, that means it goes to the next \"most-negative\" number, which, when negated, is actually \"truncating up\". <a href=\"https:\/\/t.co\/uAQmJJbYhw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">pic.twitter.com\/uAQmJJbYhw<\/a><\/p>\u2014 Paul McGuire - pyparsing guy (@ptmcguire) <a href=\"https:\/\/twitter.com\/ptmcguire\/status\/1438128407791996934?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">September 15, 2021<\/a>\n<\/blockquote>","summary":"Today I learned how to do ceiling division in Python just with `\/\/`.","date_modified":"2025-07-23T16:49:02+02:00","tags":["arithmetic","mathematics","programming","python"],"image":"\/user\/pages\/02.blog\/04.til\/001.ceiling-division-in-python\/thumbnail.webp"},{"title":"Problem #044 \u2013 send more money","date_published":"2021-09-06T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/problems\/send-more-money","url":"https:\/\/mathspp.com\/blog\/problems\/send-more-money","content_html":"<p>Can you solve this simple-looking arithmetic challenge?<\/p>\n\n<p><img alt='A piece of paper where one can read \"SEND + MORE = MONEY\".' src=\"\/images\/9\/5\/7\/8\/0\/95780f0e091d16d4dbc2866926a50488b266002e-thumbnail.png\"><\/p>\n<h2 id=\"problem-statement\">Problem statement<a href=\"#problem-statement\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The image above represents an arithmetic calculation:<\/p>\n<p>SEND + MORE = MONEY<\/p>\n<p>Each letter represents a unique digit,\nand each word represents a number with as many digits as letters\n(there are no leading 0s).<\/p>\n<p>Can you figure out the numeric value of each word?<\/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\/qJakc0\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Giorgio N.<\/a>, Italy;<\/li>\n<li>\n<a href=\"https:\/\/twitter.com\/BPrvn_Rj\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">B. Praveen R.<\/a>, India;<\/li>\n<li>\n<a href=\"https:\/\/twitter.com\/sci_c0\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Nishant M.<\/a>, India;<\/li>\n<li>David H., Taiwan;<\/li>\n<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>Ruperto Arrieta Jr, Philippines;<\/li>\n<\/ul><p>Join the list of solvers by <a href=\"mailto:rodrigo@mathspp.com?subject=Solution%20to%20Problem%20#044%20%E2%80%93%20send%20more%20money\" 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>SEND and MORE are two 4-digit numbers that are both 9999 or less.\nThat means that their sum is 19998 or less.\nBecause the result is MONEY, with 5 digits, we immediately conclude that the leading M in MONEY is 1, from which we get this:<\/p>\n<pre><code>  S E N D\n+ 1 O R E\n---------\n1 O N E Y<\/code><\/pre>\n<p>Now, we can look at the S + 1 on the left.\nThe result of S + 1 must be 10 or more (because there must be a 1 to carry to the left) and there may be a carry coming from the hundreds.\nLet us consider what happens if we assume there (or there isn't) carry.<\/p>\n<p>If there is carry, then O = S + 1 + 1 = S + 2.\nO can't be 1 (because 1 is already taken) so O must 0, because for O to be 2 or greater, S would have to be greater than or equal to 10.\nSo, if there is carry, O = 0 and S = 8.<\/p>\n<p>If there is no carry, then O = S + 1.\nO can't be 1 (because 1 is already taken) so O must be 0, because for O to be 2 or greater, S would have to be greater than or equal to 11.\nSo, if there is no carry, O = 0 and S = 9.<\/p>\n<p>Regardless of whether there is carry or not, we concluded that O = 0, so we're at this point:<\/p>\n<pre><code>  S E N D\n+ 1 0 R E\n---------\n1 0 N E Y<\/code><\/pre>\n<p>If we look at the hundreds place, we see E + 0 = N.\nThis is only possible if there is carry coming from the tens place, which means that N + R must be greater than 10.\nBut, if that is the case, then E is, at most, an 8 (which would happen if N and R were 9 and 8, and there was carry coming from the units place).\nIf E is at most an 8, then in the hundreds place the calculation E + 0 + 1 (the carry) is less than 10, which means that there is...<\/p>","summary":"Can you solve this simple-looking arithmetic challenge?","date_modified":"2025-07-23T16:49:02+02:00","tags":["arithmetic","logic","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p044-send-more-money\/thumbnail.png"},{"title":"Studying the &quot;24 Game&quot;","date_published":"2020-07-12T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/24-game","url":"https:\/\/mathspp.com\/blog\/24-game","content_html":"<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/24_Game\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">24 Game<\/a> is a well-known maths game that is played with kids in school to help them master the four basic arithmetic operations. In this blog post we will study the game in depth.<\/p>\n\n<h3 id=\"the-game\">The game<a href=\"#the-game\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>The \"24 Game\" is a simple game. You are given four numbers between <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(9\\)<\/span> (for example <span class=\"mathjax mathjax--inline\">\\(\\{1, 2, 3, 4\\}\\)<\/span>) and your objective is to find an expression that evaluates to <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span>. The rules are fairly simple:<\/p>\n<ul><li>each given number must be used exactly once;<\/li>\n<li>the only operations available are addition, subtraction, multiplication and division;<\/li>\n<li>operations may be used repeatedly or not at all;<\/li>\n<li>operation precedence can be manipulated by the use of parentheses;<\/li>\n<li>no \"clever tricks\" should be used, only simple arithmetic.<\/li>\n<\/ul><h4 id=\"examples\">Examples<a href=\"#examples\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h4>\n<p>If the given numbers are <span class=\"mathjax mathjax--inline\">\\(\\{1, 2, 3, 4\\}\\)<\/span>, an answer could be<\/p>\n<ul><li><span class=\"mathjax mathjax--inline\">\\(1 \\times 2 \\times 3 \\times 4\\)<\/span>.<\/li>\n<\/ul><p>If the given numbers are <span class=\"mathjax mathjax--inline\">\\(\\{2, 5, 7, 8\\}\\)<\/span>, an answer could be<\/p>\n<ul><li><span class=\"mathjax mathjax--inline\">\\((2\\times 5 - 7)\\times 8\\)<\/span>.<\/li>\n<\/ul><h3 id=\"the-motivation\">The motivation<a href=\"#the-motivation\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>I was talking to a friend who had just challenged me to make <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span> out of <span class=\"mathjax mathjax--inline\">\\(\\{3, 3, 8, 8\\}\\)<\/span> (<a href=\"\/blog\/problems\/make-24-with-3-3-8-8\">give it a try yourself<\/a>) and as we talked about the game, we asked each other \"Why is <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span> the target value? Is <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span> special in any way?\".<\/p>\n<p>I had already written a computer program that solves instances of the game so we decided we could use said program to find out neat things about the game itself.<\/p>\n<div class=\"notices blue\">\n<p>To make my life easier writing this blog post, let's agree that when I talk about <em>input<\/em> I mean the numbers you have to use to make <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span> and when I talk about the <em>target<\/em> I mean the number you are trying to make, which is <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span> in the standard game.<\/p>\n<\/div>\n<h3 id=\"the-code\">The code<a href=\"#the-code\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>The questions I will be asking in this blog post were answered with the help of some programming I did in <a href=\"\/blog\/lsbasi-apl-part1\">APL<\/a>. I defined a couple of useful functions that you can find at the end of this post; throughout the blog post I will be using those functions to answer the questions I will present.<\/p>\n<p>The first question me and my friend asked was<\/p>\n<h4 id=\"does-24-work-for-any-input\">Does <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span> work for any input?<a href=\"#does-24-work-for-any-input\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h4>\n<p>As far as I know, the game is usually played by giving four distinct numbers as input, so <span class=\"mathjax mathjax--inline\">\\(\\{2, 3, 4, 5\\}\\)<\/span> is a valid input but <span class=\"mathjax mathjax--inline\">\\(\\{3, 3, 4, 5\\}\\)<\/span> is not.<\/p>\n<p>Defining these types of inputs in APL is rather easy (note that I have <code>&#9109;IO &larr; 0<\/code>) and counting them is even easier:<\/p>\n<pre><code class=\"language-apl\">      uinps &larr; all\/&#9064; {&and;\/2&lt;\/&#9077;}&uml; all &larr; 1+,&#9075;4&#9076;9  &#9053; inputs with unique digits\n      &#9109;&larr; &#8802;uinps \n126\n      nuinps &larr; all\/&#9064; {&and;\/2&le;\/&#9077;}&uml; all  &#9053; digits may repeat\n      &#9109;&larr; &#8802;nuinps\n495<\/code><\/pre>\n<p>Turns out that <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span> does <em>not<\/em> work for any input. In fact, out of the <span class=\"mathjax mathjax--inline\">\\(126\\)<\/span> valid inputs, <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span> only works for <span class=\"mathjax mathjax--inline\">\\(124\\)<\/span> of them, failing for<\/p>\n<ul><li><span class=\"mathjax mathjax--inline\">\\(\\{1, 6, 7, 8\\}\\)<\/span><\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(\\{3, 4, 6, 7\\}\\)<\/span><\/li>\n<\/ul><p>You can check this fact by trying...<\/p>","summary":"The &quot;24 game&quot; is a well-known maths game for kids and in this post we study it in depth.","date_modified":"2024-08-14T18:52:06+02:00","tags":["mathematics","programming","apl","visualisation","arithmetic"],"image":"\/user\/pages\/02.blog\/24-game\/non_unique_100.webp"},{"title":"Problem #020 - make 24 with 3 3 8 8","date_published":"2020-07-09T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/problems\/make-24-with-3-3-8-8","url":"https:\/\/mathspp.com\/blog\/problems\/make-24-with-3-3-8-8","content_html":"<p>Today we are visiting a specific instance of a well-known <em>basic<\/em> mathematics game, the <a href=\"https:\/\/en.wikipedia.org\/wiki\/24_Game\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">24 Game<\/a>. The \"24 Game\" is usually played with younger students because it helps them develop skills related to the basic arithmetic operations.<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Kadir Celep on Unsplash\" alt=\"A picture of a wall with a 24 in it\" src=\"\/images\/2\/3\/7\/9\/6\/23796625afc73d0abd8edcf2d4aa5f88cdc4a7fd-24.jpg\"><figcaption class=\"\">Photo by Kadir Celep 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 \"24 Game\" is usually played with four different numbers from <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span> to <span class=\"mathjax mathjax--inline\">\\(9\\)<\/span>, for example drawn randomly out of a small deck with those numbers. For this blog post, you can forget about all that, I picked very specific numbers for you.<\/p>\n<p>Using the four numbers <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(8\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(8\\)<\/span> and the four arithmetic operations addition, subtraction, multiplication and division, you have to make an expression that evaluates to <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span>. The rules are simple:<\/p>\n<ul>\n<li>each number must be used exactly once;<\/li>\n<li>operations can be used any number of times (even zero times);<\/li>\n<li>the precedence of operations can be changed by the use of <span class=\"mathjax mathjax--inline\">\\(()\\)<\/span>;<\/li>\n<li>numbers cannot be used by positioning them in a special way, e.g. you can't make <span class=\"mathjax mathjax--inline\">\\(33\\)<\/span> by joining the two <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span>s and you can't make <span class=\"mathjax mathjax--inline\">\\(27\\)<\/span> by writing <span class=\"mathjax mathjax--inline\">\\(3^3\\)<\/span>.<\/li>\n<\/ul>\n<p>I am not giving you a puzzle with a cheap trick involved. This is pure arithmetic.<\/p>\n<p>An example valid expression would be <span class=\"mathjax mathjax--inline\">\\((3+3)\\times (8+8)\\)<\/span> except this is not the solution because it gives <span class=\"mathjax mathjax--inline\">\\(96\\)<\/span> instead of <span class=\"mathjax mathjax--inline\">\\(24\\)<\/span>.<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought, maybe take out a piece of paper and a pencil.<\/p>\n<\/div>\n<p>I heard of this from the same friend who told me about the <a href=\"\/blog\/problems\/fold-the-alphabet\">\"Fold the alphabet\"<\/a> problem.<\/p>\n<h3 id=\"hint\">Hint<a href=\"#hint\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>Do <em>not<\/em> read the hint if you haven't spent some time thinking about the problem yet!<\/p>\n<div class=\"notices green\">\n<p>The intermediate steps do <em>not<\/em> have to evaluate to integer numbers.<\/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>There really is no point in sugar coating this for you, the solution is the expression <span class=\"mathjax mathjax--inline\">\\(8 \\div (3 - 8\\div 3)\\)<\/span>. Quite ingenious, isn't it?<\/p>\n<p>[Don't forget to subscribe to the newsletter]<iframe src=\"https:\/\/embeds.beehiiv.com\/a1f06944-033c-42f6-b836-db0a2a1b49c9?slim=true\" data-test-id=\"beehiiv-embed\" width=\"50%\" height=\"52\" frameborder=\"0\" scrolling=\"no\" style=\"margin: 0; border-radius: 0px !important; background-color: transparent;\"><\/iframe> to get bi-weekly\nproblems sent straight to your inbox and to add your reaction below.<\/p>","summary":"How can you use 3, 3, 8, 8 and the arithmetic operations to make 24?","date_modified":"2025-07-23T16:49:02+02:00","tags":["arithmetic","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p020-make-24-with-3-3-8-8\/24.jpg"},{"title":"Twitter proof: irrational rationality","date_published":"2017-11-16T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/irrational-rationality","url":"https:\/\/mathspp.com\/blog\/irrational-rationality","content_html":"<p>Let's prove that there are two irrational numbers, call them <span class=\"mathjax mathjax--inline\">\\(a\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(b\\)<\/span>, such that <span class=\"mathjax mathjax--inline\">\\(a^b\\)<\/span> is a rational number! And let's do it in a tweet.<\/p>\n\n<script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<p><img alt=\"the variable a raised to the power of b\" src=\"\/images\/c\/0\/1\/8\/9\/c01897bba8131bf7b98121f7fb8f67c3b759995a-atotheb.webp\"><\/p>\n<blockquote class=\"twitter-tweet\">\n<p lang=\"en\" dir=\"ltr\">Twitter proof:<br>Let a=b=\u221a2. If a^b is rational then there is nothing to be done. Assume a^b is irrational. Redefine a=\u221a2^\u221a2. Notice how a^b = (\u221a2^\u221a2)^\u221a2 = (\u221a2)^2 = 2 which is clearly rational. QED.<a href=\"https:\/\/t.co\/3sFinyzJwu\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">https:\/\/t.co\/3sFinyzJwu<\/a><\/p>\u2014 Mathspp (@mathsppblog) <a href=\"https:\/\/twitter.com\/mathsppblog\/status\/1286089355732365313?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">July 23, 2020<\/a>\n<\/blockquote>\n<p>Do you have an idea for a twitter proof? Let me know in the comments below!<\/p>","summary":"A really short proof that there are two irrational numbers whose power gives a rational number.","date_modified":"2025-07-23T16:49:02+02:00","tags":["arithmetic","rationals","mathematics"],"image":"\/user\/pages\/02.blog\/irrational-rationality\/atotheb.webp"}]}
