diff --git a/content/blog/modulo_patterns/index.md b/content/blog/modulo_patterns/index.md index adfaebd..6662b1e 100644 --- a/content/blog/modulo_patterns/index.md +++ b/content/blog/modulo_patterns/index.md @@ -126,21 +126,21 @@ two numbers is divisible by a third number, we write: {{< /latex >}} Some things that _seem_ like they would work from this "equation-like" notation do, indeed, work. -For instance, we can "add two equations": +For instance, we can "add two equations" (I'll omit the proof here; jump down to [this +section](#adding-two-congruences) to see how it works): {{< latex >}} \textbf{if}\ a \equiv b\ (\text{mod}\ k)\ \textbf{and}\ c \equiv d, (\text{mod}\ k),\ \textbf{then}\ a+c \equiv b+d\ (\text{mod}\ k). {{< /latex >}} -Multiplying both sides by the same number (call it \\(n\\)) also works: + +Multiplying both sides by the same number (call it \\(n\\)) also works (once +again, you can find the proof in [this section below](#multiplying-both-sides-of-a-congruence)). {{< latex >}} \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, @@ -246,7 +246,15 @@ will always be 0. Repeating remainders alone do not guarantee that we will return to the center. The repeating sequence 1,2,3,4 will certainly cause a spiral. The reason is that, if we start facing "up", we will always move up 1 and down 3 after four steps, leaving us 2 steps below where we started. Next, the cycle will repeat, -and since turning four times leaves us facing "up" again, we'll end up getting _further_ down. +and since turning four times leaves us facing "up" again, we'll end up getting _further_ away. Here's +a picture that captures this behvior: + +{{< figure src="pattern_1_4.svg" caption="Spiral generated by the number 1 with divisor 4." class="tiny" alt="Spiral generated by the number 1 by summing digits." >}} + +And here's one more where the cycle repeats after 8 steps instead of 4. You can see that it also +leads to a spiral: + +{{< figure src="pattern_1_8.svg" caption="Spiral generated by the number 1 with divisor 8." class="tiny" alt="Spiral generated by the number 1 by summing digits." >}} 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 @@ -257,10 +265,10 @@ the same as turning around. The third repetition will then undo the effects of t (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. +multiple-of-4 rule: if 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. +so for our purposes, we'll stick with avoiding 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: @@ -283,7 +291,8 @@ between 2 and 8. What went wrong? Turns out, it's that last step: we can't alway Some values of \\(k\\) are special, and it's only _those_ values that can serve as cycle lengths without causing a contradiction. So, what are they? -They're values that have a common factor with 9. There are many numbers that have a common +They're values that have a common factor with 9 (an incomplete explanation is in +[this section below](#invertible-numbers-textmod-d-share-no-factors-with-d)). There are many numbers that have a common factor with 9; 3, 6, 9, 12, and so on. However, those can't all serve as cycle lengths: as we said, cycles can't get longer than 9. This leaves us with 3, 6, and 9 as _possible_ cycle lengths, none of which are divisible by 4. We've eliminated the possibility of spirals! @@ -331,7 +340,8 @@ for the length of a cycle: 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 +Where \\(d\\) is our divisor, which has been 9 until just recently, and \\(\\text{gcd}(d,n)\\) +is the greatest common factor of \\(d\\) and \\(n\\). This equation is in agreement with our experiment for \\(d = 9\\), too. Why might this be? Recall that sequences with period \\(k\\) imply the following congruence: @@ -339,17 +349,17 @@ period \\(k\\) imply the following congruence: kn \equiv 0\ (\text{mod}\ d) {{< /latex >}} -Here we've replaced 9 with \\(d\\), since we're trying to make it work for _any_ divisor, not just 9. +Here I've replaced 9 with \\(d\\), since we're trying to make it work for _any_ divisor, not just 9. Now, suppose the greatest common divisor of \\(n\\) and \\(d\\) is some number \\(f\\). Then, since this number divides \\(n\\) and \\(d\\), we can write \\(n=fm\\) for some \\(m\\), and -\\(d=fg\\) for some \\(g\\). We can rewrite our equation as follows: +\\(d=fg\\) for some \\(g\\). We can rewrite our congruence as follows: {{< latex >}} kfm \equiv 0\ (\text{mod}\ fg) {{< /latex >}} -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\\): +We can simplify this a little bit. Recall that what this congruence really means is that the +difference of \\(kfm\\) and \\(0\\), which is just \\(kfm\\), is divisible by \\(fg\\): {{< latex >}} fg|kfm @@ -364,12 +374,8 @@ g|km 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 +turn, means that \\(g\\) and \\(m\\) have no more common factors that aren't equal to 1 (see +[this section below](#numbers-divided-by-their-textgcd-have-no-common-factors)). From this, in turn, we can deduce that \\(m\\) is not relevant to \\(g\\) dividing \\(km\\), and we get: {{< latex >}} @@ -390,7 +396,7 @@ numbers. Furthermore, all of our steps can be performed in reverse, which means matches this conditon, we can work backwards and determine that a sequence of numbers has to repeat after \\(k\\) steps. -Multiple \\(k\\)s will this condition, and that's not surprising. If a sequence repeats after 5 steps, +Multiple \\(k\\)s will match 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 @@ -402,17 +408,7 @@ k = \frac{d}{\text{gcd}(d,n)} 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\)," >}} -Wait, why is this true? Well, suppose some number \(f\) divides both \(d\) and \(d-1\). -In that case, we can write \(d=af\), and \((d-1)=bf\). Subtracting one equation from the other: -{{< latex >}} -1 = (a-b)f -{{< /latex >}} -But this means that 1 is divisible by \(f\)! That's only possible if \(f=1\). Thus, the only -number that divides \(x\) and \(x-1\) is 1; that's our greatest common factor. -{{< /sidenote >}} which means that our condition further simplifies to -"\\(d\\) is divisible by 4". +\\(n=d-1\\), the greatest common factor has to be \\(1\\) (see [this section below](#divisors-of-n-and-n-1)), so we can even further simplify this "\\(d\\) is divisible by 4". Thus, we can state simply that any divisor divisible by 4 is off-limits, as it will induce loops. For example, pick \\(d=4\\). Running our algorithm for \\(n=d-1=3\\), we indeed find an infinite spiral: @@ -527,3 +523,81 @@ But let's not be so boring. We can branch out some, of course. {{< figure src="pattern_1_7_t5.svg" caption="Pattern generated by the number 1 in base 8 while turning 5 times." class="tiny" alt="Pattern generated by the number 1 by summing digits in base 8 and turning 72 degrees." >}} {{< figure src="pattern_3_11_t6.svg" caption="Pattern generated by the number 3 in base 12 while turning 6 times." class="tiny" alt="Pattern generated by the number 3 by summing digits in base 12 and turning 60 degrees." >}} + +### Omitted Proofs + +#### Adding Two Congruences +__Claim__: If for some numbers \\(a\\), \\(b\\), \\(c\\), \\(d\\), and \\(k\\), we have +\\(a \\equiv b\\ (\\text{mod}\\ k)\\) and \\(c \\equiv d\\ (\\text{mod}\\ k)\\), then +it's also true that \\(a+c \\equiv b+d\\ (\\text{mod}\\ k)\\). + +__Proof__: By definition, we have \\(k|(a-b)\\) and \\(k|(c-d)\\). This, in turn, means +that for some \\(i\\) and \\(j\\), \\(a-b=ik\\) and \\(c-d=jk\\). Add both sides to get: +{{< latex >}} + \begin{aligned} + & (a-b)+(c-d) = ik+jk \\ + \Rightarrow\ & (a+c)-(b+d) = (i+j)k \\ + \Rightarrow\ & k\ |\left[(a+c)-(b+d)\right]\\ + \Rightarrow\ & a+c \equiv b+d\ (\text{mod}\ k) \\ + \end{aligned} +{{< /latex >}} +\\(\\blacksquare\\) + +#### Multiplying Both Sides of a Congruence +__Claim__: If for some numbers \\(a\\), \\(b\\), \\(n\\) and \\(k\\), we have +\\(a \\equiv b\\ (\\text{mod}\\ k)\\) then we also have that \\(an \\equiv bn\\ (\\text{mod}\\ k)\\). + +__Proof__: By definition, we have \\(k|(a-b)\\). Since multiplying \\(a-b\\) but \\(n\\) cannot +make it _not_ divisible by \\(k\\), we also have \\(k|\\left[n(a-b)\\right]\\). Distributing +\\(n\\), we have \\(k|(na-nb)\\). By definition, this means \\(na\\equiv nb\\ (\\text{mod}\\ k)\\). + +\\(\\blacksquare\\) + +#### Invertible Numbers \\(\\text{mod}\\ d\\) Share no Factors with \\(d\\) +__Claim__: A number \\(k\\) is only invertible (can be divided by) in \\(\\text{mod}\\ d\\) if \\(k\\) +and \\(d\\) share no common factors (except 1). + +__Proof__: Write \\(\\text{gcd}(k,d)\\) for the greatest common factor divisor of \\(k\\) and \\(d\\). +Another important fact (not proven here, but see something [like this](https://sharmaeklavya2.github.io/theoremdep/nodes/number-theory/gcd/gcd-is-min-lincomb.html)), is that if \\(\\text{gcd}(k,d) = r\\), +then the smallest possible number that can be made by adding and subtracting \\(k\\)s and \\(d\\)s +is \\(r\\). That is, for some \\(i\\) and \\(j\\), the smallest possible positive value of \\(ik + jd\\) is \\(r\\). + +Now, note that \\(d \\equiv 0\\ (\\text{mod}\\ d)\\). Multiplying both sides by \\(j\\), get +\\(jd\\equiv 0\\ (\\text{mod}\\ d)\\). This, in turn, means that the smallest possible +value of \\(ik+jd \\equiv ik\\) is \\(r\\). If \\(r\\) is bigger than 1 (i.e., if +\\(k\\) and \\(d\\) have common factors), then we can't pick \\(i\\) such that \\(ik\\equiv1\\), +since we know that \\(r>1\\) is the least possible value we can make. There is therefore no +multiplicative inverse to \\(k\\). Alternatively worded, we cannot divide by \\(k\\). + +\\(\\blacksquare\\) + +#### Numbers Divided by Their \\(\\text{gcd}\\) Have No Common Factors +__Claim__: For any two numbers \\(a\\) and \\(b\\) and their largest common factor \\(f\\), +if \\(a=fc\\) and \\(b=fd\\), then \\(c\\) and \\(d\\) have no common factors other than 1 (i.e., +\\(\\text{gcd}(c,d)=1\\)). + +__Proof__: Suppose that \\(c\\) and \\(d\\) do have sommon factor, \\(e\\neq1\\). In that case, we have +\\(c=ei\\) and \\(d=ej\\) for some \\(i\\) and \\(j\\). then, we have \\(a=fei\\), and \\(b=fej\\). +From this, it's clear that both \\(a\\) and \\(b\\) are divisible by \\(fe\\). Since \\(e\\) +is greater than \\(1\\), \\(fe\\) is greater than \\(f\\). But our assumptions state that +\\(f\\) is the greatest common divisor of \\(a\\) and \\(b\\)! We have arrived at a contradiction. + +Thus, \\(c\\) and \\(d\\) cannot have a common factor other than 1. + +\\(\\blacksquare\\) + +#### Divisors of \\(n\\) and \\(n-1\\). +__Claim__: For any \\(n\\), \\(\\text{gcd}(n,n-1)=1\\). That is, \\(n\\) and \\(n-1\\) share +no common divisors. + +__Proof__: Suppose some number \\(f\\) divides both \\(n\\) and \\(n-1\\). +In that case, we can write \\(n=af\\), and \\((n-1)=bf\\) for some \\(a\\) and \\(b\\). +Subtracting one equation from the other: + +{{< latex >}} +1 = (a-b)f +{{< /latex >}} +But this means that 1 is divisible by \\(f\\)! That's only possible if \\(f=1\\). Thus, the only +number that divides \\(n\\) and \\(n-1\\) is 1; that's our greatest common factor. + +\\(\\blacksquare\\)