Pascal's triangle/Puzzle: Difference between revisions

Line 1,732:
Solution:
x=5, y=13, z=8</pre>
 
=={{header|Phix}}==
I approached this with a view to solving general pyramid puzzles, not just the one given.
<lang Phix>--This little ditty converts the pyramid to rules quite nicely, however I will concede
--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.
 
sequence pyramid = {
{151},
{"",""},
{40,"",""},
{"","","",""},
{"x",11,"y",4,"z"}}
 
sequence rules = {}
 
-- each cell in the pyramid is either an integer final value or an equation.
-- initially the equations are strings, we substitute all with triplets of
-- the form {k,x,z} ie k+l*x+m*z, and known values < last row become rules.
 
for r=5 to 1 by -1 do
for c=1 to length(pyramid[r]) do
object prc = pyramid[r][c], equ
if prc="x" then prc = {0,1,0} -- ie one x
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
else
if prc="" or r<=4 then
-- examples: x+11 is {0,1,0}+{11,0,0} -> {11,1,0},
-- 11+y is {11,0,0}+{0,1,1} -> {11,1,1},
-- 40=""+"" is {40,0,0}={22,2,1} ==> {18,2,1}
equ = sq_add(pyramid[r+1][c],pyramid[r+1][c+1])
end if
if prc="" then prc = equ
else prc = {prc,0,0}
if r<=4 then
equ[1] = prc[1]-equ[1]
rules = append(rules,equ)
end if
end if
end if
pyramid[r][c] = prc
end for
end for
ppOpt({pp_Nest,1,pp_StrFmt,1})
?"equations"
pp(pyramid)
?"rules"
pp(rules)
puts(1,"=====\n")
 
if length(rules)!=2 then ?9/0 end if -- more work needed!?
 
-- admittedly this bit is rather amateurish, and maybe problem-specific:
sequence xrule = sq_sub(sq_mul(rules[1],rules[2][3]),sq_mul(rules[2],rules[1][3])),
zrule = sq_sub(sq_mul(rules[2],rules[1][2]),sq_mul(rules[1],rules[2][2]))
 
?{"xrule",xrule}
?{"zrule",zrule}
 
integer x = xrule[1]/xrule[2],
z = zrule[1]/zrule[3],
y = x+z
 
printf(1,"x = %d, y=%d, z=%d\n",{x,y,z})
 
-- finally evaluate all the equations and print it.
for r=1 to length(pyramid) do
for c=1 to length(pyramid[r]) do
integer {k, l, m} = pyramid[r][c]
pyramid[r][c] = k+l*x+m*z
end for
end for
 
pp(pyramid)</lang>
{{out}}
<pre>
"equations"
{{{151,0,0}},
{{55,2,2}, {23,3,4}},
{{40,0,0}, {15,2,2}, {8,1,2}},
{{11,1,0}, {11,1,1}, {4,1,1}, {4,0,1}},
{{0,1,0}, {11,0,0}, {0,1,1}, {4,0,0}, {0,0,1}}}
"rules"
{{18,2,1},
{73,5,6}}
=====
{"xrule",{35,7,0}}
{"zrule",{56,0,7}}
x = 5, y=13, z=8
{{151},
{81,70},
{40,41,29},
{16,24,17,12},
{5,11,13,4,8}}
</pre>
 
=={{header|PicoLisp}}==
7,805

edits