From 81efcea0e52ff2298cc8f86b5817c9796348e29f Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 1 Jan 2022 14:45:11 -0800 Subject: [PATCH] Give initial stabs at Arthur's suggestions --- content/blog/modulo_patterns/index.md | 65 +++++++++++++++++++-------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/content/blog/modulo_patterns/index.md b/content/blog/modulo_patterns/index.md index a802d18..adfaebd 100644 --- a/content/blog/modulo_patterns/index.md +++ b/content/blog/modulo_patterns/index.md @@ -12,7 +12,7 @@ _"Pick a number"_, he said, _"And I'll teach you how to draw a pattern from it." The procedure was rather simple: 1. Pick a number between 2 and 8 (inclusive). -2. Start generating multiples of this number. If you picked 8, +2. Start generating positive multiples of this number. If you picked 8, your multiples would be 8, 16, 24, and so on. 3. If a multiple is more than one digit long, sum its digits. For instance, for 16, write 1+6=7. If the digits add up to a number that's still more than 1 digit long, add up the digits of _that_ @@ -37,7 +37,7 @@ characters (`"17"` becomes `["1", "7"]`), turning each of these character back i We may now encode the "drawing" logic. At any point, there's a "direction" we're going - which I'll denote by the Ruby symbols `:top`, `:bottom`, `:left`, and `:right`. Each step, we take -the current `x`/`y` coordinates (our position on the grid), and shift them by `n` in a particular +the current `x`,`y` coordinates (our position on the grid), and shift them by `n` in a particular direction `dir`. We also return the new direction alongside the new coordinates. {{< codelines "Ruby" "patterns/patterns.rb" 10 21 >}} @@ -109,7 +109,7 @@ larger than \(k\). \end{aligned} {{< /latex >}} -We only _really_ care about the remainder here, not the quotient, since it's the remainder +We only really care about the remainder here, not the quotient, since it's the remainder that determines if something is divisible or not. From the form of the second equation, we can deduce that \\(b-r\\) is divisible by \\(a\\) (it's literally equal to \\(a\\) times \\(k\\), so it must be divisible). Thus, we can write: @@ -138,6 +138,9 @@ Multiplying both sides by the same number (call it \\(n\\)) also works: \textbf{if}\ a \equiv b\ (\text{mod}\ k),\ \textbf{then}\ na \equiv nb\ (\text{mod}\ k). {{< /latex >}} +To see why this works, try rewriting the equivalences back into statements of divisibility, +then into actual equations (just like our very first \\(b-r=ka\\)). + Ok, that's a lot of notation and other _stuff_. Let's talk specifics. Of particular interest is the number 10, since our number system is _base ten_ (the value of a digit is multiplied by 10 for every place it moves to the left). The remainder of 10 when dividing by 3 is 1. Thus, @@ -248,8 +251,16 @@ and since turning four times leaves us facing "up" again, we'll end up getting _ From this, we can devise a simple condition to prevent spiraling -- the _length_ of the sequence before it repeats _cannot be a multiple of 4_. This way, whenever the cycle restarts, it will do so in a different direction: backwards, turned once to the left, or turned once to the right. Clearly repeating -the sequence backwards is guaranteed to take us back to the start. The same is true for the left and right-turn sequences, -since after two iterations they will _also_ leave us facing backwards. +the sequence backwards is guaranteed to take us back to the start. The same is true for the left and right-turn sequences, though it's less obvious. If drawing our sequence once left us turned to the right, +drawing our sequence twice will leave us turned more to the right. On a grid, two right turns are +the same as turning around. The third repetition will then undo the effects of the first one +(since we're facing backwards now), and the fourth will undo the effects of the second. + +There is an exception to this +multiple-of-4 rule: a sequence makes it back to the origin right before it starts over. +In that case, even if it's facing the very same direction it started with, all is well -- things +are just like when it first started, and the cycle repeats. I haven't found a sequence that does this, +so for our purposes, we'll stick with multiples of 4. Okay, so we want to avoid cycles with lengths divisible by four. What does it mean for a cycle to be of length _k_? It effectively means the following: @@ -303,10 +314,10 @@ is not ruled out as a valid cycle by our previous condition, we don't find any c So what is it that _really_ determines if there can be cycles or not? -Let's do some more playing around. What _are_ the actual cycle lengths when we divide by 9? +Let's do some more playing around. What are the actual cycle lengths when we divide by 9? For all but two numbers, the cycle lengths are 9. The two special numbers are 6 and 3, and they end up with a cycle length of 3. From this, we can say that the cycle length seems to depend on whether or -nor our \\(n\\) has any common factors with the divisor. +not our \\(n\\) has any common factors with the divisor. Let's explore this some more with a different divisor, say 12. We fill find that 8 has a cycle length of 3, 7 has a cycle length of 12, 9 has a cycle length of 4. What's @@ -321,8 +332,8 @@ k = \frac{d}{\text{gcd}(d,n)} {{< /latex >}} Where \\(d\\) is our divisor, which has been 9 until just recently. Indeed, this equation is in agreement -with our experiment for \\(d = 9\\), too. Why might this be? Let's start once again with -our equation for paths of length \\(k\\): +with our experiment for \\(d = 9\\), too. Why might this be? Recall that sequences with +period \\(k\\) imply the following congruence: {{< latex >}} kn \equiv 0\ (\text{mod}\ d) @@ -337,7 +348,7 @@ since this number divides \\(n\\) and \\(d\\), we can write \\(n=fm\\) for some kfm \equiv 0\ (\text{mod}\ fg) {{< /latex >}} -We can simplify this a little bit. Recall that what this equation _really_ means is that the +We can simplify this a little bit. Recall that what this equation really means is that the difference of \\(kfm\\) and \\(0\\), which is just \\(kfm\\) is divisible by \\(fg\\): {{< latex >}} @@ -351,14 +362,21 @@ we can write: g|km {{< /latex >}} -We also know that \\(g\\) and \\(m\\) have no common factors -- they were all divided out from \\(d\\) -and \\(n\\) when we divided by \\(f\\)! We can thus further simplify our claim: +Can we distill this statement even further? It turns out that we can. Remember that we got \\(g\\) +and \\(m\\) by dividing \\(d\\) and \\(n\\) by their greatest common factor, \\(f\\). This, in +turn, means that \\(g\\) and \\(m\\) have no more common factors (that aren't equal to 1). If they +did have such a common factor, say \\(h\\), we would be able to write \\(m=hi\\) and \\(g=hj\\). +Then, we would _also_ be able to write \\(n=fhi\\) and \\(d=fhj\\). But now, \\(fh\\) is a common +factor of both \\(n\\) and \\(d\\), and it's bigger than \\(f\\) (since \\(h\\) is not 1)! This +violates our assumption that \\(f\\) is the greatest common factor. So there can't be another shared +fadctor between \\(g\\) and \\(m\\). From this, in turn, we can deduce that \\(m\\) is not +relevant to \\(g\\) dividing \\(km\\), and we get: {{< latex >}} g|k {{< /latex >}} -This says that \\(k\\) must be divisible by \\(g\\). Recall that we got \\(g\\) by dividing +That is, we get that \\(k\\) must be divisible by \\(g\\). Recall that we got \\(g\\) by dividing \\(d\\) by \\(f\\), which is our largest common factor -- aka \\(\\text{gcd}(d,n)\\). We can thus write: @@ -366,12 +384,23 @@ write: \frac{d}{\text{gcd}(d,n)}|k {{< /latex >}} -All that's left is to pick the smallest \\(k\\) that fits this description (that would be the first -point at which our sequence of multiples of \\(n\\) will loop). Zero is divisible by anything, but -alas, our sequence numbers start at 1 -- zero's out. The next best thing is \\(d/\\text{gcd}(d,n)\\). -And there we have it: the first point at which our sequence loops, just like we guessed. +Let's stop and appreciate this result. We have found a condition that is required for a sequnce +of remainders from dividing by \\(d\\) (which was 9 in the original problem) to repeat after \\(k\\) +numbers. Furthermore, all of our steps can be performed in reverse, which means that if a \\(k\\) +matches this conditon, we can work backwards and determine that a sequence of numbers has +to repeat after \\(k\\) steps. -Lastly, recall that a cycle occurs whenever a \\(k\\) is a multiple of 4. Now that we know what +Multiple \\(k\\)s will this condition, and that's not surprising. If a sequence repeats after 5 steps, +it also repeats after 10, 15, and so on. We're interested in the first time our sequences repeat after +taking any steps, which means we have to pick the smallest possible non-zero value of \\(k\\). The smallest +number divisible by \\(d/\\text{gcd}(d,n)\\) is \\(d/\\text{gcd}(d,n)\\) itself. We thus confirm +our hypothesis: + +{{< latex >}} +k = \frac{d}{\text{gcd}(d,n)} +{{< /latex >}} + +Lastly, recall that our patterns would spiral away from the center whenever a \\(k\\) is a multiple of 4. Now that we know what \\(k\\) is, we can restate this as "\\(d/\\text{gcd}(d,n)\\) is divisible by 4". But if we pick \\(n=d-1\\), the {{< sidenote "right" "coprime-note" "greatest common factor has to be \(1\)," >}}