Pascal's triangle/Puzzle: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(40 intermediate revisions by 19 users not shown)
Line 17:
Write a program to find a solution to this puzzle.
<br><br>
 
=={{header|11l}}==
{{trans|D}}
 
<syntaxhighlight lang="11l">F e(&x, row, col) -> &
R x[row * (row + 1) I/ 2 + col]
 
F iterate(&v, &diff, do_print = 1B)
V tot = 0.0
L
e(&v, 0, 0) = 151
e(&v, 2, 0) = 40
e(&v, 4, 1) = 11
e(&v, 4, 3) = 4
 
L(i) 1..4
L(j) 0..i
e(&diff, i, j) = 0
I j < i
e(&diff, i, j) += e(&v, i - 1, j) - e(&v, i, j + 1) - e(&v, i, j)
I j != 0
e(&diff, i, j) += e(&v, i - 1, j - 1) - e(&v, i, j - 1) - e(&v, i, j)
 
L(i) 1..3
L(j) 0.<i
e(&diff, i, j) += e(&v, i + 1, j) + e(&v, i + 1, j + 1) - e(&v, i, j)
 
e(&diff, 4, 2) += e(&v, 4, 0) + e(&v, 4, 4) - e(&v, 4, 2)
 
L(i) 0 .< v.len
v[i] += diff[i] / 4
 
tot = sum(diff.map(a -> a * a))
I do_print
print(‘dev: ’tot)
I tot < 0.1
L.break
 
V v = [0.0] * 15
V diff = [0.0] * 15
iterate(&v, &diff)
 
V idx = 0
L(i) 5
L(j) 0..i
print(‘#4’.format(Int(0.5 + v[idx])), end' I j < i {‘ ’} E "\n")
idx++</syntaxhighlight>
 
{{out}}
<pre>
dev: 73410
dev: 17968.6875
dev: 6388.46484375
dev: 2883.337402344
dev: 1446.593643188
...
dev: 0.136503592
dev: 0.125866452
dev: 0.116055273
dev: 0.107006115
dev: 0.098659977
151
81 70
40 41 29
16 24 17 12
5 11 13 4 8
</pre>
 
=={{header|Ada}}==
The solution makes an upward run symbolically, though excluding Z. After that two blocks (1,1) and (3,1) being known yield a 2x2 linear system, from which X and Y are determined. Finally each block is revisited and printed.
<langsyntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
procedure Pyramid_of_Numbers is
Line 88 ⟶ 155:
end loop;
end loop;
end Pyramid_of_Numbers;</langsyntaxhighlight>
{{Out}}
<pre>
Line 98 ⟶ 165:
5 11 13 4 8
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Standard - ''lu decomp'' and ''lu solve'' are from the [[ALGOL 68G]]/gsl library}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
<!-- {{does not work with|ELLA ALGOL 68|Any (with appropriate job cards AND formatted transput statements removed) - tested with release 1.8.8d.fc9.i386 - ELLA has no FORMATted transput, and not }} -->
<langsyntaxhighlight lang="algol68">MODE
FIELD = REAL,
VEC = [0]REAL,
Line 197 ⟶ 265:
FOR var FROM 1 BY 2 TO 5 DO
printf(($5x$,$g$,puzzle[UPB puzzle][var],"=", real repr, solution[UPB puzzle][var]))
OD</langsyntaxhighlight>
{{Out}}
<pre>
Line 210 ⟶ 278:
=={{header|AutoHotkey}}==
The main part is this:
<langsyntaxhighlight lang="autohotkey">N1 := 11, N2 := 4, N3 := 40, N4 := 151
Z := (2*N4 - 7*N3 - 8*N2 + 6*N1) / 7
X := (N3 - 2*N1 - Z) / 2
MsgBox,, Pascal's Triangle, %X%`n%Z%</langsyntaxhighlight>
Message box shows:
<pre>5.000000
Line 220 ⟶ 288:
 
The GUI shows all values in the solved state.
<langsyntaxhighlight lang="autohotkey">;---------------------------------------------------------------------------
; Pascal's triangle.ahk
; by wolf_II
Line 493 ⟶ 561:
; delete status bar text
Else SB_SetText("")
}</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$ + "ARRAYLIB"
REM Describe the puzzle as a set of simultaneous equations:
Line 535 ⟶ 603:
PRINT "X = " ; vector(8)
PRINT "Y = " ; vector(9)
PRINT "Z = " ; vector(10)</langsyntaxhighlight>
{{Out}}
<pre>
Line 543 ⟶ 611:
</pre>
 
=={{header|C#}}==
This solution is based upon algebraic necessities, namely that a solution exists when (top - 4(a+b))/7 is integral. It also highlights the type difference between floating point numbers and integers in C.
<lang c sharp>
 
<syntaxhighlight lang="c">
/* Pascal's pyramid solver
*
* [top]
* [ ] [ ]
* [mid] [ ] [ ]
* [ ] [ ] [ ] [ ]
* [ x ] [ a ] [ y ] [ b ] [ z ]
* x + z = y
*
* This solution makes use of a little bit of mathematical observation,
* such as the fact that top = 4(a+b) + 7(x+z) and mid = 2x + 2a + z.
*/
 
#include <stdio.h>
#include <math.h>
 
void pascal(int a, int b, int mid, int top, int* x, int* y, int* z)
{
double ytemp = (top - 4 * (a + b)) / 7.;
if(fmod(ytemp, 1.) >= 0.0001)
{
x = 0;
return;
}
*y = ytemp;
*x = mid - 2 * a - *y;
*z = *y - *x;
}
int main()
{
int a = 11, b = 4, mid = 40, top = 151;
int x, y, z;
pascal(a, b, mid, top, &x, &y, &z);
if(x != 0)
printf("x: %d, y: %d, z: %d\n", x, y, z);
else printf("No solution\n");
 
return 0;
}
</syntaxhighlight>
{{Out}}
<pre>
x: 5, y: 13, z: 8
</pre>
 
 
 
===Field equation solver===
Treating relations between cells as if they were differential equations, and apply negative feedback to each cell at every iteration step. This is how field equations with boundary conditions are solved numerically. It is, of course, not the optimal solution for this particular task.
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
void show(int *x) {
int i, j;
 
for (i = 0; i < 5; i++)
for (j = 0; j <= i; j++)
printf("%4d%c", *(x++), j < i ? ' ' : '\n');
}
 
inline int sign(int i)
{
return i < 0 ? -1 : i > 0;
}
 
int iter(int *v, int *diff) {
int sum, i, j, e = 0;
 
# define E(x, row, col) x[(row) * ((row) + 1) / 2 + (col)]
/* enforce boundary conditions */
E(v, 0, 0) = 151;
E(v, 2, 0) = 40;
E(v, 4, 1) = 11;
E(v, 4, 3) = 4;
 
/* calculate difference from equilibrium */
for (i = 1; i < 5; i++) {
for (j = 0; j <= i; j++) {
E(diff, i, j) = 0;
if (j < i)
E(diff, i, j) += E(v, i - 1, j) -
E(v, i, j + 1) -
E(v, i, j);
if (j)
E(diff, i, j) += E(v, i - 1, j - 1) -
E(v, i, j - 1) -
E(v, i, j);
}
}
 
for (i = 0; i < 4; i++)
for (j = 0; j < i; j++)
E(diff, i, j) += E(v, i + 1, j) +
E(v, i + 1, j + 1) -
E(v, i, j);
 
E(diff, 4, 2) += E(v, 4, 0) + E(v, 4, 4) - E(v, 4, 2);
# undef E
 
/* Do feedback, check if we are done. */
for (i = sum = 0; i < 15; i++) {
sum += !!sign(e = diff[i]);
 
/* 1/5-ish feedback strength on average. These numbers are highly
magical, depending on nodes' connectivities. */
if (e >= 4 || e <= -4) v[i] += e/5;
else if (rand() < RAND_MAX/4) v[i] += sign(e);
}
return sum;
}
 
int main() {
int v[15] = { 0 }, diff[15] = { 0 }, i, s;
 
for (i = s = 1; s; i++) {
s = iter(v, diff);
printf("pass %d: %d\n", i, s);
}
show(v);
 
return 0;
}</syntaxhighlight>
{{Out}}<pre>pass 1: 12
pass 2: 12
pass 3: 14
pass 4: 14
...
pass 113: 4
pass 114: 7
pass 115: 0
151
81 70
40 41 29
16 24 17 12
5 11 13 4 8</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="c sharp">
using System;
 
Line 731 ⟶ 939:
}
}
</syntaxhighlight>
</lang>
{{out| Program Input and Output}}
<pre>
Line 798 ⟶ 1,006:
</pre>
 
=={{header|C++}}==
{{trans|C}}
This solution is based upon algebraic necessities, namely that a solution exists when (top - 4(a+b))/7 is integral. It also highlights the type difference between floating point numbers and integers in C.
<syntaxhighlight lang="cpp">#include <iostream>
#include <iomanip>
 
