Skip to content
Snippets Groups Projects
Commit c06ddbb3 authored by penguinn's avatar penguinn
Browse files

Add Euler Problem

parent 0b3aa2c3
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Euler Problem 67 - Maximum Path Sum II
In the following we will give a solution for the Euler Problem 67. For convenience, the description of the problem can be found on the official site for the [Euler Problem 67](https://projecteuler.net/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.
```
3
7 4
2 4 6
8 5 9 3
```
That is, 3 + 7 + 4 + 9 = 23.
In this notebook we are going to find the maximum total from top to bottom in triangle.txt, a 15K text file containing a triangle with one-hundred rows.
Please note, that 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 299 altogether! 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.
%% Cell type:code id: tags:
``` prob
::load
MACHINE Euler_Problem_067
DEFINITIONS
"resources/triangle_Euler67.def";
SET_PREF_TIME_OUT == 5000
CONSTANTS n, Sol, OptimalSolution, TriangleC
PROPERTIES TriangleC = Triangle &
n = size (TriangleC) &
Sol : 1..n --> seq(INTEGER) &
Sol(n) = TriangleC(n) /* Initialisation */
&
! i. (i:1..(n-1) =>
(size(Sol(i)) = size(TriangleC(i))
& !j.(j: 1..size(TriangleC(i)) => Sol(i)(j) = TriangleC(i)(j)+max({Sol(i+1)(j),Sol(i+1)(j+1)}))
)
)
& OptimalSolution = Sol(1)(1)
ASSERTIONS
OptimalSolution = 7273;
n = 100
END
```
%% Output
Loaded machine: Euler_Problem_067
%% Cell type:markdown id: tags:
An interesting fact is the that dynamic programming is done automatically by ProB’s constraint solving.
For a better overview we are showing the optimal solution in this notebook with the `:eval` command.
```
ASSERTIONS
OptimalSolution = 7273;
n = 100
```
%% Cell type:code id: tags:
``` prob
:constants
```
%% Output
Machine constants set up using operation 0: $setup_constants()
%% Cell type:code id: tags:
``` prob
:init
```
%% Output
Machine initialised using operation 1: $initialise_machine()
%% Cell type:code id: tags:
``` prob
:eval OptimalSolution
```
%% Output
$7273$
7273
%% Cell type:code id: tags:
``` prob
:eval n
:check assertions
```
%% Output
$100$
100
|Predicate|Value|
|---|---|
|$\mathit{OptimalSolution} = 7273$|$\mathit{TRUE}$|
|$\mathit{n} = 100$|$\mathit{TRUE}$|
OptimalSolution = 7273 = TRUE
n = 100 = TRUE
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment