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
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".
{{<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.">}}
(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.">}}
{{<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
{{<figuresrc="turn_3_2.gif"caption="Orientations when turning 120 degrees, twice at a time"class="small"alt="Possible orientations when turning 120 degrees, twice at a time.">}}
{{<figuresrc="turn_5_2.gif"caption="Orientations when turning 72 degrees, twice at a time"class="small"alt="Possible orientations when turning 72 degrees, twice at a time.">}}
Let's try put some mathematical backing to this "visited them all" idea, and turning in general.
First, observe that as soon as we turn 360 degrees, it's as good as not turning at all - we end
up facing up again. If we turned 480 degrees (that is, two turns of 240 degrees each), the first
360 can be safely ignored, since it puts us where we started; only the 120 degrees that remain
are needed to figure out our final bearing. In short, the final direction we're facing is
the remainder from dividing by 360. We already know how to formulate this using modular arithmetic:
if we turn \\(t\\) degrees \\(k\\) times, and end up at final bearing (remainder) \\(b\\), this
is captured by:
{{<latex>}}
kt \equiv b\ (\text{mod}\ 360)
{{</latex>}}
Of course, if we end up facing the same way we started, we get the familiar equivalence:
{{<latex>}}
kt \equiv 0\ (\text{mod}\ 360)
{{</latex>}}
Even though the variables in this equivalence mean different things now than they did last
time we saw it, the mathematical properties remain the same. For instance, we can say that
after \\(360/\\text{gcd}(360, t)\\) turns, we'll end up facing the way that we started.
So far, so good. What I don't like about this, though, is that we have all of these
numbers of degrees all over our equations: 72 degrees, 144 degrees, and so forth. However,
We're close now: we have a sequence of \\(k\\) steps that will lead us back to the beginning.
What's left is to show that these \\(k\\) steps are evenly distributed throughout our circle,
which is the key property that makes it possible for us to make a polygon out of them (and
thus end up back where we started).
To show this, say that we have a largest common divisor \\(f=\\text{gcd}(c,r)\\), and that \\(c=fe\\) and \\(r=fs\\). We can once again "divide through" by \\(f\\), and
get:
{{<latex>}}
ks \equiv 0\ (\text{mod}\ e)
{{</latex>}}
Now, we know that \\(\\text{gcd}(e,s)=1\\) ([see this section below](#numbers-divided-by-their-textgcd-have-no-common-factors)), and thus:
{{<latex>}}
k = e/\text{gcd}(e,s) = e
{{</latex>}}
That is, our cycle will repeat after \\(e\\) remainders. But wait, we've only got \\(e\\) possible
remainders: the numbers \\(0\\) through \\(e-1\\)! Thus, for a cycle to repeat after \\(e\\) remainders,
all possible remainders must occur. For a concrete example, take \\(e=5\\); our remainders will
be the set \\(\\{0,1,2,3,4\\}\\). Now, let's "multiply back through"
by \\(f\\):
{{<latex>}}
kfs \equiv 0\ (\text{mod}\ fe)
{{</latex>}}
We still have \\(e\\) possible remainders, but this time they are multiplied by \\(f\\).
For example, taking \\(e\\) to once again be equal to \\(5\\), we have the set of possible remainders
\\(\\{0, f, 2f, 3f, 4f\\}\\). The important bit is that these remainders are all evenly spaced, and
that space between them is \\(f=\\text{gcd}(c,r)\\).
Let's recap: we have confirmed that for \\(c\\) possible turns (4 in our original formulation),
and \\(r\\) turns at a time, we will always loop after \\(k=c/\\text{gcd}(c,r)\\) steps,
evenly spaced out at \\(\\text{gcd}(c,r)\\) turns. No specific properties from \\(c\\) or \\(r\\)
are needed for this to work. Finally, recall from the previous
section that \\(r\\) is zero (and thus, our pattern breaks down) whenever the divisor \\(d\\) (9 in our original formulation) is itself
divisible by \\(c\\). And so, __as long as we pick a system with \\(c\\) possible directions
and divisor \\(d\\), we will always loop back and create a pattern as long as \\(c\\nmid d\\) (\\(c\\)
does not divide \\(d\\))__.
Let's try it out! There's a few pictures below. When reading the captions, keep in mind that the _base_
is one more than the _divisor_ (we started with numbers in the usual base 10, but divided by 9).
{{<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_4_t7.svg"caption="Pattern generated by the number 3 in base 5 while turning 7 times."class="tiny"alt="Pattern generated by the number 3 by summing digits in base 5 and turning 51 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.">}}
{{<figuresrc="pattern_2_11_t7.svg"caption="Pattern generated by the number 2 in base 12 while turning 7 times."class="tiny"alt="Pattern generated by the number 2 by summing digits in base 12 and turning 51 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