Solve hanging lantern problem: Difference between revisions
Content added Content deleted
No edit summary |
(Created Nim solution.) |
||
Line 867: | Line 867: | ||
There are 65191584694745586153436251091200000 ways to take these 9 columns down. |
There are 65191584694745586153436251091200000 ways to take these 9 columns down. |
||
</pre> |
</pre> |
||
=={{header|Nim}}== |
|||
Recursive solution. |
|||
The number of elements in the columns are provided as command arguments. |
|||
<syntaxhighlight lang="Nim">import std/[os, strutils] |
|||
proc sequenceCount(columns: var seq[int]): int = |
|||
for icol in 1..columns.high: |
|||
if columns[icol] > 0: |
|||
dec columns[icol] |
|||
inc result, sequenceCount(columns) |
|||
inc columns[icol] |
|||
if result == 0: result = 1 |
|||
let ncol = paramCount() |
|||
if ncol == 0: |
|||
quit "Missing parameters.", QuitFailure |
|||
var columns = newSeq[int](ncol + 1) # We will ignore the first column. |
|||
for i in 1..ncol: |
|||
let n = paramStr(i).parseInt() |
|||
if n < 0: |
|||
quit "Wrong number of lanterns.", QuitFailure |
|||
columns[i] = n |
|||
echo columns.sequenceCount() |
|||
</syntaxhighlight> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |