Anonymous user
Pascal's triangle/Puzzle: Difference between revisions
Updated to work with Nim 1.4. Many changes to make code more idiomatic, especially regarding identifier case and indentation. Avoided float division. Some simplifications.t
(Added XPL0 example.) |
(Updated to work with Nim 1.4. Many changes to make code more idiomatic, especially regarding identifier case and indentation. Avoided float division. Some simplifications.t) |
||
Line 1,717:
=={{header|Nim}}==
{{trans|Ada}}
<lang nim>import
type
BlockValue = object
known: int
Variables = tuple[x, y, z: int]
func `+=`(left: var BlockValue; right: BlockValue) =
## Symbolically add one block to another.
left.known += right.known
left.x += right.x - right.z # Z is excluded as n(Y - X - Z) = 0.
left.y += right.y + right.z
proc toString(n: BlockValue; vars: Variables): string =
## Return the representation of the block value, when X, Y, Z are known.
result = $(n.known + n.x * vars.x + n.y * vars.y + n.z * vars.z)
proc Solve2x2(a11, a12, b1, a21, a22, b2: int): Variables =
## Solve a puzzle, supposing an integer solution exists.
if a22 == 0:
result.x = b2 div
result.y = (b1 - a11 * result.x) div a12
else:
result.x = (b1 * a22 - b2
result.y = (b1 - a11 *
result.z = result.y - result.x
var blocks : array[1..5, array[1..5, BlockValue]] # The lower triangle contains blocks.
# The bottom blocks.
blocks[5][1] = BlockValue(x: 1)
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</lang>
{{out}}
<pre>151
81 70
40 41 29
16 24 17 12
5 11 13 4 8</pre>
|