How to resolve the algorithm Pascal's triangle/Puzzle step by step in the Haskell programming language
How to resolve the algorithm Pascal's triangle/Puzzle step by step in the Haskell programming language
Table of Contents
Problem Statement
This puzzle involves a Pascals Triangle, also known as a Pyramid of Numbers. Each brick of the pyramid is the sum of the two bricks situated below it. Of the three missing numbers at the base of the pyramid, the middle one is the sum of the other two (that is, Y = X + Z).
Write a program to find a solution to this puzzle.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pascal's triangle/Puzzle step by step in the Haskell programming language
This code is a constraint solver, written in Haskell, that can be used to solve systems of linear equations.
It is given a puzzle, that is a matrix of numbers and variables, and it returns the solution to the system of equations that is defined by the puzzle.
The code uses a technique called Gaussian elimination to solve the system of equations.
The first step is to convert the puzzle into a system of equations.
This is done by creating a list of equations, one for each row of the puzzle.
The equations are in the form of a list of tuples, where the first element is the constant term, and the remaining elements are pairs of variables and coefficients.
For example, the first row of the puzzle, ["151"]
, is converted into the equation [(0, 1:(-1):1:replicate n 0)]
, where n
is the number of columns in the puzzle.
Once the system of equations has been created, the code uses Gaussian elimination to solve the system. Gaussian elimination is a technique that uses row operations to transform the system of equations into an equivalent system that is easier to solve. The row operations that are used are:
- Swapping two rows
- Multiplying a row by a constant
- Adding a multiple of one row to another row
The code uses these row operations to transform the system of equations into an upper triangular matrix. An upper triangular matrix is a matrix where all the elements below the main diagonal are zero. Once the system of equations has been transformed into an upper triangular matrix, it is easy to solve the system. The solution is found by back-substitution, which starts with the last equation and solves for the variables in that equation. The values of the variables in the last equation are then used to solve for the variables in the second-to-last equation, and so on.
Once the system of equations has been solved, the code normalizes the solution. Normalization is the process of converting the solution into a form that is easier to read and understand. The code normalizes the solution by converting all the fractions in the solution into integers.
The code can be used to solve a variety of puzzles. For example, it can be used to solve Sudoku puzzles, crossword puzzles, and other types of puzzles.
Source code in the haskell programming language
puzzle = [["151"],["",""],["40","",""],["","","",""],["X","11","Y","4","Z"]]
triangle n = n * (n+1) `div` 2
coeff xys x = maybe 0 id $ lookup x xys
row n cs = [coeff cs k | k <- [1..n]]
eqXYZ n = [(0, 1:(-1):1:replicate n 0)]
eqPyramid n h = do
a <- [1..h-1]
x <- [triangle (a-1) + 1 .. triangle a]
let y = x+a
return $ (0, 0:0:0:row n [(x,-1),(y,1),(y+1,1)])
eqConst n fields = do
(k,s) <- zip [1..] fields
guard $ not $ null s
return $ case s of
"X" - (0, 1:0:0:row n [(k,-1)])
"Y" - (0, 0:1:0:row n [(k,-1)])
"Z" - (0, 0:0:1:row n [(k,-1)])
_ - (fromInteger $ read s, 0:0:0:row n [(k,1)])
equations :: [[String]] - ([Rational], [[Rational]])
equations puzzle = unzip eqs where
fields = concat puzzle
eqs = eqXYZ n ++ eqPyramid n h ++ eqConst n fields
h = length puzzle
n = length fields
normalize :: [Rational] - [Integer]
normalize xs = [numerator (x * v) | x <- xs] where
v = fromInteger $ foldr1 lcm $ map denominator $ xs
run puzzle = map (normalize . drop 3) $ answer where
(a, m) = equations puzzle
lr = decompose 0 m
answer = case solve 0 lr a of
Nothing - []
Just x - x : kernel lr
*Main run puzzle
[[151,81,70,40,41,29,16,24,17,12,5,11,13,4,8]]
*Main run [[""],["2",""],["X","Y","Z"]]
[[3,2,1,1,1,0],[3,0,3,-1,1,2]]
You may also check:How to resolve the algorithm Mad Libs step by step in the Wren programming language
You may also check:How to resolve the algorithm Validate International Securities Identification Number step by step in the SQL PL programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the REALbasic programming language
You may also check:How to resolve the algorithm Isqrt (integer square root) of X step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the UNIX Shell programming language