inline int sign(int i) {
<lang c>
return i < 0 ? -1 : i > 0;
/* Pascal's pyramid solver
*
* [top]
* [ ] [ ]
* [mid] [ ] [ ]
* [ ] [ ] [ ] [ ]
* [ x ] [ a ] [ y ] [ b ] [ z ]
* x + z = y
*
* This solution makes use of a little bit of mathematical observation,
* such as the fact that top = 4(a+b) + 7(x+z) and mid = 2x + 2a + z.
*/
 
#include <stdio.h>
#include <math.h>
 
void pascal(int a, int b, int mid, int top, int* x, int* y, int* z)
{
double ytemp = (top - 4 * (a + b)) / 7.;
if(fmod(ytemp, 1.) >= 0.0001)
{
x = 0;
return;
}
*y = ytemp;
*x = mid - 2 * a - *y;
*z = *y - *x;
}
int main()
{
int a = 11, b = 4, mid = 40, top = 151;
int x, y, z;
pascal(a, b, mid, top, &x, &y, &z);
if(x != 0)
printf("x: %d, y: %d, z: %d\n", x, y, z);
else printf("No solution\n");
 
inline int& E(int *x, int row, int col) {
return 0;
return x[row * (row + 1) / 2 + col];
}
</lang>
{{Out}}
<pre>
x: 5, y: 13, z: 8
</pre>
 
int iter(int *v, int *diff) {
// enforce boundary conditions
E(v, 0, 0) = 151;
E(v, 2, 0) = 40;
E(v, 4, 1) = 11;
E(v, 4, 3) = 4;
 
// calculate difference from equilibrium
for (auto i = 1u; i < 5u; i++)
for (auto j = 0u; j <= i; j++) {
E(diff, i, j) = 0;
if (j < i)
E(diff, i, j) += E(v, i - 1, j) - E(v, i, j + 1) - E(v, i, j);
if (j)
E(diff, i, j) += E(v, i - 1, j - 1) - E(v, i, j - 1) - E(v, i, j);
}
 
for (auto i = 0u; i < 4u; i++)
===Field equation solver===
for (auto j = 0u; j < i; j++)
Treating relations between cells as if they were differential equations, and apply negative feedback to each cell at every iteration step. This is how field equations with boundary conditions are solved numerically. It is, of course, not the optimal solution for this particular task.
E(diff, i, j) += E(v, i + 1, j) + E(v, i + 1, j + 1) - E(v, i, j);
<lang c>#include <stdio.h>
#include <stdlib.h>
 
E(diff, 4, 2) += E(v, 4, 0) + E(v, 4, 4) - E(v, 4, 2);
void show(int *x) {
int i, j;
 
// do feedback, check if we are done
for (i = 0; i < 5; i++)
for (j = 0; juint <= isum; j++)
int e = 0;
printf("%4d%c", *(x++), j < i ? ' ' : '\n');
for (auto i = sum = 0u; i < 15u; i++) {
}
sum += !!sign(e = diff[i]);
 
// 1/5-ish feedback strength on average. These numbers are highly magical, depending on nodes' connectivities
inline int sign(int i)
if (e >= 4 || e <= -4)
{
return i < 0 ? -1 : v[i] += e >/ 05;
else if (rand() < RAND_MAX / 4)
v[i] += sign(e);
}
return sum;
}
 
intvoid itershow(int *v, int *diffx) {
for (auto i = 0u; i < 5u; i++)
int sum, i, j, e = 0;
for (auto j = 0u; j <= i; j++)
 
std::cout << std::setw(4u) << *(x++) << (j < i ? ' ' : '\n');
# define E(x, row, col) x[(row) * ((row) + 1) / 2 + (col)]
/* enforce boundary conditions */
E(v, 0, 0) = 151;
E(v, 2, 0) = 40;
E(v, 4, 1) = 11;
E(v, 4, 3) = 4;
 
/* calculate difference from equilibrium */
for (i = 1; i < 5; i++) {
for (j = 0; j <= i; j++) {
E(diff, i, j) = 0;
if (j < i)
E(diff, i, j) += E(v, i - 1, j) -
E(v, i, j + 1) -
E(v, i, j);
if (j)
E(diff, i, j) += E(v, i - 1, j - 1) -
E(v, i, j - 1) -
E(v, i, j);
}
}
 
for (i = 0; i < 4; i++)
for (j = 0; j < i; j++)
E(diff, i, j) += E(v, i + 1, j) +
E(v, i + 1, j + 1) -
E(v, i, j);
 
E(diff, 4, 2) += E(v, 4, 0) + E(v, 4, 4) - E(v, 4, 2);
# undef E
 
/* Do feedback, check if we are done. */
for (i = sum = 0; i < 15; i++) {
sum += !!sign(e = diff[i]);
 
/* 1/5-ish feedback strength on average. These numbers are highly
magical, depending on nodes' connectivities. */
if (e >= 4 || e <= -4) v[i] += e/5;
else if (rand() < RAND_MAX/4) v[i] += sign(e);
}
return sum;
}
 
int main() {
int v[15] = { 0 }, diff[15] = { 0 }, i, s;
for (auto i = 1u, s = 1u; s; i++) {
s = iter(v, diff);
std::cout << "pass " << i << ": " << s << std::endl;
}
show(v);
 
return 0;
for (i = s = 1; s; i++) {
}</syntaxhighlight>
s = iter(v, diff);
printf("pass %d: %d\n", i, s);
}
show(v);
 
return 0;
}</lang>
{{Out}}<pre>pass 1: 12
pass 2: 12
pass 3: 14
pass 4: 14
...
pass 113: 4
pass 114: 7
pass 115: 0
151
81 70
40 41 29
16 24 17 12
5 11 13 4 8</pre>
 
=={{header|Clojure}}==
Line 942 ⟶ 1,078:
X and Z are the independent variables, so first work bottom up and determine the value of each cell in the form (n0 + n1*X + n2*Z).
We'll use a vector [n0 n1 n2] to represent each cell.
<langsyntaxhighlight lang="clojure">(def bottom [ [0 1 0], [11 0 0], [0 1 1], [4 0 0], [0 0 1] ])
 
(defn plus [v1 v2] (vec (map + v1 v2)))
Line 950 ⟶ 1,086:
(defn above [row] (map #(apply plus %) (partition 2 1 row)))
 
(def rows (reverse (take 5 (iterate above bottom))))</langsyntaxhighlight>
We know the integer value of cells c00 and c20 ( base-0 row then column numbers), so by subtracting these values we get two equations of the form 0=n0+n1*X+n2*Z.
<langsyntaxhighlight lang="clojure">(def c00 (get-in rows [0 0]))
(def c20 (get-in rows [2 0]))
 
(def eqn0 (minus c00 [151 0 0]))
(def eqn1 (minus c20 [ 40 0 0]))</langsyntaxhighlight>
In this case, there are only two variables, so solving the system of linear equations is simple.
<langsyntaxhighlight lang="clojure">(defn solve [m]
(assert (<= 1 m 2))
(let [n (- 3 m)
Line 968 ⟶ 1,104:
 
(let [x (solve 1), z (solve 2), y (+ x z)]
(println "x =" x ", y =" y ", z =" z))</langsyntaxhighlight>
If you want to solve the whole pyramid, just add a call ''(show-pyramid x z)'' to the previous ''let'' form:
<langsyntaxhighlight lang="clojure">(defn dot [v1 v2] (reduce + (map * v1 v2)))
 
(defn show-pyramid [x z]
(doseq [row rows]
(println (map #(dot [1 x z] %) row)))</langsyntaxhighlight>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">let x = -1
 
do
 
let x = x + 1
let z = 0
 
do
 
let e = x + 11
let f = 11 + (x + z)
let g = (x + z) + 4
let h = 4 + z
 
if e + f = 40 then
 
let c = f + g
let d = g + h
let a = 40 + c
let b = c + d
let q = 0
 
if a + b = 151 then
 
let q = 1
 
endif
 
endif
 
if q = 0 then
 
let z = z + 1
 
endif
 
wait
 
loopwhile z < 20 and q = 0
 
if q = 0 then
 
let z = -1
 
endif
 
wait
 
loopuntil z >= 0
 
print "x = ", x
print "y = ", x + z
print "z = ", z</syntaxhighlight>
{{out| Output}}<pre>x = 5
y = 13
z = 8</pre>
 
=={{header|Curry}}==
{{Works with|PAKCS}}
<langsyntaxhighlight lang="curry">import CLPFD
import Constraint (allC, andC)
import Findall (findall)
Line 1,002 ⟶ 1,196:
, [ x, 11, y, 4, z]
]
main = findall $ solve . test</langsyntaxhighlight>
{{Out}}
<pre>Execution time: 0 msec. / elapsed: 0 msec.
Line 1,009 ⟶ 1,203:
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm;
 
void iterate(bool doPrint=true)(double[] v, double[] diff) @safe {
Line 1,069 ⟶ 1,263:
iterate(v, diff);
show(v);
}</langsyntaxhighlight>
{{out}}
<pre>dev: 73410
Line 1,092 ⟶ 1,286:
=={{header|F_Sharp|F#}}==
<p>In a script, using the [http://numerics.mathdotnet.com/ Math.NET Numerics] library</p>
<langsyntaxhighlight lang="fsharp">
#load"Packages\MathNet.Numerics.FSharp\MathNet.Numerics.fsx"
 
Line 1,115 ⟶ 1,309:
let x = A.Solve(b)
 
printfn "x = %f, Y = %f, Z = %f" x.[8] x.[9] x.[10]</langsyntaxhighlight>
{{out}}
<pre>x = 5.000000, Y = 13.000000, Z = 8.000000</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
<syntaxhighlight lang="factor">USING: arrays backtrack combinators.extras fry grouping.extras
interpolate io kernel math math.ranges sequences ;
 
: base ( ?x ?z -- seq ) 2dup + swap '[ _ 11 _ 4 _ ] >array ;
 
: up ( seq -- seq' ) [ [ + ] 2clump-map ] twice ;
 
: find-solution ( -- x z )
10 [1,b] dup [ amb-lazy ] bi@ 2dup base
up dup first 40 = must-be-true
up first 151 = must-be-true ;
 
find-solution [I X = ${1}, Z = ${}I] nl</syntaxhighlight>
{{out}}
<pre>
X = 5, Z = 8
</pre>
 
=={{header|FreeBASIC}}==
{{trans|PureBasic}}
<syntaxhighlight lang="freebasic">Function SolveForZ(x As Integer) As Integer
Dim As Integer a, b, c, d, e, f, g, h, z
For z = 0 To 20
e = x + 11
f = 11 + (x+z)
g = (x+z) + 4
h = 4 + z
If e + f = 40 Then
c = f + g
d = g + h
a = 40 + c
b = c + d
If a + b = 151 Then Return z
End If
Next z
Return -1
End Function
 
Dim As Integer x = -1, z = 0
Do
x = x + 1
z = SolveForZ(x)
Loop Until z >= 0
 
Print "X ="; x
Print "Y ="; x + z
Print "Z ="; z
Sleep
</syntaxhighlight>
{{out}}
<pre>X = 5
Y = 13
Z = 8</pre>
 
 
=={{header|Go}}==
This solution follows the way the problem might be solved with pencil and paper. It shows a possible data representation of the problem, uses the computer to do some arithmetic, and displays intermediate and final results.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,254 ⟶ 1,505:
fmt.Println("y =", solveY(y))
fmt.Println("z =", solveZ(z))
}</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,277 ⟶ 1,528:
I assume the task is to solve any such puzzle, i.e. given some data
 
<langsyntaxhighlight lang="haskell">puzzle = [["151"],["",""],["40","",""],["","","",""],["X","11","Y","4","Z"]]</langsyntaxhighlight>
 
one should calculate all possible values that fit. That just means solving a linear system of equations. We use the first three variables as placeholders for ''X'', ''Y'' and ''Z''. Then we can produce the matrix of equations:
 
<langsyntaxhighlight lang="haskell">triangle n = n * (n+1) `div` 2
 
coeff xys x = maybe 0 id $ lookup x xys
Line 1,309 ⟶ 1,560:
eqs = eqXYZ n ++ eqPyramid n h ++ eqConst n fields
h = length puzzle
n = length fields</langsyntaxhighlight>
 
To solve the system, any linear algebra library will do (e.g [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/hmatrix-0.2.0.0 hmatrix]). For this example, we assume there are functions ''decompose'' for LR-decomposition, ''kernel'' to solve the homogenous system and ''solve'' to find a special solution for an imhomogenous system. Then
 
<langsyntaxhighlight lang="haskell">normalize :: [Rational] - [Integer]
normalize xs = [numerator (x * v) | x <- xs] where
v = fromInteger $ foldr1 lcm $ map denominator $ xs
Line 1,322 ⟶ 1,573:
answer = case solve 0 lr a of
Nothing - []
Just x - x : kernel lr</langsyntaxhighlight>
 
will output one special solution and modifications that lead to more solutions, as in
 
<langsyntaxhighlight lang="haskell">*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]]</langsyntaxhighlight>
 
so for the second puzzle, not only X=1 Y=1 Z=0 is a solution,
Line 1,340 ⟶ 1,591:
Fixed points in the pyramid are 40 and 151, which I use to check a resulting pyramid for selection:
 
<langsyntaxhighlight lang="j">chk=:40 151&-:@(2 4{{."1)</langsyntaxhighlight>
 
verb for the base of the pyramid:
 
<langsyntaxhighlight lang="j">base=: [,11,+,4,]</langsyntaxhighlight>
 
the height of the pyramid:
 
<langsyntaxhighlight lang="j">ord=:5</langsyntaxhighlight>
 
=> 'chk', 'base' and 'ord' are the knowledge rules abstracted from the problem definition.
Line 1,354 ⟶ 1,605:
The J-sentence that solves the puzzle is:
 
<langsyntaxhighlight lang="j"> |."2(#~chk"2) 2(+/\)^:(<ord)"1 base/"1>,{ ;~i:28</langsyntaxhighlight>
<pre> 151 0 0 0 0
81 70 0 0 0
Line 1,363 ⟶ 1,614:
Get rid of zeros:
<langsyntaxhighlight lang="j">,.(1+i.5)<@{."0 1{.|."2(#~chk"2) 2(+/\)^:(<ord)"1 base/"1>,{ ;~i:28</langsyntaxhighlight>
or
<langsyntaxhighlight lang="j">,.(<@{."0 1~1+i.@#){.|."2(#~chk"2) 2(+/\)^:(<ord)"1 base/"1>,{ ;~i:28</langsyntaxhighlight>
 
<pre> +-----------+
Line 1,378 ⟶ 1,629:
|5 11 13 4 8|
+-----------+</pre>
 
=={{header|Java}}==
 
Generate 11 equations and 11 unknowns. Reuse code from Cramer's Rule.
 
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class PascalsTrianglePuzzle {
 
public static void main(String[] args) {
Matrix mat = new Matrix(Arrays.asList(1d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, -1d, 0d, 0d),
Arrays.asList(0d, 1d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, -1d, 0d),
Arrays.asList(0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, -1d, 1d, -1d),
Arrays.asList(0d, 0d, 1d, 0d, 0d, 0d, 0d, 0d, 0d, -1d, 0d),
Arrays.asList(0d, 0d, 0d, 1d, 0d, 0d, 0d, 0d, 0d, 0d, -1d),
Arrays.asList(1d, 1d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d),
Arrays.asList(0d, 1d, 1d, 0d, -1d, 0d, 0d, 0d, 0d, 0d, 0d),
Arrays.asList(0d, 0d, 1d, 1d, 0d, -1d, 0d, 0d, 0d, 0d, 0d),
Arrays.asList(0d, 0d, 0d, 0d, -1d, 0d, 1d, 0d, 0d, 0d, 0d),
Arrays.asList(0d, 0d, 0d, 0d, 1d, 1d, 0d, -1d, 0d, 0d, 0d),
Arrays.asList(0d, 0d, 0d, 0d, 0d, 0d, 1d, 1d, 0d, 0d, 0d));
List<Double> b = Arrays.asList(11d, 11d, 0d, 4d, 4d, 40d, 0d, 0d, 40d, 0d, 151d);
List<Double> solution = cramersRule(mat, b);
System.out.println("Solution = " + cramersRule(mat, b));
System.out.printf("X = %.2f%n", solution.get(8));
System.out.printf("Y = %.2f%n", solution.get(9));
System.out.printf("Z = %.2f%n", solution.get(10));
}
private static List<Double> cramersRule(Matrix matrix, List<Double> b) {
double denominator = matrix.determinant();
List<Double> result = new ArrayList<>();
for ( int i = 0 ; i < b.size() ; i++ ) {
result.add(matrix.replaceColumn(b, i).determinant() / denominator);
}
return result;
}
private static class Matrix {
private List<List<Double>> matrix;
@Override
public String toString() {
return matrix.toString();
}
@SafeVarargs
public Matrix(List<Double> ... lists) {
matrix = new ArrayList<>();
for ( List<Double> list : lists) {
matrix.add(list);
}
}
public Matrix(List<List<Double>> mat) {
matrix = mat;
}
public double determinant() {
if ( matrix.size() == 1 ) {
return get(0, 0);
}
if ( matrix.size() == 2 ) {
return get(0, 0) * get(1, 1) - get(0, 1) * get(1, 0);
}
double sum = 0;
double sign = 1;
for ( int i = 0 ; i < matrix.size() ; i++ ) {
sum += sign * get(0, i) * coFactor(0, i).determinant();
sign *= -1;
}
return sum;
}
private Matrix coFactor(int row, int col) {
List<List<Double>> mat = new ArrayList<>();
for ( int i = 0 ; i < matrix.size() ; i++ ) {
if ( i == row ) {
continue;
}
List<Double> list = new ArrayList<>();
for ( int j = 0 ; j < matrix.size() ; j++ ) {
if ( j == col ) {
continue;
}
list.add(get(i, j));
}
mat.add(list);
}
return new Matrix(mat);
}
 
private Matrix replaceColumn(List<Double> b, int column) {
List<List<Double>> mat = new ArrayList<>();
for ( int row = 0 ; row < matrix.size() ; row++ ) {
List<Double> list = new ArrayList<>();
for ( int col = 0 ; col < matrix.size() ; col++ ) {
double value = get(row, col);
if ( col == column ) {
value = b.get(row);
}
list.add(value);
}
mat.add(list);
}
return new Matrix(mat);
}
 
private double get(int row, int col) {
return matrix.get(row).get(col);
}
}
 
}
</syntaxhighlight>
 
{{out}}
<pre>
Solution = [16.0, 24.0, 17.0, 12.0, 41.0, 29.0, 81.0, 70.0, 5.0, 13.0, 8.0]
X = 5.00
Y = 13.00
Z = 8.00
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
 
In the spirit of the simplicity of Pascal Triangle's definition, the
first solution given in this entry works up from the bottom of the triangle using the
basic Pascal Triangle equation for each brick that is above two
others.
 
In the following, the value of the j-th brick in the i-th row is
denoted by rij if initially known (so r11 refers to the apex), and by
$Rij if initially unknown, so the variable "X" in the puzzle
could be denoted by $R51.
 
It is assumed that:
* all values in the triangle must be positive integers;
* the task is to solve for any given values of r11, r31, r52, r54;
* all solutions for the triple [X, Y, Z] should be found.
<syntaxhighlight lang=jq>
def solve(r11; r31; r52; r54):
range(1;r31 - 1) as $X
| range(1; r31 - 1) as $Y
| (($Y - $X) | select(. > 0)) as $Z
| (r52 + $X) as $R41
| (r52 + $Y) as $R42
| select($R41 + $R42 == r31)
| ($Y + r54) as $R43
| (r54 + $Z) as $R44
| ($R42 + $R43) as $R32
| ($R43 + $R44) as $R33
| (r31 + $R32) as $R21
| ($R32 + $R33) as $R22
| select($R21 + $R22 == r11)
| [$X, $Y, $Z];
solve(151; 40; 11; 4),
</syntaxhighlight>
{{output}}
<pre>
[5,13,8]
</pre>
===Algebraic solution===
As noted elsewhere on this page, elementary considerations show that
the apex (top) value and the value in the third row (mid) can be written as:
<pre>
top = 4(a+b) + 7(x+z)
mid = 2x + 2a + z
</pre>
where a and b are the known values at the base.
 
Using the jq program at [[Cramer%27s_rule#jq|Cramer's rule]], with the unknown vector being [x, z]:
<syntaxhighlight lang=jq>
include "rc-cramers-rule";
def solve(top; mid; a; b):
cramer(
[ [7, 7],
[2, 1]];
[top - 4*(a+b), mid-2*a]);
 
solve(151; 40; 11; 4)
</syntaxhighlight>
This gives the solution for [x,z] as:
{{output}}
<pre>
[5,8]
</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Kotlin}}
<syntaxhighlight lang="julia">function pascal(a::Integer, b::Integer, mid::Integer, top::Integer)
 
<lang julia>function pascal(a::Integer, b::Integer, mid::Integer, top::Integer)
yd = round((top - 4 * (a + b)) / 7)
!isinteger(yd) && return 0, 0, 0
Line 1,396 ⟶ 1,840:
else
println("There is no solution.")
end</langsyntaxhighlight>
 
{{out}}
Line 1,403 ⟶ 1,847:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
data class Solution(val x: Int, val y: Int, val z: Int)
Line 1,424 ⟶ 1,868:
else
println("There is no solutuon")
}</langsyntaxhighlight>
 
{{out}}
Line 1,431 ⟶ 1,875:
</pre>
 
=={{header|MathematicaMaple}}==
<syntaxhighlight lang="maple">
sys := {22 + x + y = 40, 78 + 5*y + z = 151, x + z = y}:
solve(sys, {x,y,z});
</syntaxhighlight>
{{out}}<pre>
{x = 5, y = 13, z = 8}
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
We assign a variable to each block starting on top with a, then on the second row b,c et cetera. k,m, and o are replaced by X, Y, and Z. We can write the following equations:
<langsyntaxhighlight Mathematicalang="mathematica">b+c==a
d+e==b
e+f==c
Line 1,443 ⟶ 1,896:
n+Y==i
n+Z==j
X+Z==Y</langsyntaxhighlight>
And we have the knowns
<langsyntaxhighlight Mathematicalang="mathematica">a->151
d->40
l->11
n->4</langsyntaxhighlight>
Giving us 10 equations with 10 unknowns; i.e. solvable. So we can do so by:
<langsyntaxhighlight Mathematicalang="mathematica">eqs={a==b+c,d+e==b,e+f==c,g+h==d,h+i==e,i+j==f,l+X==g,l+Y==h,n+Y==i,n+Z==j,Y==X+Z};
knowns={a->151,d->40,l->11,n->4};
Solve[eqs/.knowns,{b,c,e,f,g,h,i,j,X,Y,Z}]</langsyntaxhighlight>
gives back:
<langsyntaxhighlight Mathematicalang="mathematica">{{b -> 81, c -> 70, e -> 41, f -> 29, g -> 16, h -> 24, i -> 17, j -> 12, X -> 5, Y -> 13, Z -> 8}}</langsyntaxhighlight>
In pyramid form that would be:
<langsyntaxhighlight Mathematicalang="mathematica"> 151
81 70
40 41 29
16 24 17 12
5 11 13 4 8</langsyntaxhighlight>
 
An alternative solution in Mathematica 10, constructing the triangle:
<langsyntaxhighlight Mathematicalang="mathematica">triangle[n_, m_] := Nest[MovingMap[Total, #, 1] &, {x, 11, y, 4, z}, n - 1][[m]]
Solve[{triangle[3, 1] == 40, triangle[5, 1] == 151, y == x + z}, {x, y, z}]</langsyntaxhighlight>
Three equations and three unknowns, which gives back:
<langsyntaxhighlight Mathematicalang="mathematica">{{x -> 5, y -> 13, z -> 8}}</langsyntaxhighlight>
 
=={{header|MiniZinc}}==
<syntaxhighlight lang="minizinc">
%Pascal's Triangle Puzzle. Nigel Galloway, February 17th., 2020
int: N11=151; constraint N11=N21+N22;
var 1..N11: N21=N31+N32;
var 1..N11: N22=N32+N33;
int: N31=40; constraint N31=N41+N42;
var 1..N11: N32=N42+N43;
var 1..N11: N33=N43+N44;
var 1..N11: N41=X+11;
var 1..N11: N42=Y+11;
var 1..N11: N43=Y+4;
var 1..N11: N44=Z+4;
var 1..N11: X;
var 1..N11: Y=X+Z;
var 1..N11: Z;
</syntaxhighlight>
{{out}}
<pre>
Z = 8;
X = 5;
----------
</pre>
 
=={{header|Nim}}==
{{trans|Ada}}
Translation of Ada solution:
<syntaxhighlight lang ="nim">import math, strutils
var B_X, B_Y, B_Z : int = 0
type
Block_Value = object
Known : int
X, Y, Z : int
 
type
let
X: Block_Value = Block_Value(Known:0, X:1, Y:0, Z:0)
Y: Block_Value = Block_Value(Known:0, X:0, Y:1, Z:0)
Z: Block_Value = Block_Value(Known:0, X:0, Y:0, Z:1)
proc Add (L : var Block_Value, R : Block_Value) =
# Symbolically adds one block to another
L.Known = L.Known + R.Known
L.X = L.X + R.X - R.Z # Z is excluded as n(Y - X - Z) = 0
L.Y = L.Y + R.Y + R.Z
 
BlockValue = object
proc Add (L: var Block_Value, R: int) =
known: int
# Symbolically adds a value to the block
L.Known =x, L.Knowny, +z: Rint
proc Image (N : Block_Value): string =
# The block value, when X,Y,Z are known
result = $(N.Known + N.X * B_X + N.Y * B_Y + N.Z * B_Z)
proc Solve_2x2 (A11: int, A12:int, B1:int, A21:int, A22:int, B2: int) =
# Don't care about things, supposing an integer solution exists
if A22 == 0:
B_X = toInt(B2 / A21)
B_Y = toInt((B1 - (A11*B_X)) / A12)
else:
B_X = toInt((B1*A22 - B2*A12) / (A11*A22 - A21*A12))
B_Y = toInt((B1 - A11*B_X) / A12)
B_Z = B_Y - B_X
var B : array [1..5, array[1..5, Block_Value]] # The lower triangle contains blocks
 
Variables = tuple[x, y, z: int]
# The bottom blocks
 
Add(B[5][1],X)
func `+=`(left: var BlockValue; right: BlockValue) =
Add(B[5][2],11)
## Symbolically add one block to another.
Add(B[5][3],Y)
left.known += right.known
Add(B[5][4],4)
left.x += right.x - right.z # Z is excluded as n(Y - X - Z) = 0.
Add(B[5][5],Z)
left.y += right.y + right.z
 
# Upward run
proc toString(n: BlockValue; vars: Variables): string =
for Row in countdown(4,1):
## Return the representation of the block value, when X, Y, Z are known.
for Column in 1 .. Row:
result = $(n.known + n.x * vars.x + n.y * vars.y + n.z * vars.z)
Add (B[Row][Column], B[Row + 1][Column])
 
Add (B[Row][Column], B[Row + 1][Column + 1])
proc Solve2x2(a11, a12, b1, a21, a22, b2: int): Variables =
## Solve a puzzle, supposing an integer solution exists.
# Now have known blocks 40=[3][1], 151=[1][1] and Y=X+Z to determine X,Y,Z
if a22 == 0:
Solve_2x2( B[1][1].X,
result.x = b2 div B[1][1].Y, a21
result.y = (b1 - a11 * result.x) div a12
151 - B[1][1].Known,
else:
B[3][1].X,
result.x = (b1 * a22 - b2 B[3][1].Y,* a12) div (a11 * a22 - a21 * a12)
result.y = (b1 - a11 * 40 - B[3][1]result.Knownx) div a12
result.z = result.y - result.x
 
#Print the results
var blocks : array[1..5, array[1..5, BlockValue]] # The lower triangle contains blocks.
for Row in 1..5:
 
writeln(stdout,"")
# The bottom blocks.
for Column in 1..Row:
blocks[5][1] = BlockValue(x: 1)
write(stdout, Image(B[Row][Column]), " ")</lang>
blocks[5][2] = BlockValue(known: 11)
blocks[5][3] = BlockValue(y: 1)
blocks[5][4] = BlockValue(known: 4)
blocks[5][5] = BlockValue(z: 1)
 
# Upward run.
for row in countdown(4, 1):
for column in 1..row:
blocks[row][column] += blocks[row + 1][column]
blocks[row][column] += blocks[row + 1][column + 1]
 
# Now have known blocks 40=[3][1], 151=[1][1] and Y=X+Z to determine X,Y,Z.
let vars = Solve2x2(blocks[1][1].x,
blocks[1][1].y,
151 - blocks[1][1].known,
blocks[3][1].x,
blocks[3][1].y,
40 - blocks[3][1].known)
 
# Print the results.
for row in 1..5:
var line = ""
for column in 1..row:
line.addSep(" ")
line.add toString(blocks[row][column], vars)
echo line</syntaxhighlight>
{{out}}
<pre>151
81 70
40 41 29
16 24 17 12
5 11 13 4 8</pre>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">%% to compile : ozc -x <file.oz>
functor
 
Line 1,612 ⟶ 2,083:
 
{Application.exit 0}
end</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
[ 6y+x+z+4a[2]+4a[4]= 7y +4a[2]+4a[4]]
Line 1,621 ⟶ 2,093:
 
this helped me...
<langsyntaxhighlight lang="parigp">
Pascals_triangle_puzzle(topvalue=151,leftsidevalue=40,bottomvalue1=11,bottomvalue2=4) = {
y=(topvalue-(4*(bottomvalue1+bottomvalue2)))/7;
Line 1,627 ⟶ 2,099:
z=y-x;
print(x","y","z); }
</syntaxhighlight>
</lang>
 
I'm thinking of one to solve all puzzles regardless of size and positions. but the objective was to solve this puzzle.
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl"># set up triangle
my $rows = 5;
my @tri = map { [ map { {x=>0,z=>0,v=>0,rhs=>undef} } 1..$_ ] } 1..$rows;
Line 1,671 ⟶ 2,144:
my $y = $x+$z;
printf "x=%d, y=%d, z=%d\n", $x, $y, $z;
</syntaxhighlight>
</lang>
{{out}}
<pre>Equations:
Line 1,680 ⟶ 2,153:
x=5, y=13, z=8
</pre>
=={{header|Perl 6}}==
{{trans|Perl}}
<lang perl6># set up triangle
my $rows = 5;
my @tri = (1..$rows).map: { [ { x => 0, z => 0, v => 0, rhs => Nil } xx $_ ] }
@tri[0][0]<rhs> = 151;
@tri[2][0]<rhs> = 40;
@tri[4][0]<x> = 1;
@tri[4][1]<v> = 11;
@tri[4][2]<x> = 1;
@tri[4][2]<z> = 1;
@tri[4][3]<v> = 4;
@tri[4][4]<z> = 1;
# aggregate from bottom to top
for @tri - 2 ... 0 -> $row {
for 0 ..^ @tri[$row] -> $col {
@tri[$row][$col]{$_} = @tri[$row+1][$col]{$_} + @tri[$row+1][$col+1]{$_} for 'x','z','v';
}
}
 
# find equations
my @eqn = gather for @tri -> $row {
for @$row -> $cell {
take [ $cell<x>, $cell<z>, $cell<rhs> - $cell<v> ] if defined $cell<rhs>;
}
}
 
# print equations
say "Equations:";
say " x + z = y";
for @eqn -> [$x,$z,$y] { say "$x x + $z z = $y" }
 
# solve
my $f = @eqn[0][1] / @eqn[1][1];
@eqn[0][$_] -= $f * @eqn[1][$_] for 0..2;
$f = @eqn[1][0] / @eqn[0][0];
@eqn[1][$_] -= $f * @eqn[0][$_] for 0..2;
 
# print solution
say "Solution:";
my $x = @eqn[0][2] / @eqn[0][0];
my $z = @eqn[1][2] / @eqn[1][1];
my $y = $x + $z;
say "x=$x, y=$y, z=$z";</lang>
{{out}}
<pre>Equations:
x + z = y
7 x + 7 z = 91
2 x + 1 z = 18
Solution:
x=5, y=13, z=8</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
I approached this with a view to solving general pyramid puzzles, not just the one given.
<span style="color: #000080;font-style:italic;">--
<lang Phix>--This little ditty converts the pyramid to rules quite nicely, however I will concede
-- demo\rosetta\Pascal_triangle_Puzzle.exw
--that solving those two rules (18=2x+z and 73=5x+6z) and specifically converting them
-- =======================================
--into xrule(35=7x) and zrule(56=7z) is somewhat amateurish - suggestions welcome.
--
 
-- I approached this with a view to solving general pyramid puzzles, not just the one given.
sequence pyramid = {
--
{151},
-- This little ditty converts the pyramid to rules quite nicely, then uses a modified copy
{"",""},
-- of solveN() from [[Solving_coin_problems#Phix]] to solve those simultaneous equations.
{40,"",""},
--</span>
{"","","",""},
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
{"x",11,"y",4,"z"}}
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">pyramid</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">151</span><span style="color: #0000FF;">},</span>
sequence rules = {}
<span style="color: #0000FF;">{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">},</span>
 
<span style="color: #0000FF;">{</span><span style="color: #000000;">40</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">},</span>
-- each cell in the pyramid is either an integer final value or an equation.
<span style="color: #0000FF;">{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">},</span>
-- initially the equations are strings, we substitute all with triplets of
<span style="color: #0000FF;">{</span><span style="color: #008000;">"x"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"y"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"z"</span><span style="color: #0000FF;">}}</span>
-- the form {k,x,z} ie k+l*x+m*z, and known values < last row become rules.
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rules</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
for r=5 to 1 by -1 do
for c=1 to length(pyramid[r]) do
<span style="color: #000080;font-style:italic;">-- each cell in the pyramid is either an integer final value or an equation.
object prc = pyramid[r][c], equ
-- initially the equations are strings, we substitute all with triplets of
if prc="x" then prc = {0,1,0} -- ie one x
-- the form {k,x,z} ie k+l*x+m*z, and known values &lt; last row become rules.</span>
elsif prc="y" then prc = {0,1,1} -- ie one x plus one z
elsif prc="z" then prc = {0,0,1} -- ie one z
<span style="color: #008080;">for</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">=</span><span style="color: #000000;">5</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
else
<span style="color: #008080;">for</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pyramid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">do</span>
if prc="" or r<=4 then
<span style="color: #004080;">object</span> <span style="color: #000000;">prc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pyramid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">equ</span>
-- examples: x+11 is {0,1,0}+{11,0,0} -> {11,1,0},
<span style="color: #008080;">if</span> <span style="color: #000000;">prc</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"x"</span> <span style="color: #008080;">then</span> <span style="color: #000000;">prc</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- ie 0 + one x</span>
-- 11+y is {11,0,0}+{0,1,1} -> {11,1,1},
<span style="color: #008080;">elsif</span> <span style="color: #000000;">prc</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"y"</span> <span style="color: #008080;">then</span> <span style="color: #000000;">prc</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- ie 0 + one x plus one z</span>
-- 40=""+"" is {40,0,0}={22,2,1} ==> {18,2,1}
<span style="color: #008080;">elsif</span> <span style="color: #000000;">prc</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"z"</span> <span style="color: #008080;">then</span> <span style="color: #000000;">prc</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- ie 0 + one z</span>
equ = sq_add(pyramid[r+1][c],pyramid[r+1][c+1])
<span style="color: end if#008080;">else</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">prc</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span> <span style="color: #008080;">or</span> <span style="color: #000000;">r</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">4</span> <span style="color: #008080;">then</span>
if prc="" then prc = equ
else <span style="color: #000080;font-style:italic;">-- examples: x+11 is {0,1,0}+{11,0,0} prc =-&gt; {prc11,01,0},
-- if11+y r<=4is then{11,0,0}+{0,1,1} -&gt; {11,1,1},
-- 40=""+"" is equ[{40,0,0}={22,2,1]} ==&gt; prc[1]-equ[{18,2,1]}</span>
<span style="color: #000000;">equ</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pyramid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">],</span><span style="color: #000000;">pyramid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
rules = append(rules,equ)
<span style="color: #008080;">end</span> <span style="color: end #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">prc</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span> <span style="color: #000000;">prc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">equ</span>
end if
<span style="color: #008080;">else</span> <span style="color: #000000;">prc</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">prc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">r</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">4</span> <span style="color: #008080;">then</span>
pyramid[r][c] = prc
<span style="color: #000000;">equ</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">prc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">equ</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
end for
<span style="color: #000000;">rules</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">,</span><span style="color: #000000;">equ</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
ppOpt({pp_Nest,1,pp_StrFmt,1})
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
?"equations"
<span style="color: #000000;">pyramid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">prc</span>
pp(pyramid)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
?"rules"
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
pp(rules)
puts(1,"=====\n")
<span style="color: #7060A8;">ppOpt</span><span style="color: #0000FF;">({</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">pp_StrFmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #004600;">pp_IntCh</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">})</span>
 
<span style="color: #0000FF;">?</span><span style="color: #008000;">"equations"</span>
if length(rules)!=2 then ?9/0 end if -- more work needed!?
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pyramid</span><span style="color: #0000FF;">)</span>
 
<span style="color: #0000FF;">?</span><span style="color: #008000;">"rules"</span>
-- admittedly this bit is rather amateurish, and maybe problem-specific:
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {18,2,1} === 18=2x+z
sequence xrule = sq_sub(sq_mul(rules[1],rules[2][3]),sq_mul(rules[2],rules[1][3])),
-- {73,5,6} === 73=5x+6z</span>
zrule = sq_sub(sq_mul(rules[2],rules[1][2]),sq_mul(rules[1],rules[2][2]))
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"=====\n"</span><span style="color: #0000FF;">)</span>
 
?{"xrule",xrule}
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- more work needed!?
?{"zrule",zrule}
 
-- modified copy of solveN() from Solving_coin_problems.exw as promised, a
integer x = xrule[1]/xrule[2],
-- bit of a sledgehammer to crack a peanut is the phrase you are looking for:</span>
z = zrule[1]/zrule[3],
<span style="color: #008080;">function</span> <span style="color: #000000;">solveN</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">)</span>
y = x+z
<span style="color: #000080;font-style:italic;">--
 
-- Based on https://mathcs.clarku.edu/~djoyce/ma105/simultaneous.html
printf(1,"x = %d, y=%d, z=%d\n",{x,y,z})
-- aka the ancient Chinese Jiuzhang suanshu ~100 B.C. (!!)
 
--
-- finally evaluate all the equations and print it.
-- Example (not related to the task problem):
for r=1 to length(pyramid) do
-- rules = <nowiki>{{</nowiki>18,1,1},{38,1,5<nowiki>}}</nowiki>, ie 18==x+y, 38==x+5y
for c=1 to length(pyramid[r]) do
-- integer==&gt; {k13, l5}, m}ie x=13, pyramid[r][c]y=5
--
pyramid[r][c] = k+l*x+m*z
-- In the elimination phase, both x have multipliers of 1, ie both rii and rij are 1,
end for
-- so we can ignore the two sq_mul and just do [sq_sub] (38=x+5y)-(18=x+y)==&gt;(20=4y).
end for
-- Obviously therefore y is 5 and substituting backwards x is 13.
 
--
pp(pyramid)</lang>
-- Example2 (taken from the task problem):
-- rules = <nowiki>{{</nowiki>18,2,1},{73,5,6<nowiki>}}</nowiki>, ie 18==2x+z, 73==5x+6z
-- ==&gt; <nowiki>{{</nowiki>18,2,1},{56,0,7<nowiki>}}</nowiki>, ie rules[2]:=rules[2]*2-rules[1]*5 (eliminate)
-- ==&gt; <nowiki>{{</nowiki>18,2,1},8}, ie rules[2]:=56/7, aka z:=8 (substitute)
-- ==&gt; <nowiki>{{</nowiki>10,2,0},8}, ie rules[1]-=1z (substitute)
-- ==&gt; {5,8}, ie rules[1]:=10/2, aka x:=5 (substitute)
-- ==&gt; {5,8}, ie x=5, z=8
-- </span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ri</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rj</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">rii</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rji</span>
<span style="color: #000000;">rules</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
<span style="color: #000080;font-style:italic;">-- successively eliminate (grow lower left triangle of 0s)</span>
<span style="color: #000000;">ri</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">l</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">rii</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ri</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rii</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (see note below)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">rj</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">rji</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rji</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rj</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rj</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rii</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rji</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]==</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (job done)</span>
<span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rj</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000080;font-style:italic;">-- then substitute each backwards</span>
<span style="color: #000000;">ri</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">rii</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ri</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]/</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- (all else should be 0)</span>
<span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rii</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">rj</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">rji</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rji</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">rj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">rji</span><span style="color: #0000FF;">*</span><span style="color: #000000;">rii</span>
<span style="color: #000000;">rj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rj</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">rules</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #000080;font-style:italic;">-- Obviously these next two lines directly embody knowledge from the task, and
-- would need changing for an even slightly different version of the problem:</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">solveN</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">z</span> <span style="color: #000080;font-style:italic;">-- (as per task desc)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"x=%d, y=%d, z=%d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">})</span>
<span style="color: #000080;font-style:italic;">-- finally evaluate all the equations and print it.</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pyramid</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pyramid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pyramid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">pyramid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">m</span><span style="color: #0000FF;">*</span><span style="color: #000000;">z</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pyramid</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,820 ⟶ 2,304:
{73,5,6}}
=====
x=5, y=13, z=8
{"xrule",{35,7,0}}
{"zrule",{56,0,7}}
x = 5, y=13, z=8
{{151},
{81,70},
Line 1,829 ⟶ 2,311:
{5,11,13,4,8}}
</pre>
Interestingly, this appears to match Python in that 40 is propagated up the tree, whereas Perl and Go appear to propagate 18=22+2x+z up, not that I can think of any case where that would make a difference.
 
A couple of other ways to use that solveN():
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #0000FF;">?</span><span style="color: #000000;">solveN</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">73</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}})</span> <span style="color: #000080;font-style:italic;">-- -- {5,13,8}</span>
<span style="color: #7060A8;">ppOpt</span><span style="color: #0000FF;">({</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span>
<span style="color: #000080;font-style:italic;">-- Or, hand-coding 11 simultaneous equations for 11 unknowns:
-- (be warned I had to reorder a bit to avoid rii==0 mishaps,
-- perhaps solveN() should find or even sort rules somehow)
--sequence pyramid = <nowiki>{{</nowiki>151},
-- {"a","b"},
-- {40,"c","d"},
-- {"e","f","g","h"},
-- {"x", 11,"y", 4,"z"<nowiki>}}</nowiki>
-- a b c d e f g h x y z</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">solveN</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">151</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- a+b=151</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">40</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- 40+c=a</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- c+d=b</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- g+h=d</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">40</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- e+f=40</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- f+g=c</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">11</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- x+11=e</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- y+4=g</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">11</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- 11+y=f</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- 4+z=h</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- x+z=y
-- })) -- {81,70,41,29,16,24,17,12,5,13,8}</span>
<span style="color: #0000FF;">})[-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #000080;font-style:italic;">-- {5,13,8}</span>
<!--</syntaxhighlight>-->
 
And finally, a cheeky little two-liner that does the whole job
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">Y</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">151</span><span style="color: #0000FF;">-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">*(</span><span style="color: #000000;">11</span><span style="color: #0000FF;">+</span><span style="color: #000000;">4</span><span style="color: #0000FF;">))/</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">X</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">40</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">11</span><span style="color: #0000FF;">-</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">Z</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Y</span><span style="color: #0000FF;">-</span><span style="color: #000000;">X</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Two-liner: x=%d, y=%d, z=%d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">X</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Z</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Two-liner: x=5, y=13, z=8
</pre>
 
=={{header|Picat}}==
Below are three different approaches using constraint modelling (cp solver).
 
===Using lists to represent the triangle===
{{trans|Prolog}}
<syntaxhighlight lang="picat">import cp.
 
go =>
puzzle(T, X, Y,Z),
foreach(TT in T)
println(TT)
end,
println([x=X,y=Y,z=Z]),
nl,
fail, % are there any more solutions?
nl.
% Port of the Prolog solution
puzzle(Ts, X, Y, Z) :-
Ts = [ [151],
[_, _],
[40, _, _],
[_, _, _, _],
[X, 11, Y, 4, Z]],
Y #= X + Z,
triangle(Ts),
Vs = vars(Ts),
Vs :: 0..10000,
solve(Vs).
triangle([T|Ts]) :-
( Ts = [N|_] -> triangle_(T, N), triangle(Ts) ; true ).
triangle_([], _).
triangle_([T|Ts],[A,B|Rest]) :-
T #= A + B, triangle_(Ts, [B|Rest]).</syntaxhighlight>
 
{{out}}
<pre>[151]
[81,70]
[40,41,29]
[16,24,17,12]
[5,11,13,4,8]
[x = 5,y = 13,z = 8]</pre>
 
===Calculating the positions===
Here is a constraint model which calculates the positions in the triangle that should be added together.
<syntaxhighlight lang="picat">import cp.
 
puzzle2 =>
N = 5, % number of rows
Len = (N*(N+1)) div 2, % number of entries
% The triangle numbers for 1..N
T = [I*(I+1) div 2 : I in 1..N],
 
% The index of first number to use in addition
% create the indices of the numbers to add,
% i.e. Adds[I] + Adds[I+1]
Adds = new_list(T[N-1]),
Adds[1] := 2,
foreach(I in 2..T[N-1])
% "jump" of 2 when i-1 is a triangle number
if membchk(I-1,T) then
Adds[I] := Adds[I-1] + 2
else
Adds[I] := Adds[I-1] + 1
end
end,
 
% the pyramid
MaxVal = 10_000,
L = new_list(Len),
L :: 1..MaxVal,
 
% The clues.
L = [ 151,
_, _,
40, _, _,
_, _,_ , _ ,
X, 11, Y, 4, Z
],
 
% The sums
foreach(I in 1..T[N-1])
L[I] #= L[Adds[I]]+L[Adds[I]+1]
end,
 
% The extra constraint
Y #= X + Z,
solve(L),
println([x=X,y=Y,z=Z]),
fail, % check if there is another solution
nl.</syntaxhighlight>
 
{{out}}
<pre>[x = 5,y = 13,z = 8]</pre>
 
===Hard coded constraints===
<syntaxhighlight lang="picat">import cp.
 
puzzle3 =>
% 1
% 2 3
% 4 5 6
% 7 8 9 10
%11 12 13 14 15
X = new_list(15),
X :: 0..10_000,
X[1] #= X[2]+X[3],
X[2] #= X[4]+X[5],
X[3] #= X[5]+X[6],
X[4] #= X[7]+X[8],
X[5] #= X[8]+X[9],
X[6] #= X[9]+X[10],
X[7] #= X[11]+X[12],
X[8] #= X[12]+X[13],
X[9] #= X[13]+X[14],
X[10] #= X[14]+X[15],
X[13] #= X[11] + X[15], % Y=X+Z,
 
% The hints
X[1] #= 151,
X[4] #= 40,
X[12] #= 11,
X[14] #= 4,
 
solve(X),
println([x=X[11],y=X[13],z=X[15]]),
fail,
nl.</syntaxhighlight>
 
{{out}}
<pre>[x = 5,y = 13,z = 8]</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(be number (@N @Max)
(^ @C (box 0))
(repeat)
Line 1,875 ⟶ 2,533:
(+ 4 @Z @H)
(+ @X @Z @Y)
T )</langsyntaxhighlight>
{{Out}}
<pre>: (? (puzzle @X @Y @Z))
Line 1,881 ⟶ 2,539:
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">:- use_module(library(clpfd)).
 
puzzle(Ts, X, Y, Z) :-
Line 1,899 ⟶ 2,557:
% X = 5,
% Y = 13,
% Z = 8 ;</langsyntaxhighlight>
 
=={{header|PureBasic}}==
Brute force solution.
<langsyntaxhighlight PureBasiclang="purebasic">; Known;
; A.
; [ 151]
Line 1,933 ⟶ 2,591:
z=SolveForZ(x)
Until z>=0
MessageRequester(title$,"X="+Str(x)+#CRLF$+"Y="+Str(x+z)+#CRLF$+"Z="+Str(z))</langsyntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|2.4+}}
<langsyntaxhighlight lang="python"># Pyramid solver
# [151]
# [ ] [ ]
Line 2,058 ⟶ 2,716:
p = [ [151], [None,None], [40,None,None], [None,None,None,None], ['X', 11, 'Y', 4, 'Z'] ]
addlConstraint = { 'X':1, 'Y':-1, 'Z':1, '1':0 }
SolvePyramid( p, addlConstraint)</langsyntaxhighlight>
{{Out}}
<pre>Constraint Equations:
Line 2,074 ⟶ 2,732:
http://www.fantascienza.net/leonardo/so/csp.zip
 
<langsyntaxhighlight lang="python">from csp import Problem
 
p = Problem()
Line 2,092 ⟶ 2,750:
p.addrule("Y == X + Z")
for sol in p.xsolutions():
print [sol[k] for k in "XYZ"]</langsyntaxhighlight>
 
{{Out}}
Line 2,103 ⟶ 2,761:
We'll use a struct (cell v x z) to represent each cell,
where the value is (v + x*X + z*Z).
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket/base
(require racket/list)
Line 2,118 ⟶ 2,776:
(- (cell-x cx) (cell-x cy))
(- (cell-z cx) (cell-z cy))))
</syntaxhighlight>
</lang>
 
We first work bottom up and determine the value of each cell, starting from the bottom row.
<syntaxhighlight lang="racket">
<lang Racket>
(define (row-above row) (map cell-add (drop row 1) (drop-right row 1)))
 
Line 2,129 ⟶ 2,787:
(define row3 (row-above row2))
(define row4 (row-above row3))
</syntaxhighlight>
</lang>
 
We know the value of two additional cells, so by subtracting these values we get two equations of the form 0=v+x*X+z*Z. In the usual notation we get x*X+z*Z=-v, so v has the wrong sign.
 
<syntaxhighlight lang="racket">
<lang Racket>
(define eqn40 (cell-sub (car row4) (cell 151 0 0)))
(define eqn20 (cell-sub (car row2) (cell 40 0 0)))
</syntaxhighlight>
</lang>
 
To solve the 2 equation system, we will use the Cramer's rule.
<syntaxhighlight lang="racket">
<lang Racket>
(define (det2 eqnx eqny get-one get-oth)
(- (* (get-one eqnx) (get-oth eqny)) (* (get-one eqny) (get-oth eqnx))))
Line 2,146 ⟶ 2,804:
(/ (det2 eqnx eqny get-val get-oth)
(det2 eqnx eqny get-unk get-oth)))
</syntaxhighlight>
</lang>
 
To get the correct values of X, Y and Z we must change their signs.
<syntaxhighlight lang="racket">
<lang Racket>
(define x (- (cramer2 eqn20 eqn40 cell-v cell-x cell-z)))
(define z (- (cramer2 eqn20 eqn40 cell-v cell-z cell-x)))
Line 2,156 ⟶ 2,814:
(displayln (list "Y" (+ x z)))
(displayln (list "Z" z))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 2,164 ⟶ 2,822:
(Z 8)
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" line># set up triangle
my $rows = 5;
my @tri = (1..$rows).map: { [ { x => 0, z => 0, v => 0, rhs => Nil } xx $_ ] }
@tri[0][0]<rhs> = 151;
@tri[2][0]<rhs> = 40;
@tri[4][0]<x> = 1;
@tri[4][1]<v> = 11;
@tri[4][2]<x> = 1;
@tri[4][2]<z> = 1;
@tri[4][3]<v> = 4;
@tri[4][4]<z> = 1;
# aggregate from bottom to top
for @tri - 2 ... 0 -> $row {
for 0 ..^ @tri[$row] -> $col {
@tri[$row][$col]{$_} = @tri[$row+1][$col]{$_} + @tri[$row+1][$col+1]{$_} for 'x','z','v';
}
}
 
# find equations
my @eqn = gather for @tri -> $row {
for @$row -> $cell {
take [ $cell<x>, $cell<z>, $cell<rhs> - $cell<v> ] if defined $cell<rhs>;
}
}
 
# print equations
say "Equations:";
say " x + z = y";
for @eqn -> [$x,$z,$y] { say "$x x + $z z = $y" }
 
# solve
my $f = @eqn[0][1] / @eqn[1][1];
@eqn[0][$_] -= $f * @eqn[1][$_] for 0..2;
$f = @eqn[1][0] / @eqn[0][0];
@eqn[1][$_] -= $f * @eqn[0][$_] for 0..2;
 
# print solution
say "Solution:";
my $x = @eqn[0][2] / @eqn[0][0];
my $z = @eqn[1][2] / @eqn[1][1];
my $y = $x + $z;
say "x=$x, y=$y, z=$z";</syntaxhighlight>
{{out}}
<pre>Equations:
x + z = y
7 x + 7 z = 91
2 x + 1 z = 18
Solution:
x=5, y=13, z=8</pre>
 
=={{header|REXX}}==
<langThis rexx>/*REXX programversion solvesalso displays a (Pascal's) "Pyramiddiagram of Numbers" the puzzle given four values. */
<syntaxhighlight lang="rexx">/*REXX program solves a (Pascal's) "Pyramid of Numbers" puzzle given four values. */
/*┌───────────────────────────────────────────────────────────┐
/* ╔══════════════════════════════════════════════════╗
│ answer │
answer mid / │
/ \ /
mid / \ 151 │
\ 151 \ ααα ααα │
\ ααα ααα 40 ααα ααα │
40 ααα ααα ααα ααα ααα
ααα ααα ααα ααα x 11 y 4 z │
x 11 y 4 z / \ │
/ \ / \
find: / \ / \ │
│Find: x y z b b d d
╚══════════════════════════════════════════════════╝ */
└───────────────────────────────────────────────────────────┘*/
do #=2; _= sourceLine(#); n= pos('_', _) /* [↓] this DO loop shows (above) box.*/
parse arg b d mid answer . /*obtain optional variables from the CL*/
if n\==0 then leave; say _ /*only display up to the above line. */
end /*#*/; say /* [↑] this is a way for in─line doc. */
parse arg b d mid answer . /*obtain optional variables from the CL*/
if b=='' | b=="," then b= 11 /*Not specified? Then use the default.*/
if d=='' | d=="," then d= 4 /* " " " " " " */
if mid='' | mid=="," then mid= 40 /* " " " " " " */
if answer='' | answer=="," then answer= 151 /* " " " " " " */
pad= left('', 15) big= answer - 4*b - 4*d /*usedcalculate for insertingBIG spaces innumber output.less constants*/
do x=-big to big
big= answer - 4*b - 4*d /*calculate big number less constants*/
do y=-big to big
middle= mid - 2*b /* " middle " " " */
if x+y\==mid - 2*b then iterate /*40 = x+2B+Y ──or── 40-2*11 = x+y */
 
do x =-big to big
do y=-big to big
if x+y\==middle then iterate /*40 = x+2B+Y ──or── 40-2*11 = x+y */
do z=-big to big
if z \== y - x then iterate /*zZ has to equal y Y-xX (yY=x X+zZ) */
if x+y*6+z == big then say pad right('x = ', n) x pad right("y = " ,n) y pad right('z = ' ,n) z
end /*z*/
end end /*y*/
end end /*x*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
/* ╔══════════════════════════════════════════════════╗
x = 5 y = 13 z = 8
║ answer ║
║ / ║
║ mid / ║
║ \ 151 ║
║ \ ααα ααα ║
║ 40 ααα ααα ║
║ ααα ααα ααα ααα ║
║ x 11 y 4 z ║
║ / \ ║
║ find: / \ ║
║ x y z b d ║
╚══════════════════════════════════════════════════╝ */
 
x = 5 y = 13 z = 8
</pre>
 
=={{header|Ruby}}==
uses [[Reduced row echelon form#Ruby]]
<langsyntaxhighlight lang="ruby">require 'rref'
 
pyramid = [
Line 2,267 ⟶ 2,993:
end
puts
answer.each{|row| p row}</langsyntaxhighlight>
 
{{out}}
Line 2,289 ⟶ 3,015:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object PascalTriangle extends App {
 
val (x, y, z) = pascal(11, 4, 40, 151)
Line 2,300 ⟶ 3,026:
 
println(if (x != 0) s"Solution is: x = $x, y = $y, z = $z" else "There is no solution.")
}</langsyntaxhighlight>
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/UJF14fw/0 (JavaScript)]
or by [https://scastie.scala-lang.org/l0AlwpSdR7i801Fq8CWHEQ Scastie (JVM)].
 
=={{header|Sidef}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="ruby"># set up triangle
var rows = 5
var tri = rows.of {|i| (i+1).of { Hash(x => 0, z => 0, v => 0, rhs => nil) } }
Line 2,351 ⟶ 3,078:
var z = (eqn[1][2] / eqn[1][1])
var y = (x + z)
say "x=#{x}, y=#{y}, z=#{z}"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,364 ⟶ 3,091:
=={{header|SystemVerilog}}==
We can view this as a problem of generating a set of random numbers that satisfy the constraints. Because there is only one solution, the result isn't very random...
<langsyntaxhighlight SystemVeriloglang="systemverilog">program main;
 
class Triangle;
Line 2,401 ⟶ 3,128:
 
Triangle answer = new;
endprogram</langsyntaxhighlight>
<pre> [151]
[81][70]
Line 2,411 ⟶ 3,138:
=={{header|Tcl}}==
using code from [[Reduced row echelon form#Tcl]]
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
namespace path ::tcl::mathop
 
Line 2,478 ⟶ 3,205:
lappend solved $newrow
}
print_matrix $solved</langsyntaxhighlight>
<pre>x=5.0
y=13.0
Line 2,487 ⟶ 3,214:
16.0 24.0 17.0 12.0
5.0 11.0 13.0 4.0 8.0</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var isIntegral = Fn.new { |x, tol| x.fraction.abs <= tol }
 
var pascal = Fn.new { |a, b, mid, top|
var yd = (top - 4 * (a + b)) / 7
if (!isIntegral.call(yd, 0.0001)) return [0, 0, 0]
var y = yd.truncate
var x = mid - 2*a - y
return [x, y, y - x]
}
 
var sol = pascal.call(11, 4, 40, 151)
if (sol[0] != 0) {
Fmt.print("Solution is: x = $d, y = $d, z = $d", sol[0], sol[1], sol[2])
} else {
System.print("There is no solution")
}</syntaxhighlight>
 
{{out}}
<pre>
Solution is: x = 5, y = 13, z = 8
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">proc Print; int N, A, B, C, D, E;
int I, P;
def Tab = $09;
[P:= @A; \point to first number
for I:= N to 5-1 do ChOut(0, Tab);
for I:= 0 to N-1 do
[IntOut(0, P(I)); ChOut(0, Tab); ChOut(0, Tab)];
CrLf(0);
];
 
int N, P, Q, R, S, T, U, V, W, X, Y, Z; \ 151
[for X:= 0 to 40-11 do \ N P
for Z:= 0 to 151-4 do \ Q R S
[Y:= X+Z; \ T U V W
T:= X+11; \X 11 Y 4 Z
U:= 11+Y;
V:= Y+4;
W:= 4+Z;
if T+U = 40 then
[R:= U+V;
S:= V+W;
N:= 40+R;
P:= R+S;
if N+P = 151 then
[Print(1, 151);
Print(2, N, P);
Print(3, 40, R, S);
Print(4, T, U, V, W);
Print(5, X, 11, Y, 4, Z);
exit;
];
];
];
]</syntaxhighlight>
 
{{out}}
<pre>
151
81 70
40 41 29
16 24 17 12
5 11 13 4 8
</pre>
 
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight lang="zkl"># Pyramid solver
# [151]
# [ ] [ ]
Line 2,501 ⟶ 3,300:
L("X", 11, "Y", 4, "Z") );
addlConstraint:=Dictionary( "X",1, "Y",-1, "Z",1, "1",0 );
solvePyramid(p, addlConstraint);</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn solvePyramid([List]vl,[Dictionary]cnstr){ //ListOfLists,Hash-->zip
vl=vl.reverse();
constraints:=L(cnstr);
Line 2,584 ⟶ 3,383:
}
return(mtx);
}</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits