Long stairs: Difference between revisions

1,504 bytes added ,  11 months ago
Created Nim solution.
(Added AppleScript.)
(Created Nim solution.)
Line 650:
Ten thousand trials to top:
(mean(times), mean(heights)) = (2927.0853, 14735.4265)
</pre>
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[random, strformat]
 
randomize()
 
proc simulate(verbose: bool): (int, int) =
if verbose:
echo "Seconds Steps behind Steps ahead"
var curr = 1 # Number of current step.
var last = 100 # Number of last step.
var t = 0
while true:
inc t
inc curr
if curr > last:
return (t, last) # Escaped!
# Add new steps.
for i in 1..5:
let n = rand(1..last)
if n < curr: inc curr # Behind current step.
inc last
if verbose and t in 600..609:
echo &"{t:^7} {curr:^12} {last - curr:^12}"
if t == 609: return # This part is terminated.
 
# First part of the task.
discard simulate(true)
echo()
 
# Second part of the task.
var tSum, stepSum = 0
for _ in 1..10_000:
let (t, n) = simulate(false)
tSum += t
stepSum += n
echo &"Average seconds taken: {tSum / 10_000}"
echo &"Average final length of staircase: {stepSum / 10_000}"
</syntaxhighlight>
 
{{out}}
<pre>Seconds Steps behind Steps ahead
600 2031 1069
601 2034 1071
602 2038 1072
603 2043 1072
604 2046 1074
605 2050 1075
606 2055 1075
607 2061 1074
608 2066 1074
609 2070 1075
 
Average seconds taken: 2924.1551
Average final length of staircase: 14715.7755
</pre>
 
256

edits