Cookie math

This page contains all the math behind the post “Cookie calculations.”

Calculation of lattice cookie density

To compare the ideal efficiency of different arrangements, we can look at each lattice’s representative “unit cell” — i.e., the shape that is repeated in the pattern:

unit cells

In a square lattice, each of the squares holds four cookie quarters, one in each corner, for a total of one cookie. The square takes up an area c^2, where c is the distance between cookie centers. So the “cookie density” is:

(1 cookie per square)/(area of the square) = 1/c^2

Each triangle in the triangular lattice, meanwhile, holds three sixths of a cookie (for a total of one half cookie) and takes up area (1/2)(base)(height) = (1/2)(c)(\sqrt3 c/2) = .433c^2. So its cookie density is:

(1/2 cookie per triangle)/(area of the triangle) = .5/(.433c^2) = 1.15/c^2

Alternately, to calculate what percent of the area is taken up by cookies (rather than the space between them), divide the area of cookie in the unit cell by the total unit cell area. (This is equivalent to multiplying the area of a cookie by the cookie density.) When the cookies are touching, the radius of a cookie equals c/2, so the area is \pi(radius)^2 = \pi c^2 /4.

square unit cell: (\pi c^2 /4)/(c^2) = 78.5 \%

triangular unit cell: (\pi c^2 /8)/(.433c^2) = 90.7 \%

Those are the theoretical values. When you have boundaries, however, the cookie density at the edges is lower due to there being extra space there (but not enough extra space to fit another cookie). The only time the density reaches the theoretical value is for a square lattice on a cookie sheet that’s an exact number of cookies in both dimensions. Here is a graph showing how cookie coverage depends on width, when the length is 4.0 cookies:

cookie density (length = 4.0 cookies)The square lattice hits its theoretical value at integer values of width; in between, the density drops off as the cookie sheet area increases while the number of cookies that fit stays the same. The triangular lattice is well below its theoretical value, but does asymptote toward a higher value than the square lattice does, in the limit of very large width.

When the width is 3.5 or 4.5 cookies, the triangular lattice always has an equal or higher density than the square lattice. Lattice densities are both much lower than they would be without boundaries.

For smaller cookies or larger cookie sheets (9.5 cookies long, 10.0 cookies long), the densities are larger. This is due to the decrease in the perimeter:area ratio (the 2D equivalent of a surface area: volume ratio). Only for very large cookie sheets (99.5 cookies long, 100.0 cookies long), does the density of the triangular lattice seem to really approach that ideal 90.7%.

Long-term behavior with unlimited width

We’ve seen that the best lattice to use flips back and forth between square and triangular when the dimensions of the cookie sheet are small. (This is when you have a set length, and are looking at what happens for different widths.) But does one or the other win out at larger widths? To figure this out, consider the average rate at which cookies are added with increasing width — i.e., the average steepness of the stepped incline.

The steepness of the step function for a square lattice with n cookies per row is:

(cookies per row)/(distance between rows) = n/1 = n

The steepness of the step function for an triangular lattice with n cookies in the first row but n - 1 in the second (then n in the third, n - 1 in the fourth, and so on . . .) is:

(average cookies per row)/(distance between rows) = (n - .5)/(\sqrt3 /2) = (n - .5)/.886

With a wide enough cookie sheet, triangular lattices will will out despite having one fewer cookie in every other row when:

(n - .5)/.886 > n

.155n > .577

n > 3.73

That means that a triangular lattice with rows of 4-3-4-3-4… will ultimately, once you get above some threshold width, always be more efficient than an square lattice with 4 in every row. But a triangular lattice with rows of 3-2-3-2-3… will ultimately always be less efficient than a square lattice with 3 in every row.

This definitely falls into the category of “curiosity” rather than “useful fact,” though (even more so than the rest of this exercise), since the “thresholds” in question are quite large (91 cookies wide and 27 cookies wide, respectively). So for any sizes of interest, whether a square lattice or a triangular lattice is better depends on the specific width of the pan.

Equation for the number of cookies that fit on a cookie sheet

First, for the sake of absolute clarity, let’s explicitly define the variables:

  • L = cookie sheet length (measured in the units of your choice — inches, cm, furlongs, light-years, etc)
  • W = cookie sheet width (measured in the same units as length)
  • C = distance between cookie centers (measured in the same units as length and width)

Next, we can simplify to two variables with dimensionless units:

w = W/C = width measured in cookies

l = L/C = length measured in cookies

Finally, I should explain that I’ll be using two somewhat uncommon functions:

  1. The floor function, written as \lfloor x\rfloor when operating on the variable x, is a mathematical function that rounds a real number down to the nearest integer. (So long as you’re dealing with positive numbers, it’s the same as just dropping whatever is to the right of the decimal point.)
  2. The modulo function, written a \bmod b for variables a and b, gives the remainder when you divide a by b. For example, 12 \bmod 4 = 0 (because 4 goes into 12 evenly), 12 \bmod 5 = 2, and 12.5 \bmod 1 = .5.

That done, it’s quite simple to calculate the number of cookies in the largest square lattice that will fit on the cookie sheet. Just round the length and width down to the nearest integer, and multiply them together:

    \begin{equation*} \text{cookies}_{\blacksquare} = \lfloor l\rfloor \lfloor w\rfloor  \end{equation*}

Triangular lattices are a bit more complicated, and most easily defined piece-wise:

    \begin{equation*} \text{cookies}_{\blacktriangle} = \left\{ \begin{array}{ll} \lfloor l\rfloor n_{col}                             &  \text{if } l \bmod 1 \ge .5 \\ (2\lfloor l\rfloor-1)n_{col}/2                       &  \text{if } l \bmod 1 < .5, n_{col} \text{ even} \\ \lfloor l\rfloor + (2\lfloor l\rfloor-1)(n_{col}-1)/2 &  \text{if } l \bmod 1 < .5, n_{col} \text{ odd} \end{array} \right. \end{equation*}

where n_{col} = \lfloor 1+2(w-1)/\sqrt3 \rfloor is the number of columns that can fit in the width of the sheet.

(This is for a triangular lattice lined up with the length of the cookie sheet, rather the width — the one I dubbed “triangular 1.”)