(Try out my JavaScript implementation here.)
The triangle path
Lets say you have a triangle lattice of numbers. Starting at the top of the triangle and moving to adjacent numbers on the row below, you want to add up the largest numbers along the path. Question: What is the maximum possible sum? For the following example, this maximum possible sum occurs on the path highlighted in bold:
5
9 6
4 6 8
0 7 1 5
The sum of the path is: 5 + 9 + 6 + 7 = 27.
The puzzle
Write a program that can determine the maximum possible sum for any triangle (lattice of numbers). The brute-force method would be to add up all the possible paths, and then choose the largest sum. For the example triangle above, this requires summing 8 paths:
5 + 9 + 4 + 0 = 18
5 + 9 + 4 + 7 = 25
5 + 9 + 6 + 7 = 27
5 + 9 + 6 + 1 = 21
5 + 6 + 6 + 7 = 24
5 + 6 + 6 + 1 = 18
5 + 6 + 8 + 1 = 20
5 + 6 + 8 + 5 = 24
These triangle lattices have the following properties:
2 rows:
a11
a21 a22
# of elements: 3
# of paths: 2
3 rows:
a11
a21 a22
a31 a32 a33
# of elements: 6
# of paths: 4
4 rows:
a11
a21 a22
a31 a32 a33
a41 a42 a43 a44
# of elements: 10
# of paths: 8
General properties
In general, the properties of the triangle lattices can be summarized in the following table:

(For triangles with n rows, the formula for the # of elements and # of paths can be proved using induction.)
Brute force unfeasibility
As n gets large, the number of possible paths blows up. For example, a triangle with 100 rows has 299 = 6.338253 × 1029
possible paths! To put this very large number in perspective, consider that:
- the average human body is comprised of around 7 × 1027 atoms
- there are an estimated 5 × 1030 bacterial cells in the world
A better perspective is to estimate how long it would take to actually calculate the sum of each of the 299 paths in a triangle lattice of 100 rows. The number of floating point addition operations would be 100 × 299 = 6.338253 × 1031. (Since each path has 100 numbers.)
Most contemporary personal computers have computational speeds in the range of gigaFLOPS. (109 floating point operations per second.) Hence for a personal computer it would take roughly 1031/109 seconds = 1022 seconds = 3.16887646 × 1014 years to apply the brute force method on a triangle with 100 rows.
Even using the current world’s fastest computer (the Japanese K computer) with computational speeds in the petaFLOP range (1015 floating point operations per second), the problem would take roughly 1031/1015 seconds = 1016 seconds = 316,887,646 years.
My solution
My first approach to this problem began with a misunderstanding. I initially interpreted the problem incorrectly, which I didn’t realize until I ended up solving a variation to this problem, that I now call the Myopic Traveler variant (see below), where the “myopia” both qualitatively describes the variant and also signifies my own shortsightedness on the complexity of the original triangle problem.
So after figuring out that I am suppose to find the maximum sum out of all possible triangle paths (not just the small subset available to the “myopic traveler”), I began by trying to find patterns. After a fruitless attempt to find patterns on the indices of the triangle lattice, I realized that since I just needed to find the maximum possible sum, information about each and every possible triangle path was redundant, as large portions of the lattice points (especially in the middle of the triangle) are traversed over and over again. However, information about the maximum sum is implicitly embedded in each lattice point, regardless of the individual paths.
Knowing this, I realized that if I were to look at the bottom row of the triangle first, and then determine the maximum amongst the n - 1 pairs of numbers in the nth row, add that maximum to each of the n - 1 numbers in the immediate top (or previous) n - 1 row, and keep doing this iteratively until the top row, I will result in the maximum possible sum out of all of the possible triangle paths. So for the current triangle example, the algorithm looks like this:
Step 1: Determine the maximum out of each consecutive pairs on the bottom row.
5
9 6
4 6 8
0 7 1 5
Step 2: Add the maximum to the appropriate number in the row above, and then determine the maximum out of each consecutive pairs in this row.
5
9 6
11 13 13
0 7 1 5
Step 3: Repeat Step 2.
5
22 19
11 13 13
0 7 1 5
Step 4: Repeat Step 3. The maximum sum is the number at the apex.
27
22 19
11 13 13
0 7 1 5
JavaScript implementation
You can try out my JavaScript implementation of the general solution to the triangle puzzle. For a triangle with 100 rows, finding the maximum sum took less than a second.
The “Myopic Traveler” variant
The following is the variant to the triangle puzzle that I had mentioned earlier. Due to a misunderstanding on my part, this variant is the result of my misinterpretation of the original triangle puzzle. My initial shortsightedness on the complexity of the original triangle problem, along with the apropos nature of this variant, led me to name the variant the “Myopic Traveler”.
Suppose you are a traveler on this triangle lattice, and starting at the top, you can only see the next row (hence the myopia). Then what is the maximum sum that you will end up with?
5
9 6
4 6 8
0 7 1 5
For this example triangle, the sum of the path of the myopic traveler is: 5 + 9 + 6 + 7 = 27. It is a coincidence that this path also happens to have the maximum sum out of all possible paths of this triangle.
The traveler’s myopia is really apparent in this example triangle (where the number 5 in the last row is changed to 50). You can see the maximum possible sum is not the myopic traveler’s path 5 + 9 + 6 + 7 = 27, but the following highlighted one:
5
9 6
4 6 8
0 7 1 50
The sum of the path is: 5 + 6 + 8 + 50 = 69. Because the myopic traveler can only see immediate row, this traveler will never have come across the number 50.
Twin path restriction
There is however one restriction I have to add on the possible triangle lattices: there cannot be twin numbers along the path. Otherwise there does not exist a unique maximum sum. For example the following triangle has two “maximum” sums, depending on which twin number you choose for along your path:
Twin path 1:
5
9 6
6 6 8
0 7 1 5
Choosing the 6 on the right results in the sum: 5 + 9 + 6 + 7 = 27.
Twin path 2:
5
9 6
6 6 8
0 7 1 5
Choosing the 6 on the left results in the sum: 5 + 9 + 6 + 0 = 20
“Mypoic Traveler” JavaScript implementation
You can try out my JavaScript implementation of the general solution to the Myopic Traveler variant.