Project Euler is a website that provides a series of challenging mathematical and computational problems designed to encourage and develop problem-solving skills in mathematics and computer programming. The problems on the website range in difficulty from relatively easy to extremely difficult, and cover a wide range of topics including number theory, combinatorics, and cryptography. The site provides a platform for programmers and mathematicians to share their solutions and discuss problem-solving techniques, and is a popular resource for those interested in improving their mathematical and computational skills. From time to time I choose one problem and solve it.
Problem 67 on Project Euler is a variant of problem 18, which asks us to find the maximum total from top to bottom of a given triangle. Here’s the problem statement for problem 67:
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
Copy code3 7 4 2 4 6 8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom in the triangle below:
markdownCopy code75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 32 37 16 94 29 53 71 44 65 25 43 91 52 97 51 14 70 11 33 28 77 73 17 78 39 68 17 57 91 71 52 38 17 14 91 43 58 50 27 29 48 63 66 04 68 89 53 67 30 73 16 69 87 40 31 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
As with problem 18, the approach to solving this problem involves a dynamic programming algorithm that computes the maximum total for each cell in the triangle based on the maximum totals of the cells above it. Here’s one possible solution in C#:
public static long MaxPathSum(string triangle)
{
string[] rows = triangle.Split('\n');
int n = rows.Length;
long[][] memo = new long[n][];
for (int i = 0; i < n; i++)
{
string[] nums = rows[i].Split();
memo[i] = new long[nums.Length];
for (int j = 0; j < nums.Length; j++)
{
memo[i][j] = Int64.Parse(nums[j]);
}
}
for (int i = n - 2; i >= 0; i--)
{
for (int j = 0; j <= i; j++)
{
memo[i][j] += Math.Max(memo[i + 1][j], memo[i + 1][j + 1]);
}
}
return memo[0][0];
}
In this solution, we first split the input triangle string into rows and parse each row into an array of integers. We then create a 2D array memo
to store the maximum total for each cell in the triangle.
We then iterate over the rows of the triangle from bottom to top, computing the maximum total for each cell based on the maximum totals of the cells below it. Specifically, for each cell (i,j)
in the current row, we add the maximum of the two adjacent cells in the row below (i+1,j)
and (i+1,j+1)
to the value of the current cell.
Once we have computed the maximum total for the top cell of the triangle, we return it as the solution to the problem.
To test our solution, let’s write some unit tests using NUnit:
[TestFixture]
public class Problem67Tests
{
[Test]
public void TestMaxPathSumWithExampleTriangle()
{
string triangle = "3\n7 4\n2 4 6\n8 5 9 3";
long result = Problem67.MaxPathSum(triangle);
Assert.AreEqual(23, result);
}
[Test]
public void TestMaxPathSumWithLargeTriangle()
{
string triangle = "75\n95 64\n17 47 82\n18 35 87 10\n20 04 82 47 65\n19 01 23 75 03 34\n88 02 77 73 07 63 67\n99 65 04 28 06 16 70 92\n41 41 26 56 83 40 80 70 33\n41 48 72 33 47 32 37 16 94 29\n53 71 44 65 25 43 91 52 97 51 14\n70 11 33 28 77 73 17 78 39 68 17 57\n91 71 52 38 17 14 91 43 58 50 27 29 48\n63 66 04 68 89 53 67 30 73 16 69 87 40 31\n04 62 98 27 23 09 70 98 73 93 38 53 60 04 23";
long result = Problem67.MaxPathSum(triangle);
Assert.AreEqual(7273, result);
}
}
In these tests, we check the output of the MaxPathSum
function for two different input triangles: a small example triangle, and a large triangle taken from the problem statement. The expected output for the first test is 23, and the expected output for the second test is 7273.
When we run these tests, they should both pass if our implementation is correct.
Overall, this problem is a great exercise in dynamic programming and algorithm design. With a little bit of programming, we can efficiently compute the maximum total for a given triangle and find the solution to a challenging mathematical problem.