direction `dir`. We also return the new direction alongside the new coordinates.
{{<codelines"Ruby""patterns/patterns.rb"1021>}}
The top-level algorithm is captured by the following code, which produces a list of
coordinates in the order that you'd visit them.
{{<codelines"Ruby""patterns/patterns.rb"2335>}}
I will omit the code for generating SVGs from the body of the article -- you can always find the complete
source code in this blog's Git repo (or by clicking the link in the code block above). Let's run the code on a few other numbers. Here's one for 4, for instance:
{{<figuresrc="pattern_4.svg"caption="Pattern generated by the number 4."class="tiny"alt="Pattern generated by the number 4.">}}
And one more for 2, which I don't find as pretty.
{{<figuresrc="pattern_2.svg"caption="Pattern generated by the number 2."class="tiny"alt="Pattern generated by the number 2.">}}
It really does always work out! Young me was amazed, though I would often run out of space on my
grid paper to complete the pattern, or miscount the length of my lines partway in. It was only
recently that I started thinking about _why_ it works, and I think I figured it out. Let's take a look!
### Is a number divisible by 3?
You might find the whole "add up the digits of a number" thing familiar, and for good reason:
it's one way to check if a number is divisible by 3. The quick summary of this result is,
> If the sum of the digits of a number is divisible by 3, then so is the whole number.
For example, the sum of the digits of 72 is 9, which is divisible by 3; 72 itself is correspondingly
also divisible by 3, since 24*3=72. On the other hand, the sum of the digits of 82 is 10, which
is _not_ divisible by 3; 82 isn't divisible by 3 either (it's one more than 81, which _is_ divisible by 3).
Why does _this_ work? Let's talk remainders.
If a number doesn't cleanly divide another (we're sticking to integers here),
what's left behind is the remainder. For instance, dividing 7 by 3 leaves us with a remainder 1.
On the other hand, if the remainder is zero, then that means that our dividend is divisible by the
divisor (what a mouthful). In mathematics, we typically use
\\(a|b\\) to say \\(a\\) divides \\(b\\), or, as we have seen above, that the remainder of dividing
\\(b\\) by \\(a\\) is zero.
Working with remainders actually comes up pretty commonly in discrete math. A well-known
example I'm aware of is the [RSA algorithm](https://en.wikipedia.org/wiki/RSA_(cryptosystem)),
which works with remainders resulting from dividing by a product of two large prime numbers.
But what's a good way to write, in numbers and symbols, the claim that "\\(a\\) divides \\(b\\)
with remainder \\(r\\)"? Well, we know that dividing yields a quotient (possibly zero) and a remainder
(also possibly zero). Let's call the quotient \\(k\\).
{{<sidenote"right""r-less-note""Then,weknowthatwhendividing \(b\) by \(a\) wehave:">}}
It's important to point out that for the equation in question to represent division
with quotient \(k\) and remainder \(r\), it must be that \(r\) is less than \(a\).
Otherwise, you could write \(r = s + a\) for some \(s\), and end up with
{{<latex>}}
\begin{aligned}
& b = ka + r \\
\Rightarrow\ & b = ka + (s + a) \\
\Rightarrow\ & b = (k+1)a + s
\end{aligned}
{{</latex>}}
In plain English, if \(r\) is bigger than \(a\) after you've divided, you haven't
taken out "as much \(a\) from your dividend as you could", and the actual quotient is
We can put this to good use. Let's take a large number that's divisible by 3. This number
will be made of multiple digits, like \\(d_2d_1d_0\\). Note that I do __not__ mean multiplication
here, but specifically that each \\(d_i\\) is a number between 0 and 9 in a particular place
in the number -- it's a digit. Now, we can write:
{{<latex>}}
\begin{aligned}
0 &\equiv d_2d_1d_0 \\
& = 100d_2 + 10d_1 + d_0 \\
& \equiv d_2 + d_1 + d_0
\end{aligned}
{{</latex>}}
We have just found that \\(d_2+d_1+d_0 \\equiv 0\\ (\\text{mod}\ 3)\\), or that the sum of the digits
is also divisible by 3. The logic we use works in the other direction, too: if the sum of the digits
is divisible, then so is the actual number.
There's only one property of the number 3 we used for this reasoning: that \\(10 \\equiv 1\\ (\\text{mod}\\ 3)\\). But it so happens that there's another number that has this property: 9. This means
that to check if a number is divisible by _nine_, we can also check if the sum of the digits is
divisible by 9. Try it on 18, 27, 81, and 198.
Here's the main takeaway: __summing the digits in the way described by my headmaster is
the same as figuring out the remainder of the number from dividing by 9__. Well, almost.
The difference is the case of 9 itself: the __remainder__ here is 0, but we actually use 9
to draw our line. We can actually try just using 0. Here's the updated `sum_digits` code:
```Ruby
def sum_digits(n)
n % 9
end
```
The results are similarly cool:
{{<figuresrc="pattern_8_mod.svg"caption="Pattern generated by the number 8."class="tiny"alt="Pattern generated by the number 8 by just using remainders.">}}
{{<figuresrc="pattern_4_mod.svg"caption="Pattern generated by the number 4."class="tiny"alt="Pattern generated by the number 4 by just using remainders.">}}
{{<figuresrc="pattern_2_mod.svg"caption="Pattern generated by the number 2."class="tiny"alt="Pattern generated by the number 2 by just using remainders.">}}
### Sequences of Remainders
So now we know what the digit-summing algorithm is really doing. But that algorithm isn't all there
is to it! We're repeatedly applying this algorithm over and over to multiples of another number. How
First, let's take a closer look at our sequence of multiples. Suppose we're working with multiples
of some number \\(n\\). Let's write \\(a_i\\) for the \\(i\\)th multiple. Then, we end up with:
{{<latex>}}
\begin{aligned}
a_1 &= n \\
a_2 &= 2n \\
a_3 &= 3n \\
a_4 &= 4n \\
... \\
a_i &= in
\end{aligned}
{{</latex>}}
This is actually called an [arithmetic sequence](https://mathworld.wolfram.com/ArithmeticProgression.html);
for each multiple, the number increases by \\(n\\).
Here's a first seemingly trivial point: at some time, the remainder of \\(a_i\\) will repeat.
There are only so many remainders when dividing by nine: specifically, the only possible remainders
are the numbers 0 through 8. We can invoke the [pigeonhole principle](https://en.wikipedia.org/wiki/Pigeonhole_principle) and say that after 9 multiples, we will have to have looped. Another way
of seeing this is as follows:
{{<latex>}}
\begin{aligned}
& 9 \equiv 0\ (\text{mod}\ 9) \\
\Rightarrow\ & 9n \equiv 0\ (\text{mod}\ 9) \\
\Rightarrow\ & 10n \equiv n\ (\text{mod}\ 9) \\
\end{aligned}
{{</latex>}}
The 10th multiple is equivalent to n, and will thus have the same remainder. The looping may
happen earlier: the simplest case is if we pick 9 as our \\(n\\), in which case the remainder
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_ away. Here's
a picture that captures this behvior:
{{<figuresrc="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:
{{<figuresrc="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.">}}
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.
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:
\\(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:
{{<figuresrc="pattern_3_4.svg"caption="Spiral generated by the number 3 with divisor 4."class="tiny"alt="Spiral generated by the number 3 by summing digits.">}}
Let's try again. Pick \\(d=8\\); then, for \\(n=d-1=7\\), we also get a spiral:
{{<figuresrc="pattern_7_8.svg"caption="Spiral generated by the number 7 with divisor 8."class="tiny"alt="Spiral generated by the number 7 by summing digits.">}}
A poem comes to mind:
> Turning and turning in the wydening gyre
>
> The falcon cannot hear the falconner;
Fortunately, there are plenty of numbers that are not divisible by four, and we can pick
any of them! I'll pick primes for good measure. Here are a few good ones from using 13
(which corresponds to summing digits of base-14 numbers):
{{<figuresrc="pattern_8_13.svg"caption="Pattern generated by the number 8 in base 14."class="tiny"alt="Pattern generated by the number 8 by summing digits.">}}
{{<figuresrc="pattern_4_13.svg"caption="Pattern generated by the number 4 in base 14."class="tiny"alt="Pattern generated by the number 4 by summing digits.">}}
{{<figuresrc="pattern_5_17.svg"caption="Pattern generated by the number 5 in base 18."class="tiny"alt="Pattern generated by the number 5 by summing digits.">}}
Finally, base-30:
{{<figuresrc="pattern_2_29.svg"caption="Pattern generated by the number 2 in base 30."class="tiny"alt="Pattern generated by the number 2 by summing digits.">}}
{{<figuresrc="pattern_6_29.svg"caption="Pattern generated by the number 6 in base 30."class="tiny"alt="Pattern generated by the number 6 by summing digits.">}}
### Generalizing to Arbitrary Numbers of Directions
What if we didn't turn 90 degrees each time? What, if, instead, we turned 120 degrees (so that
turning 3 times, not 4, would leave you facing the same direction you started)? We can pretty easily
do that, too. Let's call this number of turns \\(c\\). Up until now, we had \\(c=4\\).
First, let's update our condition. Before, we had "\\(d\\) cannot be divisible by 4". Now,
we aren't constraining ourselves to only 4, but rather using a generic variable \\(c\\).
We then end up with "\\(d\\) cannot be divisible by \\(c\\)". For instance, suppose we kept
our divisor as 9 for the time being, but started turning 3 times instead of 4. This
violates are divisibility condtion, and we once again end up with a spiral:
{{<figuresrc="pattern_8_9_t3.svg"caption="Pattern generated by the number 8 in base 10 while turning 3 times."class="tiny"alt="Pattern generated by the number 3 by summing digits and turning 120 degrees.">}}
If, on the other hand, we pick \\(d=8\\) and \\(c=3\\), we get patterns for all numbers just like we hoped.
Here's one such pattern:
{{<figuresrc="pattern_7_8_t3.svg"caption="Pattern generated by the number 7 in base 9 while turning 3 times."class="tiny"alt="Pattern generated by the number 7 by summing digits in base 9 and turning 120 degrees.">}}
Hold on a moment; it's actully not so obvious why our condition _still_ works. When we just turned
on a grid, things were simple. As long as we didn't end up facing the same way we started, we will
eventually perform the exact same motions in reverse. The same is not true when turning 120 degrees, like
we suggested. Here's a circle with the turn angles labeled:
{{<figuresrc="turn_3_1.png"caption="Orientations when turning 120 degrees"class="small"alt="Possible orientations when turning 120 degrees.">}}
We never quite do the exact _opposite_ of any one of our movements. So then, will we come back to the
origin anyway? Well, let's start simple. Suppose we always turn by exactly one 120-degree increment
(we might end up turning more or less, just like we may end up turning left, right, or back in the
90 degree case). Now,
1. Suppose that having performed one complete cycle, we end up away from the center
by \\(dx\\) on the \\(x\\)-axis, and \\(dy\\) on the \\(y\\)-axis (we do this without loss
of generality).
2. We are now turned around by 120 degrees, so once we perform the cycle again, we end up offset
by \\(dx(\\cos 120)-dy(\\sin 120)\\) on the \\(x\\)-axis, and \\(dx(\\sin 120)+dy(\\cos 120)\\) on
the \\(y\\)-axis (I got this from the [rotation matrx](https://en.wikipedia.org/wiki/Rotation_matrix)
page on Wikipedia).
3. After one more step, we end up with having rotated a total of 240 degrees. As we perform the cycle
again, we end up having moved by an additional \\(dx(\\cos 240)-dy(\\sin 240)\\) and \\(dx(\\sin 240)+dy(\\cos 240)\\).
For reasons beyond the scope of this article, sums like those between the square brackets
in the above equations _always_ equal zero. This means that after all the turns have been made,
we get \\(x=0\\) and \\(y=0\\) -- back at the origin, where we started!
{{<todo>}}Maybe we can prove the sin/cos thing? {{</todo>}}
What if we turn by 240 degrees at a time: 2 turns instead of 1? Even though we first turn
a whole 240 degrees, the second time we turn we "overshoot" our initial bearing, and end up at 120 degrees
compared to it. As soon as we turn 240 more degrees (turning the third time), we end up back at 0.
In short, even though we "visited" each bearing in a different order, we visited them all.
{{<figuresrc="turn_3_2.png"caption="Orientations when turning 120 degrees, twice at a time"class="small"alt="Possible orientations when turning 120 degrees, twice at a time.">}}
Let's try put some mathematical backing to this "visited them all" idea.
{{<todo>}}Remainders, visited them all, etc.{{</todo>}}
But let's not be so boring. We can branch out some, of course.
{{<figuresrc="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.">}}
{{<figuresrc="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.">}}
__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.