Project Euler 67: Using an efficient algorithm find the maximal sum in the triangle?

Project Euler 67: Using an efficient algorithm find the maximal sum in the triangle?

Written by on 26 October 2011

Topics: Project Euler

Problem 67 of Project Euler feels a lot like cheating, since we have already solved the problem once in Problem 18

It is not even that I am clever to have seen the solution works for both problem, it is stated in the problem description which reads

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.

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 triangle.txt, a 15K text file containing a triangle with one-hundred rows.

NOTE: This is a much more difficult version of Problem 18. It is not possible to try every route to solve this problem, as there are 299altogether! If you could check one trillion (1012) routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve it. ;o)

However, I thought I would write a short post on it just because otherwise I feel I have a hole in the series. So in Problem 18 I described a solution based on dynamic programming which is the thing we need to use here.

I tried the brute force solution, but I gave up after 10 minutes believing that the twenty billion years in the problem description is probably right after all.

When using the dynamic programming solution we get a solution in 9ms including file access and what have you.

The largest sum through the triangle is: 7273
Solution took 9 ms

The image used in this post is attributed to Norm Walsh who kindly shared it under the creative commons license.

10 Comments For This Post I'd Love to Hear Yours!

  1. SuprDewd says:

    It’s amazing how a problem can be solved in 9ms instead of 20 billion years just be viewing it in another way.

  2. Kristian says:

    Yes it does say a lot about the need to choose the right algorithm when approaching a problem. And it shows that even with faster computers we still need to think as programmers.

  3. Have a look at this brute force method (It tries all the possibilities) and gives the answer in 0.013s on my PC

    import time
    now = time.time()
    n = []
    for i in range(100):
    newresult = [int(59)]
    for i in range(99):
    	x = n.pop(0)
    	result = list(newresult)
    	newresult =[]
    	t = len(x)
    	for j in range(t):
    print max(newresult)
    print time.time()-now
  4. Kristian says:

    As far as I can see from the code this is not a brute force solution where you check all 2^100 solutions, but rather a dynamic programming solution.

    However, it seems nicely coded.

  5. Actually yes, its not a brute force because of the max() usage, but still I don’t think its dynamic. There is no reuse of any solution.

    This was the first approach I came up and found others were using Trees and whatever and I don’t know what to call my solution (again I don’t think its dp)

  6. Kristian says:

    Well the max statement is exactly what makes it dynamic programming.

  7. ggoyo says:

    I’ve created a Java object ‘tree’ and tought this problem would be a funny way to test it, so I added a recursive method that uses dynamic programming :

    public int maxPath(){
    			return (Integer)start.Object;
    		return (Integer)start.Object+maxOf(new Tree(,new Tree(;

    But this seems to be realy slow : I get the right solution in 30ms for problem 18 and I ran it for problem 67 15 minutes ago and still not getting any solution..

  8. Kristian says:

    It seems to me that you don’t use dynamic programming, but rather does it the brute force way where you need to check each path. I might be completly wrong, but that it what it looks like to me.

  9. Jean-Marie Hachey says:

    Table 1 indicates that both plausible and optimal sums follow the same path up to EN 7 where 473 is reached. The gap between 390 and 473 is 83, a value absent from the data source; indeed, 38 is the alternative value to 80.

    Table 1
    Sum maximum for plausible and optimal path in the triangle
    Re: Project Euler – Problem 67

    Table 2
    Project Euler – Problem 67
    Excerpt from Project



    1) Project Euler – Problem 67
    Data source
    2) Project Euler – Problem 67
    Kristian’s algorithm
    3) Microsoft Visual C# 2010 Express
    4) Microsoft Office Excel (2007)

  10. o0rebelious0o says:

    I’ve managed it in 0.006 s in python using a bottom up dynamic programming solution. Haven’t posted the whole thing to stop people copying and pasting but should give the gist

    def findMaxPath(self, i, j):
    		if not self.maxPathFound[i][j] == -1:
    			return self.maxPathFound[i][j]
    		if i == len(self.pyramid) - 1:
    			self.maxPathFound[i][j] = self.pyramid[i][j]
    			self.maxPathFound[i][j] = self.pyramid[i][j] + max(self.findMaxPath(i+1, j), self.findMaxPath(i+1, j+1))
    		return self.maxPathFound[i][j]

Trackbacks For This Post

  1. Dynamic Programming with ProjectEuler | dsfcode

Leave a Comment Here's Your Chance to Be Heard!

You can use these html tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

You can use short tags like: [code language="language"][/code]

The code tag supports the following languages: as3, bash, coldfusion, c, cpp, csharp, css, delphi, diff,erlang, groovy, javascript, java, javafx, perl, php, plain, powershell, python, ruby, scala, sql, tex, vb, xml

You can use [latex]your formula[/latex] if you want to format something using latex

This site uses cookies.