Compare commits

..

3 Commits

2 changed files with 140 additions and 37 deletions

View File

@ -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: The procedure was rather simple:
1. Pick a number between 2 and 8 (inclusive). 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. 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. 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_ 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 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 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. direction `dir`. We also return the new direction alongside the new coordinates.
{{< codelines "Ruby" "patterns/patterns.rb" 10 21 >}} {{< codelines "Ruby" "patterns/patterns.rb" 10 21 >}}
@ -109,7 +109,7 @@ larger than \(k\).
\end{aligned} \end{aligned}
{{< /latex >}} {{< /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 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\\), 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: so it must be divisible). Thus, we can write:
@ -126,13 +126,16 @@ two numbers is divisible by a third number, we write:
{{< /latex >}} {{< /latex >}}
Some things that _seem_ like they would work from this "equation-like" notation do, indeed, work. 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 >}} {{< latex >}}
\textbf{if}\ a \equiv b\ (\text{mod}\ k)\ \textbf{and}\ c \equiv d, (\text{mod}\ k),\ \textbf{then}\ \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). a+c \equiv b+d\ (\text{mod}\ k).
{{< /latex >}} {{< /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 >}} {{< latex >}}
\textbf{if}\ a \equiv b\ (\text{mod}\ k),\ \textbf{then}\ na \equiv nb\ (\text{mod}\ k). \textbf{if}\ a \equiv b\ (\text{mod}\ k),\ \textbf{then}\ na \equiv nb\ (\text{mod}\ k).
@ -243,13 +246,29 @@ will always be 0.
Repeating remainders alone do not guarantee that we will return to the center. The repeating sequence 1,2,3,4 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 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 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 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 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 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, 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,
since after two iterations they will _also_ leave us facing backwards. 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: 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 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: 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:
@ -272,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 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? 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, 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, 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! none of which are divisible by 4. We've eliminated the possibility of spirals!
@ -303,10 +323,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? 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 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 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 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 of 3, 7 has a cycle length of 12, 9 has a cycle length of 4. What's
@ -320,25 +340,26 @@ for the length of a cycle:
k = \frac{d}{\text{gcd}(d,n)} k = \frac{d}{\text{gcd}(d,n)}
{{< /latex >}} {{< /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)\\)
with our experiment for \\(d = 9\\), too. Why might this be? Let's start once again with is the greatest common factor of \\(d\\) and \\(n\\). This equation is in agreement
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 >}} {{< latex >}}
kn \equiv 0\ (\text{mod}\ d) kn \equiv 0\ (\text{mod}\ d)
{{< /latex >}} {{< /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, 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 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 >}} {{< latex >}}
kfm \equiv 0\ (\text{mod}\ fg) kfm \equiv 0\ (\text{mod}\ fg)
{{< /latex >}} {{< /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 congruence really means is that the
difference of \\(kfm\\) and \\(0\\), which is just \\(kfm\\) is divisible by \\(fg\\): difference of \\(kfm\\) and \\(0\\), which is just \\(kfm\\), is divisible by \\(fg\\):
{{< latex >}} {{< latex >}}
fg|kfm fg|kfm
@ -351,14 +372,17 @@ we can write:
g|km g|km
{{< /latex >}} {{< /latex >}}
We also know that \\(g\\) and \\(m\\) have no common factors -- they were all divided out from \\(d\\) Can we distill this statement even further? It turns out that we can. Remember that we got \\(g\\)
and \\(n\\) when we divided by \\(f\\)! We can thus further simplify our claim: 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 (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 >}} {{< latex >}}
g|k g|k
{{< /latex >}} {{< /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 \\(d\\) by \\(f\\), which is our largest common factor -- aka \\(\\text{gcd}(d,n)\\). We can thus
write: write:
@ -366,24 +390,25 @@ write:
\frac{d}{\text{gcd}(d,n)}|k \frac{d}{\text{gcd}(d,n)}|k
{{< /latex >}} {{< /latex >}}
All that's left is to pick the smallest \\(k\\) that fits this description (that would be the first Let's stop and appreciate this result. We have found a condition that is required for a sequnce
point at which our sequence of multiples of \\(n\\) will loop). Zero is divisible by anything, but of remainders from dividing by \\(d\\) (which was 9 in the original problem) to repeat after \\(k\\)
alas, our sequence numbers start at 1 -- zero's out. The next best thing is \\(d/\\text{gcd}(d,n)\\). numbers. Furthermore, all of our steps can be performed in reverse, which means that if a \\(k\\)
And there we have it: the first point at which our sequence loops, just like we guessed. matches this conditon, we can work backwards and determine that a sequence of numbers has
to repeat after \\(k\\) 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
our hypothesis:
Lastly, recall that a cycle occurs 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 <em>this</em> 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 >}} {{< latex >}}
1 = (a-b)f k = \frac{d}{\text{gcd}(d,n)}
{{< /latex >}} {{< /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. Lastly, recall that our patterns would spiral away from the center whenever a \\(k\\) is a multiple of 4. Now that we know what
{{< /sidenote >}} which means that our condition further simplifies to \\(k\\) is, we can restate this as "\\(d/\\text{gcd}(d,n)\\) is divisible by 4". But if we pick
"\\(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. 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 For example, pick \\(d=4\\). Running our algorithm for \\(n=d-1=3\\), we indeed find an infinite
spiral: spiral:
@ -498,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_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." >}} {{< 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\\)

@ -1 +1 @@
Subproject commit 9b0c70ac05c209011e32f97a35cbca0b42267974 Subproject commit f4d4f4e5d74785e1f84e34afb2f4910a76f80cd5