<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The ryg blog]]></provider_name><provider_url><![CDATA[https://fgiesen.wordpress.com]]></provider_url><author_name><![CDATA[fgiesen]]></author_name><author_url><![CDATA[https://fgiesen.wordpress.com/author/fgiesen/]]></author_url><title><![CDATA[Intervals in modular&nbsp;arithmetic]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>I wrote about regular interval overlap checking <a href="https://fgiesen.wordpress.com/2011/10/16/checking-for-interval-overlap/">before</a>. Let&#8217;s consider a somewhat trickier case, where our intervals are not defined over the real numbers or regular integers, but are instead subsets of the integers modulo N (for some fixed N). In this post, I&#8217;ll just consider the math; but this is something I&#8217;ve used in several places, so I expect I&#8217;ll be writing about some concrete uses eventually. Throughout this document, I&#8217;ll be writing <img src="https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D_N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D_N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D_N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mathbb{Z}_N" class="latex" /> for the set <img src="https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D+%2F+N%5Cmathbb%7BZ%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D+%2F+N%5Cmathbb%7BZ%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D+%2F+N%5Cmathbb%7BZ%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mathbb{Z} / N&#92;mathbb{Z}" class="latex" /> of integers mod N (feel free to ignore the notation if you&#8217;re not familiar with it).</p>
<h3>Intervals mod N, first attempt</h3>
<p>The first thing we need to do is agree on what we mean by an interval mod N. Suppose that N=16. In that setting, the meaning of the interval [5,7] is pretty clear: the set {5, 6, 7}. And we can also take something like the integer interval [14,17] representing the set {14, 15, 16, 17} and reduce it mod N to get {14, 15, 0, 1}. But doing this required us to use values outside of {0, &hellip;, N-1} to specify the end points. This is often a useful model &#8211; I talk about some advantages when doing this for ring buffers in <a href="https://fgiesen.wordpress.com/2010/12/14/ring-buffers-and-queues/">this old post</a> &#8211; but it&#8217;s a bit of a cheat; we&#8217;d like a way to specify intervals mod N that don&#8217;t require us to leave the set of integers mod N to handle the &#8220;wraparound&#8221; case.</p>
<p>This means that we want to define something like the interval [14,1]. For the regular integers, this would be the empty set; but mod N, it makes more sense to properly wrap around (for example, when you say &#8220;the trains don&#8217;t run from 11:30pm to 5:30am&#8221;, it&#8217;s understood that you mean 5:30am the next day, not an empty interval). One way to do this is by handling the wrap-around case separately. <b>Assuming a, b are reduced mod N</b>, we could for example define:</p>
<p><img src="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%5Cmod+N+%3A%3D+%5Cbegin%7Bcases%7D++%5C%7B+a%2C+a+%2B+1%2C+%5Chdots%2C+b+%5C%7D+%26+%5Cmbox%7Bif+%7D+a+%5Cle+b+%5C%5C++%5C%7B+a%2C+a+%2B+1%2C+%5Chdots%2C+N+-+1+%5C%7D+%5Ccup+%5C%7B+0%2C+1%2C+%5Chdots%2C+b+%5C%7D+%26+%5Cmbox%7Bif+%7D+a+%3E+b++%5Cend%7Bcases%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%5Cmod+N+%3A%3D+%5Cbegin%7Bcases%7D++%5C%7B+a%2C+a+%2B+1%2C+%5Chdots%2C+b+%5C%7D+%26+%5Cmbox%7Bif+%7D+a+%5Cle+b+%5C%5C++%5C%7B+a%2C+a+%2B+1%2C+%5Chdots%2C+N+-+1+%5C%7D+%5Ccup+%5C%7B+0%2C+1%2C+%5Chdots%2C+b+%5C%7D+%26+%5Cmbox%7Bif+%7D+a+%3E+b++%5Cend%7Bcases%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%5Cmod+N+%3A%3D+%5Cbegin%7Bcases%7D++%5C%7B+a%2C+a+%2B+1%2C+%5Chdots%2C+b+%5C%7D+%26+%5Cmbox%7Bif+%7D+a+%5Cle+b+%5C%5C++%5C%7B+a%2C+a+%2B+1%2C+%5Chdots%2C+N+-+1+%5C%7D+%5Ccup+%5C%7B+0%2C+1%2C+%5Chdots%2C+b+%5C%7D+%26+%5Cmbox%7Bif+%7D+a+%3E+b++%5Cend%7Bcases%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="[a,b] &#92;mod N := &#92;begin{cases}  &#92;{ a, a + 1, &#92;hdots, b &#92;} &amp; &#92;mbox{if } a &#92;le b &#92;&#92;  &#92;{ a, a + 1, &#92;hdots, N - 1 &#92;} &#92;cup &#92;{ 0, 1, &#92;hdots, b &#92;} &amp; &#92;mbox{if } a &gt; b  &#92;end{cases}" class="latex" /></p>
<p>This works, but it&#8217;s messy, and chopping up our single connected interval mod N into two pieces for the wrap-around case feels like it&#8217;s missing something fundamental about arithmetic mod N. We&#8217;re thinking in terms of a number line:</p>
<p><a href="https://fgiesen.files.wordpress.com/2015/09/line1.png"><img loading="lazy" data-attachment-id="2695" data-permalink="https://fgiesen.wordpress.com/2015/09/24/intervals-in-modular-arithmetic/line/" data-orig-file="https://fgiesen.files.wordpress.com/2015/09/line1.png" data-orig-size="489,23" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Interval [14,1] on the number line" data-image-description="" data-image-caption="" data-medium-file="https://fgiesen.files.wordpress.com/2015/09/line1.png?w=300" data-large-file="https://fgiesen.files.wordpress.com/2015/09/line1.png?w=489" src="https://fgiesen.files.wordpress.com/2015/09/line1.png?w=489&#038;h=23" alt="Interval [14,1] on the number line" width="489" height="23" class="aligncenter size-full wp-image-2695" srcset="https://fgiesen.files.wordpress.com/2015/09/line1.png 489w, https://fgiesen.files.wordpress.com/2015/09/line1.png?w=150&amp;h=7 150w, https://fgiesen.files.wordpress.com/2015/09/line1.png?w=300&amp;h=14 300w" sizes="(max-width: 489px) 100vw, 489px" /></a></p>
<p>But the integers mod N aren&#8217;t very &#8220;line-like&#8221; at all, and they&#8217;re commonly (and more appropriately) drawn as a circle (like a clock, with 0 at the top and numbers increasing clockwise, to maximize familiarity&mdash;apologies to all mathematicians who expect positive angles to move counter-clockwise).</p>
<p><a href="https://fgiesen.files.wordpress.com/2015/09/circle.png"><img loading="lazy" data-attachment-id="2697" data-permalink="https://fgiesen.wordpress.com/2015/09/24/intervals-in-modular-arithmetic/circle/" data-orig-file="https://fgiesen.files.wordpress.com/2015/09/circle.png" data-orig-size="213,209" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Interval [14,1] on a circle" data-image-description="" data-image-caption="" data-medium-file="https://fgiesen.files.wordpress.com/2015/09/circle.png?w=213" data-large-file="https://fgiesen.files.wordpress.com/2015/09/circle.png?w=213" src="https://fgiesen.files.wordpress.com/2015/09/circle.png?w=213&#038;h=209" alt="Interval [14,1] on a circle" width="213" height="209" class="aligncenter size-full wp-image-2697" srcset="https://fgiesen.files.wordpress.com/2015/09/circle.png 213w, https://fgiesen.files.wordpress.com/2015/09/circle.png?w=150&amp;h=147 150w" sizes="(max-width: 213px) 100vw, 213px" /></a></p>
<p>And in this visualization, there is absolutely nothing special about our interval [14,1]. We happen to pass through 0, but 0 isn&#8217;t actually special. It&#8217;s just another point on the circle. The reason 0 <em>becomes</em> special when we think in terms of regular integers (and hence a number line) is that it&#8217;s usually the place where we decide to cut open the circle so we can flatten it into a line. But that has nothing to do with the circle (or the integers mod N); it&#8217;s an incidental artifact of the representation we&#8217;re picking.</p>
<h3>A different approach</h3>
<p>The key problem here is that intervals are normally defined in terms of ordering relationships. For example, real-number intervals are usually defined as sets</p>
<p><img src="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BR%7D+%7C+a+%5Cle+x+%5Cle+b+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BR%7D+%7C+a+%5Cle+x+%5Cle+b+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BR%7D+%7C+a+%5Cle+x+%5Cle+b+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="[a,b] = &#92;{ x &#92;in &#92;mathbb{R} | a &#92;le x &#92;le b &#92;}" class="latex" />.</p>
<p>based on a total order &#8220;&le;&#8221;. But in our circle, we don&#8217;t have a useful ordering relation. When you write &#8220;4 &lt; 7&#8221;, this means that 4 is to the left of 7 on the real number line. If you start at 4 and keep walking right, you&#8217;re gonna come by 7 eventually. If you instead were to walk left, the numbers would just keep getting smaller indefinitely.</p>
<p>On our circle mod N, this is not true. If you start at 4 and keep walking in the positive direction (clockwise in our case), you&#8217;ll reach 7 after three steps. If you instead walk counterclockwise from 4, you still reach 7 &#8211; this time after taking the long way round, in thirteen steps. This is true for any pair of numbers on the circle, which after all represent congruence classes of integers mod N (if you don&#8217;t know the terminology, just ignore the rest of this paragraph). It makes sense to say that the integer 4 is less than the integer 7, but the set <img src="https://s0.wp.com/latex.php?latex=%5Cbar%7B4%7D+%3D+4+%2B+N%5Cmathbb%7BZ%7D+%3D+%5C%7B+4+%2B+kN+%7C+k+%5Cin+%5Cmathbb%7BZ%7D+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cbar%7B4%7D+%3D+4+%2B+N%5Cmathbb%7BZ%7D+%3D+%5C%7B+4+%2B+kN+%7C+k+%5Cin+%5Cmathbb%7BZ%7D+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cbar%7B4%7D+%3D+4+%2B+N%5Cmathbb%7BZ%7D+%3D+%5C%7B+4+%2B+kN+%7C+k+%5Cin+%5Cmathbb%7BZ%7D+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;bar{4} = 4 + N&#92;mathbb{Z} = &#92;{ 4 + kN | k &#92;in &#92;mathbb{Z} &#92;}" class="latex" /> is not in any useful sense &#8220;less&#8221; or &#8220;greater&#8221; than the set <img src="https://s0.wp.com/latex.php?latex=%5Cbar%7B7%7D+%3D+7+%2B+N%5Cmathbb%7BZ%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cbar%7B7%7D+%3D+7+%2B+N%5Cmathbb%7BZ%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cbar%7B7%7D+%3D+7+%2B+N%5Cmathbb%7BZ%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;bar{7} = 7 + N&#92;mathbb{Z}" class="latex" />. Viewed in that light, the definition for [a,b] mod N given above is outright weird; the case distinction between &#8220;a &le; b&#8221; and &#8220;a &gt; b&#8221; is testing a condition that doesn&#8217;t really make sense in terms of the concepts being expressed.</p>
<p>So ordering relationships are on shaky footing. We can, however, usefully talk about <em>distances</em>. For example, &#8220;4&#8221; and &#8220;7&#8221; are definitely three steps apart when we take the shortest path, also three steps apart when we&#8217;re only allowed to move clockwise, and thirteen steps apart when we&#8217;re only allowed to move counterclockwise. In general, we can define distance functions for the &#8220;increasing&#8221; (clockwise), &#8220;decreasing&#8221; (counter-clockwise) and shortest-path distances between two points (we won&#8217;t actually be using that latter one, I just mention it for completeness):</p>
<p><img src="https://s0.wp.com/latex.php?latex=d%5E%7B%5C%2C%2B%7D%28a%2Cb%29+%3A%3D+%28b-a%29+%5Cbmod+N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=d%5E%7B%5C%2C%2B%7D%28a%2Cb%29+%3A%3D+%28b-a%29+%5Cbmod+N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=d%5E%7B%5C%2C%2B%7D%28a%2Cb%29+%3A%3D+%28b-a%29+%5Cbmod+N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="d^{&#92;,+}(a,b) := (b-a) &#92;bmod N" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=d%5E%7B%5C%2C-%7D%28a%2Cb%29+%3A%3D+%28a-b%29+%5Cbmod+N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=d%5E%7B%5C%2C-%7D%28a%2Cb%29+%3A%3D+%28a-b%29+%5Cbmod+N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=d%5E%7B%5C%2C-%7D%28a%2Cb%29+%3A%3D+%28a-b%29+%5Cbmod+N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="d^{&#92;,-}(a,b) := (a-b) &#92;bmod N" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=d%28a%2C+b%29+%3A%3D+%5Cmin%28d%5E%7B%5C%2C%2B%7D%28a%2Cb%29%2C+d%5E%7B%5C%2C-%7D%28a%2Cb%29%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=d%28a%2C+b%29+%3A%3D+%5Cmin%28d%5E%7B%5C%2C%2B%7D%28a%2Cb%29%2C+d%5E%7B%5C%2C-%7D%28a%2Cb%29%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=d%28a%2C+b%29+%3A%3D+%5Cmin%28d%5E%7B%5C%2C%2B%7D%28a%2Cb%29%2C+d%5E%7B%5C%2C-%7D%28a%2Cb%29%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="d(a, b) := &#92;min(d^{&#92;,+}(a,b), d^{&#92;,-}(a,b))" class="latex" /></p>
<p>These distance functions are, technically speaking, mappings <img src="https://s0.wp.com/latex.php?latex=d+%3A+%5Cmathbb%7BZ%7D_N+%5Ctimes+%5Cmathbb%7BZ%7D_N+%5Crightarrow+%5Cmathbb%7BN%7D_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=d+%3A+%5Cmathbb%7BZ%7D_N+%5Ctimes+%5Cmathbb%7BZ%7D_N+%5Crightarrow+%5Cmathbb%7BN%7D_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=d+%3A+%5Cmathbb%7BZ%7D_N+%5Ctimes+%5Cmathbb%7BZ%7D_N+%5Crightarrow+%5Cmathbb%7BN%7D_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="d : &#92;mathbb{Z}_N &#92;times &#92;mathbb{Z}_N &#92;rightarrow &#92;mathbb{N}_0" class="latex" /> from the integers mod N to the natural numbers (hence the explicit use of mod as a binary operation). The distances are regular non-negative integers; while points in the integers mod N can&#8217;t be meaningfully compared, distances can. That&#8217;s why they&#8217;re useful to us. Also note that the &#8220;mod N&#8221; here is the modulus under Euclidean/floor division &#8211; that is, a non-negative value smaller than N.</p>
<p>Most programming languages use truncating division instead, which means the modulus has absolute value less than N but might be negative; you need to consider this when turning any of the equations in here into code!</p>
<p>Given all this, let&#8217;s go back to our problem of defining intervals nicely. Well, how do we draw something like the interval [14,1] on our circle? We just move the pen to &#8220;14&#8221; and start drawing a clockwise arc until we hit the &#8220;1&#8221;. We can use that exact idea to define intervals: namely, a point x is inside the interval [a,b] if, starting from a and walking in increasing order (clockwise), we hit x before we leave the interval. That leads to our improved definition of a closed interval mod N:</p>
<p><img src="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C%2B%7D%28a%2Cx%29+%5Cle+d%5E%7B%5C%2C%2B%7D%28a%2Cb%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C%2B%7D%28a%2Cx%29+%5Cle+d%5E%7B%5C%2C%2B%7D%28a%2Cb%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C%2B%7D%28a%2Cx%29+%5Cle+d%5E%7B%5C%2C%2B%7D%28a%2Cb%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="[a,b] &#92;pmod N := &#92;{ x &#92;in &#92;mathbb{Z}_N | &#92;;d^{&#92;,+}(a,x) &#92;le d^{&#92;,+}(a,b) &#92;}" class="latex" /><br />
or the equivalent<br />
<img src="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C-%7D%28b%2Cx%29+%5Cle+d%5E%7B%5C%2C-%7D%28b%2Ca%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C-%7D%28b%2Cx%29+%5Cle+d%5E%7B%5C%2C-%7D%28b%2Ca%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C-%7D%28b%2Cx%29+%5Cle+d%5E%7B%5C%2C-%7D%28b%2Ca%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="[a,b] &#92;pmod N := &#92;{ x &#92;in &#92;mathbb{Z}_N | &#92;;d^{&#92;,-}(b,x) &#92;le d^{&#92;,-}(b,a) &#92;}" class="latex" /></p>
<p>and the generalizations to half-open intervals are straightforward:</p>
<p><img src="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%29+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C%2B%7D%28a%2Cx%29+%3C+d%5E%7B%5C%2C%2B%7D%28a%2Cb%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%29+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C%2B%7D%28a%2Cx%29+%3C+d%5E%7B%5C%2C%2B%7D%28a%2Cb%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ba%2Cb%29+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C%2B%7D%28a%2Cx%29+%3C+d%5E%7B%5C%2C%2B%7D%28a%2Cb%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="[a,b) &#92;pmod N := &#92;{ x &#92;in &#92;mathbb{Z}_N | &#92;;d^{&#92;,+}(a,x) &lt; d^{&#92;,+}(a,b) &#92;}" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=%28a%2Cb%5D+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C-%7D%28b%2Cx%29+%3C+d%5E%7B%5C%2C-%7D%28b%2Ca%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%28a%2Cb%5D+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C-%7D%28b%2Cx%29+%3C+d%5E%7B%5C%2C-%7D%28b%2Ca%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%28a%2Cb%5D+%5Cpmod+N+%3A%3D+%5C%7B+x+%5Cin+%5Cmathbb%7BZ%7D_N+%7C+%5C%3Bd%5E%7B%5C%2C-%7D%28b%2Cx%29+%3C+d%5E%7B%5C%2C-%7D%28b%2Ca%29+%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="(a,b] &#92;pmod N := &#92;{ x &#92;in &#92;mathbb{Z}_N | &#92;;d^{&#92;,-}(b,x) &lt; d^{&#92;,-}(b,a) &#92;}" class="latex" /></p>
<p>Things are simpler if we always start measuring from the closed (inclusive) end, so that&#8217;s what we do. I&#8217;ll drop the (mod N) for the rest of the article; we know that&#8217;s our setting.</p>
<h3>Point-in-interval tests and symmetry</h3>
<p>This definition can be turned into code immediately and leads to fairly elegant point-in-interval tests that don&#8217;t break down into multiple cases:</p>
<pre>
// modN(x) is assumed to calculate Euclidean (=non-negative) x % N.

// x in [a,b] (mod N)?
static bool point_in_closed_interval(int x, int a, int b)
{
    return modN(x - a) &lt;= modN(b - a);
}

// x in [a,b) (mod N)?
static bool point_in_half_open_interval(int x, int a, int b)
{
    return modN(x - a) &lt; modN(b - a);
}
</pre>
<p>At this point, you should also be noticing something funny about that code (it&#8217;s a bit harder to see in the definitions above since the detour through the distance functions obscures the actual computation going on); namely, the fact that we&#8217;re subtracting a and then reducing mod N on both sides.</p>
<p>It&#8217;s confession time. This whole notion with measuring distances along the circle is not how I derived this; it&#8217;s the best way I know to <em>think</em> about this problem, but that knowledge came in retrospect. I got here the long way round, with several false starts. The key idea turned out to be thinking about symmetries, which is always worth doing if the problem you&#8217;re working on has any; see for example <a href="https://fgiesen.wordpress.com/2009/12/15/dxt5-alpha-block-index-determination/">this</a> post I wrote 6 years ago!</p>
<p>In this case, the integers mod N are a cyclic group&mdash;they wrap around. That&#8217;s why it makes sense to draw them as a circle. And that&#8217;s why being attached to any particular point being 0 is a bit silly: a circle has continuous rotational symmetry, and our discrete cyclic group has N-fold rotational symmetry. We get the same image if we rotate by an N&#8217;th of a full turn. And going back to our setting, since we really only care about distances, we can cyclically rotate our points around any way we want without changing the results.</p>
<p>What these tests really do is exploit this symmetry, &#8220;translating&#8221; (or more appropriately, rotating) everything by -a. This turns testing x against the interval <img src="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ba%2Cb%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="[a,b]" class="latex" /> (or the half-open variants) into testing <img src="https://s0.wp.com/latex.php?latex=%28x-a%29+%5Cbmod+N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%28x-a%29+%5Cbmod+N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%28x-a%29+%5Cbmod+N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="(x-a) &#92;bmod N" class="latex" /> against <img src="https://s0.wp.com/latex.php?latex=%5B0%2C%28b-a%29+%5Cbmod+N%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5B0%2C%28b-a%29+%5Cbmod+N%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5B0%2C%28b-a%29+%5Cbmod+N%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="[0,(b-a) &#92;bmod N)" class="latex" />. Once we move the start point of the interval to 0, we don&#8217;t have to worry about wrap-around happening in inconvenient places anymore; comparing the integers directly works fine. Score one for symmetry.</p>
<h3>Interval overlap</h3>
<p>This takes care of points. Now for something trickier: how do we test for interval overlap?</p>
<p>The <a href="https://fgiesen.wordpress.com/2011/10/16/checking-for-interval-overlap/">standard tests for interval overlap</a> are slick, but not really applicable in our situation: the center-extent trick actually generalizes just fine to integers mod N and much more general settings (it works in arbitrary metric spaces provided the sets in question can be expressed as balls in the target metric) but is not ideal in a discrete setting, and the direct tests rely heavily on an order structure we don&#8217;t have in our cyclic world.</p>
<p>But what we do have are reasonably simple point-in-interval tests. Can we build an interval overlap test out of them? Well, we can try. Suppose we have two intervals [a,b] and [c,d]. For example, surely, if the &#8220;left&#8221; endpoint c of [c,d] falls inside [a,b], the two intervals overlap &#8211; after all, we know a point that is in both of them! And by symmetry, swapping the roles of the two intervals, if a falls inside [c,d], we again must have overlap.</p>
<p>If you start drawing a few pictures of intervals in various configurations, you&#8217;ll soon notice that testing both of these conditions seems to detect all configurations where the intervals actually overlap (and this works with intervals in the real numbers as well as in our discrete setting). The question is, can we <em>prove</em> that testing these two points is sufficient, or are we merely lucky? It turns out we didn&#8217;t just luck out; here&#8217;s a quick proof:</p>
<p><b>Lemma</b>: <img src="https://s0.wp.com/latex.php?latex=%28%5Ba%2Cb%5D+%5Ccap+%5Bc%2Cd%5D%29+%5Cne+%5Cemptyset&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%28%5Ba%2Cb%5D+%5Ccap+%5Bc%2Cd%5D%29+%5Cne+%5Cemptyset&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%28%5Ba%2Cb%5D+%5Ccap+%5Bc%2Cd%5D%29+%5Cne+%5Cemptyset&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="([a,b] &#92;cap [c,d]) &#92;ne &#92;emptyset" class="latex" /> <img src="https://s0.wp.com/latex.php?latex=%5CLeftrightarrow&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5CLeftrightarrow&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5CLeftrightarrow&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;Leftrightarrow" class="latex" /> <img src="https://s0.wp.com/latex.php?latex=%28c+%5Cin+%5Ba%2Cb%5D%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%28c+%5Cin+%5Ba%2Cb%5D%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%28c+%5Cin+%5Ba%2Cb%5D%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="(c &#92;in [a,b])" class="latex" /> or <img src="https://s0.wp.com/latex.php?latex=%28a+%5Cin+%5Bc%2Cd%5D%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%28a+%5Cin+%5Bc%2Cd%5D%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%28a+%5Cin+%5Bc%2Cd%5D%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="(a &#92;in [c,d])" class="latex" />. In words, the two (non-empty) intervals overlap if and only if at least one of a or c is inside the respective other interval.<br />
Proof: &#8220;<img src="https://s0.wp.com/latex.php?latex=%5CLeftarrow&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5CLeftarrow&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5CLeftarrow&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;Leftarrow" class="latex" />&#8221; <img src="https://s0.wp.com/latex.php?latex=c+%5Cin+%5Bc%2Cd%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=c+%5Cin+%5Bc%2Cd%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=c+%5Cin+%5Bc%2Cd%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="c &#92;in [c,d]" class="latex" />, so if we also have <img src="https://s0.wp.com/latex.php?latex=c+%5Cin+%5Ba%2Cb%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=c+%5Cin+%5Ba%2Cb%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=c+%5Cin+%5Ba%2Cb%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="c &#92;in [a,b]" class="latex" />, then that gives us a point in the intersection of [a,b] and [c,d], which therefore can&#8217;t be empty. Likewise with a. Therefore, if either of the two conditions on the right-hand side holds, we indeed have a non-empty intersection.<br />
&#8220;<img src="https://s0.wp.com/latex.php?latex=%5CRightarrow&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5CRightarrow&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5CRightarrow&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;Rightarrow" class="latex" />&#8221; The intersection isn&#8217;t empty, so take <img src="https://s0.wp.com/latex.php?latex=x+%5Cin+%28%5Ba%2Cb%5D+%5Ccap+%5Bc%2Cd%5D%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=x+%5Cin+%28%5Ba%2Cb%5D+%5Ccap+%5Bc%2Cd%5D%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=x+%5Cin+%28%5Ba%2Cb%5D+%5Ccap+%5Bc%2Cd%5D%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="x &#92;in ([a,b] &#92;cap [c,d])" class="latex" />. x is in both intervals. Informally, we now &#8220;slide&#8221; x to the &#8220;left&#8221; (in negative direction) through the intersection until we hit either of the interval end points a or c. Formally, consider the distances from the interval start points to x <img src="https://s0.wp.com/latex.php?latex=d_a+%3A%3D+d%5E%7B%5C%2C%2B%7D%28a%2Cx%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=d_a+%3A%3D+d%5E%7B%5C%2C%2B%7D%28a%2Cx%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=d_a+%3A%3D+d%5E%7B%5C%2C%2B%7D%28a%2Cx%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="d_a := d^{&#92;,+}(a,x)" class="latex" /> and <img src="https://s0.wp.com/latex.php?latex=d_c+%3A%3D+d%5E%7B%5C%2C%2B%7D%28c%2Cx%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=d_c+%3A%3D+d%5E%7B%5C%2C%2B%7D%28c%2Cx%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=d_c+%3A%3D+d%5E%7B%5C%2C%2B%7D%28c%2Cx%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="d_c := d^{&#92;,+}(c,x)" class="latex" />. Suppose that d<sub>a</sub> &le; d<sub>c</sub>. Then we have</p>
<p><img src="https://s0.wp.com/latex.php?latex=0+%5Cle+d%5E%7B%5C%2C%2B%7D%28c%2Ca%29+%3D+a+-+c+%3D+a+-+x+%2B+x+-+c&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=0+%5Cle+d%5E%7B%5C%2C%2B%7D%28c%2Ca%29+%3D+a+-+c+%3D+a+-+x+%2B+x+-+c&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=0+%5Cle+d%5E%7B%5C%2C%2B%7D%28c%2Ca%29+%3D+a+-+c+%3D+a+-+x+%2B+x+-+c&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="0 &#92;le d^{&#92;,+}(c,a) = a - c = a - x + x - c" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=%3D+%28x+-+c%29+-+%28x+-+a%29+%3D+d_c+-+d_a+%5Cle+d_c+%5Cle+d%5E%7B%5C%2C%2B%7D%28c%2Cd%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%3D+%28x+-+c%29+-+%28x+-+a%29+%3D+d_c+-+d_a+%5Cle+d_c+%5Cle+d%5E%7B%5C%2C%2B%7D%28c%2Cd%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%3D+%28x+-+c%29+-+%28x+-+a%29+%3D+d_c+-+d_a+%5Cle+d_c+%5Cle+d%5E%7B%5C%2C%2B%7D%28c%2Cd%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="= (x - c) - (x - a) = d_c - d_a &#92;le d_c &#92;le d^{&#92;,+}(c,d)" class="latex" /></p>
<p>In words, a&#8217;s distance from c, in positive direction, is no more than x&#8217;s; since x already was inside of [c,d], surely a must be too. If instead d<sub>a</sub> &gt; d<sub>c</sub>, we learn that c is inside [a,b]. In either case, this proves the claim.</p>
<p>This takes care of closed intervals. Note that this proof leans heavily on the intervals in question being non-empty. We can readily adapt it to half-open intervals of the form [a,b), but we do need to make sure to catch either interval being empty first, in which case the intersection of intervals is necessarily empty too. Likewise, you can also easily adapt it to half-open intervals of type (a,b], but in this case you want to be using the &#8220;right&#8221; end points of intervals for testing, not the left end points.</p>
<p>This may all sound complicated, but the implementation is actually quite short and simple:</p>
<pre>
// do [a,b] and [c,d] overlap?
static bool closed_intervals_overlap(int a, int b, int c, int d)
{
    return modN(c - a) &lt;= modN(b - a) || modN(a - c) &lt;= modN(d - c);
}

// do [a,b) and [c,d) overlap?
static bool half_open_intervals_overlap(int a, int b, int c, int d)
{
    int w0 = modN(b - a);
    int w1 = modN(d - c);

    return (w1 != 0 &amp;&amp; modN(c - a) &lt; w0) ||
           (w0 != 0 &amp;&amp; modN(a - c) &lt; w1);
}
</pre>
<p>And there we go. Interval overlap tests mod N.</p>
<h3>Implementation notes and variations</h3>
<p>The most common case in systems programming involves power-of-2 N. In that scenario, <code>modN</code> is readily implemented via bit masking as a single binary AND operation. If N is 2<sup>32</sup> or 2<sup>64</sup>, 32- or 64-bit unsigned integers can (and should) be used directly, in which case there is no need for explicit masking in the code (although on some 64-bit architectures, unsigned 32-bit arithmetic compiles into 64-bit arithmetic with masking anyway). This is one of the rare cases where unsigned integer overflow works <em>exactly</em> the way we need. In this case, you want to be using unsigned integers throughout.</p>
<p>As presented, we&#8217;re working with intervals given in terms of two end points, because that&#8217;s the most common presentation. But computationally, <em>all</em> of the functions in the code shown actually use a single endpoint (on the &#8220;closed&#8221; end) along with the width of the interval. That&#8217;s the <code>modN(b - a)</code> and <code>modN(d - c)</code> terms we keep computing. So if you&#8217;re working a lot with intervals mod N, or storing them, you probably want to consider working in that representation directly.</p>
<p>The intervals in this article are intentionally defined with their endpoints coming from <img src="https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D_N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D_N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D_N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mathbb{Z}_N" class="latex" />, to force us to think about the effects of the cyclical wrap-around in the integers mod N cleanly. Now that we&#8217;ve spent some time thinking it through, we can relax that requirement. In particular, when using ring buffers with what I call the &#8220;<a href="https://fgiesen.wordpress.com/2010/12/14/ring-buffers-and-queues/">virtual stream</a>&#8221; model (read/write cursors not reduced mod N), it can make sense to just not reduce the interval lengths mod N at all&mdash;that is, turn occurrences of <code>modN(b - a)</code> and <code>modN(d - c)</code> in the code into plain <code>b - a</code> and <code>d - c</code>, respectively, or reduce with respect to a modulus that&#8217;s a larger multiple of N. Among other things, this allows us to have half-open intervals covering the entirety of <img src="https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D_N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D_N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmathbb%7BZ%7D_N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mathbb{Z}_N" class="latex" />, something the fully reduced variant cannot easily do.</p>
<p>And as a final closing remark, this article comes in at 2500 words to explain less than 20 lines of code doing nothing but straightforward integer arithmetic. That has to be <em>some</em> sort of personal record. I&#8217;m not sure if that&#8217;s good or bad.</p>
]]></html><thumbnail_url><![CDATA[https://fgiesen.files.wordpress.com/2015/09/circle.png?fit=440%2C330]]></thumbnail_url><thumbnail_width><![CDATA[213]]></thumbnail_width><thumbnail_height><![CDATA[209]]></thumbnail_height></oembed>