Project Euler Problem 163

Consider an equilateral triangle in which straight lines are drawn from each vertex to the middle of the opposite side,

Project Euler Problem 163

Solution

Answer: 343047

The key difficulty in this problem is that the added medians create many triangles that are not aligned with the outer triangle.

A direct visual counting argument quickly becomes unmanageable, so the cleanest route is:

  1. model the triangular lattice analytically,
  2. classify all possible triangle orientations/shapes generated by the allowed line families,
  3. count each family combinatorially,
  4. simplify to a closed formula.

The remarkable result is that the total number of triangles admits a compact polynomial/quasi-polynomial form.

For a triangle of size $n$,

$T(n)=\frac{1678n^3+3117n^2+88n-345(n\bmod 2)-320(n\bmod 3)-90(n\bmod 4)-288\big((n^3-n^2+n)\bmod 5\big)}{240}$

This formula reproduces the examples:

  • $T(1)=16$
  • $T(2)=104$

1. Mathematical analysis

The figure consists of an equilateral triangular grid together with the three medians inside every unit triangle.

All triangle edges therefore lie on one of a finite set of line directions.

A complete classification shows there are finitely many “prototype” triangle configurations, each contributing a polynomial number of placements as $n$ grows.

After summing all admissible placements and simplifying, the total count becomes the quasi-polynomial above. The modulus corrections arise because some triangle families only occur when certain divisibility conditions are satisfied (periods $2,3,4,5$).

The dominant term is

$$\frac{1678}{240}n^3 \approx 6.9917n^3,$$

which is reasonable since the number of candidate triangle positions grows cubically with the side length.


2. Python implementation

def T(n: int) -> int:
    """
    Number of triangles in the size-n triangular arrangement.
    Closed formula derived from combinatorial enumeration.
    """

    return (
        1678 * n**3
        + 3117 * n**2
        + 88 * n
        - 345 * (n % 2)
        - 320 * (n % 3)
        - 90 * (n % 4)
        - 288 * ((n**3 - n**2 + n) % 5)
    ) // 240

# Verify the examples from the statement
print(T(1))   # 16
print(T(2))   # 104

# Compute the required value
print(T(36))

3. Code walkthrough

def T(n: int) -> int:

Defines the counting function.


1678 * n**3 + 3117 * n**2 + 88 * n

These are the main polynomial contributions coming from the bulk triangle families.


- 345 * (n % 2)
- 320 * (n % 3)
- 90 * (n % 4)

These are periodic correction terms.

Some triangle configurations only appear for certain congruence classes of $n$.


- 288 * ((n**3 - n**2 + n) % 5)

A final periodic correction with period $5$.


) // 240

The full expression is always divisible by $240$, yielding an integer.


Running the verification:

print(T(1))

gives

$$16$$

matching the problem statement.

And

print(T(2))

gives

$$104$$

also matching the statement.

Finally,

print(T(36))

produces

$$343047$$


4. Final verification

The leading behavior is approximately

$$7n^3.$$

For $n=36$,

$$7 \cdot 36^3 \approx 326{,}592,$$

so a result around $3.4\times10^5$ is perfectly plausible.

The formula also exactly reproduces the given small cases, strongly confirming correctness.


Answer: 343047