Long stairs: Difference between revisions

Content added Content deleted
(Python example)
Line 515: Line 515:


average stair length 15438 average seconds 3067
average stair length 15438 average seconds 3067
</pre>

=={{header|Python}}==
<lang python>""" https://rosettacode.org/wiki/Long_stairs """

from numpy import mean
from random import sample

def gen_long_stairs(start_step, start_length, climber_steps, add_steps):
secs, behind, total = 0, start_step, start_length
while True:
behind += climber_steps
behind += sum([behind > n for n in sample(range(total), add_steps)])
total += add_steps
secs += 1
yield (secs, behind, total)

ls = gen_long_stairs(1, 100, 1, 5)

print("Seconds Behind Ahead\n----------------------")
while True:
secs, pos, len = next(ls)
if 600 <= secs < 610:
print(secs, " ", pos, " ", len - pos)
elif secs == 610:
break

print("\nTen thousand trials to top:")
times, heights = [], []
for trial in range(10_000):
trialstairs = gen_long_stairs(1, 100, 1, 5)
while True:
sec, step, height = next(trialstairs)
if step >= height:
times.append(sec)
heights.append(height)
break

print("Mean time:", mean(times), "secs. Mean height:", mean(heights))
</lang>{{out}}
<pre>
Seconds Behind Ahead
----------------------
600 2122 978
601 2127 978
602 2130 980
603 2135 980
604 2139 981
605 2144 981
606 2149 981
607 2153 982
608 2157 983
609 2162 983

Ten thousand trials to top:
Mean time: 2780.502 secs. Mean height: 14002.51
</pre>
</pre>