Pascal's triangle/Puzzle: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(8 intermediate revisions by 2 users not shown)
Line 1,111:
(doseq [row rows]
(println (map #(dot [1 x z] %) row)))</syntaxhighlight>
 
=={{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}}==
Line 1,698 ⟶ 1,756:
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>
 
Line 3,093 ⟶ 3,218:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var isIntegral = Fn.new { |x, tol| x.fraction.abs <= tol }
9,485

edits