Long stairs: Difference between revisions

added RPL
(Added Go)
(added RPL)
 
(15 intermediate revisions by 11 users not shown)
Line 12:
{{trans|C}}
 
<langsyntaxhighlight lang="11l">V secs_tot = 0
V steps_tot = 0
print(‘Seconds steps behind steps ahead’)
Line 35:
 
print(‘Average secs taken: #.6’.format(secs_tot / 10000.0))
print(‘Average final length of staircase: #.6’.format(steps_tot / 10000.0))</langsyntaxhighlight>
 
{{out}}
Line 53:
Average final length of staircase: 14700.387500
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use AppleScript version "2.5" -- OS X 10.11 (El Capitan) or later.
use framework "Foundation"
use framework "GameplayKit" -- For the random number generator (faster than AppleScript's).
 
on longStairs()
set output to {"Sample from first run:", "Seconds Steps behind Steps ahead"}
set RNG to current application's class "GKMersenneTwisterRandomSource"'s new()
set {runCount, totalTime, totalLength, firstRun, padding} to {10000, 0, 0, true, " "}
repeat runCount times
set t to 0
set stepsBehind to 0
set stepsAhead to 100
repeat until (stepsAhead = 0)
repeat 5 times
if ((RNG's nextIntWithUpperBound:(stepsBehind + stepsAhead)) > stepsBehind) then
set stepsAhead to stepsAhead + 1
else
set stepsBehind to stepsBehind + 1
end if
end repeat
set t to t + 1
set stepsBehind to stepsBehind + 1
set stepsAhead to stepsAhead - 1
if ((firstRun) and (t < 610) and (t > 599)) then ¬
set end of output to text -5 thru -1 of (padding & t) & ¬
text -13 thru -1 of (padding & stepsBehind) & ¬
text -15 thru -1 of (padding & stepsAhead)
end repeat
set totalTime to totalTime + t
set totalLength to totalLength + stepsBehind
set firstRun to false
end repeat
set {avTime, avLength} to {totalTime / runCount, totalLength / runCount}
set end of output to "Average time taken over " & intToText(runCount, ",") & " runs: " & ¬
(avTime div minutes) & " minutes " & (avTime mod minutes) & " seconds"
set end of output to "Average final staircase length: " & intToText(avLength div 1, ",") & ¬
"." & text 3 thru -1 of ((avLength mod 1) as text) & " steps"
return join(output, linefeed)
end longStairs
 
on intToText(int, separator)
set groups to {}
repeat while (int > 999)
set groups's beginning to ((1000 + (int mod 1000 as integer)) as text)'s text 2 thru 4
set int to int div 1000
end repeat
set groups's beginning to int
return join(groups, separator)
end intToText
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
longStairs()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"Sample from first run:
Seconds Steps behind Steps ahead
600 2276 824
601 2278 827
602 2282 828
603 2287 828
604 2292 828
605 2296 829
606 2299 831
607 2304 831
608 2310 830
609 2315 830
Average time taken over 10,000 runs: 48 minutes 39.0985 seconds
Average final staircase length: 14,695.4925 steps"</syntaxhighlight>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">result := "Sec steps behind steps ahead"
x := longStairs(609)
for i, s in x.4
result .= "`n" i+599 "`t" s "`t`t" x.2 - s
tests := 10000
loop % tests
{
x := longStairs()
totSteps += x.1
totSec += x.3
}
MsgBox, 262144, , % result .= "`n`nAfter " tests " tests:`nAverage time taken = " totSec/tests " Sec"
. "`nAverage Stair length = " totSteps/tests " steps"
return
 
longStairs(t:=0){
Stairs := [], last10Steps := [], s := 0
loop 100
Stairs[A_Index] := 0
loop
{
Stairs[++s] := "S" ; take a step forward
if (last10Steps.count()>=10)
last10Steps.RemoveAt(1)
last10Steps.push(s)
loop 5 ; add 5 steps to stairs
{
Random, stp, 1, % Stairs.Count()
Stairs.InsertAt(stp, "x")
s += s > stp ? 1 : 0
}
escT := s = Stairs.Count() ? A_Index : 0
if (A_Index = t || escT) ; reached time limit or Escaped
break
}
return [s, Stairs.Count(), escT, last10Steps]
}</syntaxhighlight>
{{out}}
<pre>Sec steps behind steps ahead
600 1991 1154
601 1993 1152
602 1997 1148
603 2001 1144
604 2004 1141
605 2008 1137
606 2010 1135
607 2013 1132
608 2016 1129
609 2018 1127
 
After 10000 tests:
Average time taken = 3063.260000 Sec
Average Stair length = 15416.300000 steps</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">steps_behind = 0
stairs_length = 100
seconds_tot = 0
steps_tot = 0
print "Seconds steps behind steps ahead"
for trial = 1 to 10000 #We'll have the runner try this 10000 times
steps_behind = 0 #runner starts at the bottom
seconds = 0 #reset time taken
stairs_length = 100 #Staircase has 100 steps
while steps_behind < stairs_length #if the runner hasn#t reached the top
steps_behind += 1 #go up one step
for j = 1 to 5 #The evil wizard conjures another five steps
if int(rand*stairs_length) < steps_behind then steps_behind += 1
#there#s a chance that a new step will be behind you
stairs_length += 1 #but either way the staircase is one step longer
next j
seconds += 1 #that all took one second
if trial = 1 and seconds > 599 and seconds < 610 then print seconds, steps_behind, stairs_length - steps_behind
#for the first attempt, see how the runner is doing after ten minutes
end while
seconds_tot += seconds #if the runner escaped, track the time taken and the length of the stairs
steps_tot += stairs_length
next trial
 
print "Average time taken: "; seconds_tot/10000; " seconds."
print "Average final staircase length: "; steps_tot/10000; " steps."
#if you noticed that last number is about 100*exp(5), that#s no coincidence</syntaxhighlight>
{{out}}
<pre>Similar as FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="vbnet">
randomize timer
dim as uinteger steps_behind = 0, stairs_length = 100, seconds, j
dim as uinteger seconds_tot, steps_tot
print "Seconds", "steps behind", "steps ahead"
for trial as uinteger = 1 to 10000 'We'll have the runner try this 10000 times
steps_behind = 0 'runner starts at the bottom
seconds = 0 'reset time taken
stairs_length = 100 'Staircase has 100 steps
while steps_behind < stairs_length 'if the runner hasn't reached the top
steps_behind += 1 'go up one step
for j = 1 to 5 'The evil wizard conjures another five steps
if int(rnd*stairs_length) < steps_behind then steps_behind += 1
'there's a chance that a new step will be behind you
stairs_length += 1 'but either way the staircase is one step longer
next j
seconds += 1 'that all took one second
if trial = 1 and seconds >599 and seconds < 610 then print seconds, steps_behind, stairs_length - steps_behind
'for the first attempt, see how the runner is doing after ten minutes
wend
seconds_tot += seconds 'if the runner escaped, track the time taken and the length of the stairs
steps_tot += stairs_length
next trial
 
print "Average time taken: ";seconds_tot/10000; " seconds."
print "Average final staircase length: ";steps_tot/10000; " steps."
'if you noticed that last number is about 100*exp(5), that's no coincidence</syntaxhighlight>
{{out}}<pre>
Seconds steps behind steps ahead
600 2032 1068
601 2038 1067
602 2042 1068
603 2045 1070
604 2048 1072
605 2053 1072
606 2055 1075
607 2060 1075
608 2064 1076
609 2068 1077
Average time taken: 2921.9457 seconds.
Average final staircase length: 14709.7285 steps.
</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
 
Randomize
Dim steps_behind As Integer = 0, stairs_length As Integer = 100, seconds As Integer
Dim seconds_tot As Integer, steps_tot As Integer, j As Integer
Print "Seconds", "steps behind", "steps ahead"
For trial As Integer = 1 To 10000 'We'll have the runner try this 10000 times
steps_behind = 0 'runner starts at the bottom
seconds = 0 'reset time taken
stairs_length = 100 'Staircase has 100 steps
While steps_behind < stairs_length 'if the runner hasn't reached the top
steps_behind += 1 'go up one step
For j = 1 To 5 'The evil wizard conjures another five steps
If Int(Rnd * stairs_length) < steps_behind Then steps_behind += 1
'there's a chance that a new step will be behind you
stairs_length += 1 'but either way the staircase is one step longer
Next
seconds += 1 'that all took one second
If trial = 1 And seconds > 599 And seconds < 610 Then Print seconds, steps_behind, Chr$(9); stairs_length - steps_behind
'for the first attempt, see how the runner is doing after ten minutes
Wend
seconds_tot += seconds 'if the runner escaped, track the time taken and the length of the stairs
steps_tot += stairs_length
Next
Print "Average time taken: "; seconds_tot / 10000; " seconds."
Print "Average final staircase length: "; steps_tot / 10000; " steps."
'if you noticed that last number is about 100*exp(5), that's no coincidence
End </syntaxhighlight>
{{out}}
<pre>Similar as FreeBASIC entry.</pre>
 
==={{header|GW-BASIC}}===
<syntaxhighlight lang="qbasic">
10 RANDOMIZE TIMER
20 TIMET = 0 : STEPST = 0
30 FOR TRIAL = 1 TO 10000
40 TIME = 0
50 SBEH = 0
60 SLEN = 100
70 SBEH = SBEH + 1
80 IF SBEH >= SLEN THEN GOTO 160
90 FOR WIZ = 1 TO 5
100 IF INT(RND*SLEN)<SBEH THEN SBEH = SBEH + 1
110 SLEN = SLEN + 1
120 NEXT WIZ
130 TIME = TIME + 1
140 IF TRIAL = 1 AND 599<TIME AND TIME <610 THEN PRINT TIME, SBEH, SLEN-SBEH
150 GOTO 70
160 TIMET = TIMET+TIME+1
170 STEPST = STEPST + SLEN
180 NEXT TRIAL
190 PRINT TIMET/10000
200 PRINT STEPST/10000</syntaxhighlight>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">OpenConsole()
Define.i steps_behind = 0, stairs_length = 100, seconds, j
Define.i seconds_tot, steps_tot
PrintN("Seconds" + #TAB$ + "steps behind" + #TAB$ + "steps ahead")
For trial.i = 1 To 10000 ;We'll have the runner try this 10000 times
steps_behind = 0 ;runner starts at the bottom
seconds = 0 ;reset time taken
stairs_length = 100 ;Staircase has 100 steps
While steps_behind < stairs_length ;if the runner hasn;t reached the top
steps_behind + 1 ;go up one step
For j = 1 To 5 ;The evil wizard conjures another five steps
If Int(Random(1) * stairs_length) < steps_behind:
steps_behind + 1
EndIf
;there;s a chance that a new step will be behind you
stairs_length + 1 ;but either way the staircase is one step longer
Next j
seconds + 1 ;that all took one second
If (trial = 1) And (seconds > 599) And (seconds < 610):
PrintN(Str(seconds) + #TAB$ + #TAB$ + Str(steps_behind) + #TAB$ + Str(stairs_length - steps_behind))
;for the first attempt, see how the runner is doing after ten minutes
EndIf
Wend
seconds_tot + seconds ;if the runner escaped, track the time taken and the length of the stairs
steps_tot + stairs_length
Next trial
 
PrintN("Average time taken: " + Str(seconds_tot/10000) + " seconds.")
PrintN("Average final staircase length: " + Str(steps_tot/10000) + " steps.")
;if you noticed that last number is about 100*exp(5), that;s no coincidence
 
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Similar as FreeBASIC entry.</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|True BASIC}}
<syntaxhighlight lang="qbasic">RANDOMIZE TIMER : REM RANDOMIZE for True BASIC
stepsbehind = 0
stairslength = 100
 
PRINT "Seconds", "steps behind", "steps ahead"
FOR trial = 1 TO 10000
stepsbehind = 0
seconds = 0
stairslength = 100
DO WHILE stepsbehind < stairslength
stepsbehind = stepsbehind + 1
FOR j = 1 TO 5
IF INT(RND * stairslength) < stepsbehind THEN stepsbehind = stepsbehind + 1
stairslength = stairslength + 1
NEXT j
seconds = seconds + 1
IF trial = 1 AND seconds > 599 AND seconds < 610 THEN PRINT seconds, stepsbehind, stairslength - stepsbehind
LOOP
secondstot = secondstot + seconds
stepstot = stepstot + stairslength
NEXT trial
 
PRINT "Average time taken: "; secondstot / 10000; " seconds."
PRINT "Average final staircase length: "; stepstot / 10000; " steps."
END</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|QBasic}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">RANDOMIZE ! RANDOMIZE TIME for QBasic
LET stepsbehind = 0
LET stairslength = 100
 
PRINT "Seconds", "steps behind", "steps ahead"
FOR trial = 1 TO 10000
LET stepsbehind = 0
LET seconds = 0
LET stairslength = 100
DO WHILE stepsbehind < stairslength
LET stepsbehind = stepsbehind + 1
FOR j = 1 TO 5
IF INT(RND * stairslength) < stepsbehind THEN LET stepsbehind = stepsbehind + 1
LET stairslength = stairslength + 1
NEXT j
LET seconds = seconds + 1
IF trial = 1 AND seconds > 599 AND seconds < 610 THEN PRINT seconds, stepsbehind, stairslength - stepsbehind
LOOP
LET secondstot = secondstot + seconds
LET stepstot = stepstot + stairslength
NEXT trial
 
PRINT "Average time taken: "; secondstot / 10000; " seconds."
PRINT "Average final staircase length: "; stepstot / 10000; " steps."
END</syntaxhighlight>
 
==={{header|VBScript}}===
<syntaxhighlight lang="vb">
Option Explicit
Randomize Timer
 
Function pad(s,n)
If n<0 Then pad= right(space(-n) & s ,-n) Else pad= left(s& space(n),n) End If
End Function
 
Sub print(s)
On Error Resume Next
WScript.stdout.WriteLine (s)
If err= &h80070006& Then WScript.Echo " Please run this script with CScript": WScript.quit
End Sub
 
Function Rounds(maxsecs,wiz,a)
Dim mystep,maxstep,toend,j,i,x,d
If IsArray(a) Then d=True: print "seconds behind pending"
maxstep=100
For j=1 To maxsecs
For i=1 To wiz
If Int(Rnd*maxstep)<=mystep Then mystep=mystep+1
maxstep=maxstep+1
Next
mystep=mystep+1
If mystep=maxstep Then Rounds=Array(j,maxstep) :Exit Function
If d Then
If j>=a(0) And j<=a(1) Then print pad(j,-7) & pad (mystep,-7) & pad (maxstep-mystep,-8)
End If
Next
Rounds=Array(maxsecs,maxstep)
End Function
 
 
Dim n,r,a,sumt,sums,ntests,t,maxsecs
ntests=10000
maxsecs=7000
t=timer
a=Array(600,609)
For n=1 To ntests
r=Rounds(maxsecs,5,a)
If r(0)<>maxsecs Then
sumt=sumt+r(0)
sums=sums+r(1)
End if
a=""
Next
 
print vbcrlf & "Done " & ntests & " tests in " & Timer-t & " seconds"
print "escaped in " & sumt/ntests & " seconds with " & sums/ntests & " stairs"
</syntaxhighlight>
{{out}}
<small>
<pre>
seconds behind pending
600 2123 977
601 2126 979
602 2130 980
603 2134 981
604 2137 983
605 2141 984
606 2145 985
607 2151 984
608 2155 985
609 2159 986
 
Done 10000 tests in 51.30469 seconds
escaped in 2923.1174 seconds with 14715.587 stairs
</pre>
</small>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">steps_behind = 0
stairs_length = 100
print "Seconds", chr$(9), "steps behind", chr$(9), "steps ahead"
for trial = 1 to 10000 //We'll have the runner try this 10000 times
steps_behind = 0 //runner starts at the bottom
seconds = 0 //reset time taken
stairs_length = 100 //Staircase has 100 steps
while steps_behind < stairs_length //if the runner hasn//t reached the top
steps_behind = steps_behind + 1 //go up one step
for j = 1 to 5 //The evil wizard conjures another five steps
if int(ran(1)*stairs_length) < steps_behind steps_behind = steps_behind + 1
//there//s a chance that a new step will be behind you
stairs_length = stairs_length + 1 //but either way the staircase is one step longer
next j
seconds = seconds + 1 //that all took one second
if trial = 1 and seconds > 599 and seconds < 610 print seconds, chr$(9), steps_behind, chr$(9), chr$(9), stairs_length - steps_behind
//for the first attempt, see how the runner is doing after ten minutes
wend
seconds_tot = seconds_tot + seconds //if the runner escaped, track the time taken and the length of the stairs
steps_tot = steps_tot + stairs_length
next trial
 
print "Average time taken: ", seconds_tot/10000, " seconds."
print "Average final staircase length: ", steps_tot/10000, " steps."
//if you noticed that last number is about 100*exp(5), that//s no coincidence</syntaxhighlight>
{{out}}
<pre>Similar as FreeBASIC entry.</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">
<lang c>
#include <stdio.h>
#include <stdlib.h>
Line 86 ⟶ 552:
printf( "Average final length of staircase: %f\n", steps_tot/10000.0 );
return 0;
}</langsyntaxhighlight>
{{out}}<pre>
Seconds steps behind steps ahead
Line 102 ⟶ 568:
Average final length of staircase: 14707.466000
</pre>
 
=={{header|Dart}}==
{{trans|C}}
<syntaxhighlight lang="dart">import 'dart:math';
 
void main() {
int secsTot = 0,
stepsTot = 0; // keep track of time and steps over all the trials
Random rand = new Random();
 
print("Seconds steps behind steps ahead");
 
for (int trial = 1; trial <= 10000; trial++) {
// 10000 attempts for the runner
int sbeh = 0, slen = 100, secs = 0; // initialise this trial
 
while (sbeh < slen) {
// as long as the runner is still on the stairs
sbeh += 1; // runner climbs a step
 
for (int wiz = 1; wiz <= 5; wiz++) {
// evil wizard conjures five new steps
if (rand.nextInt(slen) < sbeh)
sbeh += 1; // maybe a new step is behind us
slen += 1; // either way, the staircase is longer
}
 
secs += 1; // one second has passed
 
if (trial == 1 && 599 < secs && secs < 610)
print("$secs $sbeh ${slen - sbeh}");
}
 
secsTot += secs;
stepsTot += slen;
}
 
print("Average secs taken: ${secsTot / 10000.0}");
print("Average final length of staircase: ${stepsTot / 10000.0}");
}</syntaxhighlight>
{{out}}
<pre>Similar as C entry.</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators.random io kernel math math.order prettyprint ;
 
: position ( -- n ) 0 ;
Line 128 ⟶ 636:
: main ( -- ) .header sims nl steps time ;
 
main</langsyntaxhighlight>
{{out}}
<pre>
Line 147 ⟶ 655:
</pre>
And for fun, a version without stack effects.
<langsyntaxhighlight lang="factor">USING: combinators.random effects.parser io kernel math
math.order parser prettyprint stack-checker words ;
 
Line 174 ⟶ 682:
.: main .header sims nl steps time ;
 
main</langsyntaxhighlight>
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">
time_tot:=0; {we'll keep track of the total time and steps across all the trials}
steps_tot:=0;
Line 203 ⟶ 711:
 
!!(time_tot/10000);
!!(steps_tot/10000);</langsyntaxhighlight>
{{out}}<pre>
Seconds Steps behind Steps ahead
Line 219 ⟶ 727:
3722083 / 250
</pre>
 
=={{header|FreeBASIC}}==
 
<lang freebasic>
randomize timer
dim as uinteger steps_behind = 0, stairs_length = 100, seconds, j
dim as uinteger seconds_tot, steps_tot
print "Seconds", "steps behind", "steps ahead"
for trial as uinteger = 1 to 10000 'We'll have the runner try this 10000 times
steps_behind = 0 'runner starts at the bottom
seconds = 0 'reset time taken
stairs_length = 100 'Staircase has 100 steps
while steps_behind < stairs_length 'if the runner hasn't reached the top
steps_behind += 1 'go up one step
for j = 1 to 5 'The evil wizard conjures another five steps
if int(rnd*stairs_length) < steps_behind then steps_behind += 1
'there's a chance that a new step will be behind you
stairs_length += 1 'but either way the staircase is one step longer
next j
seconds += 1 'that all took one second
if trial = 1 and seconds >599 and seconds < 610 then print seconds, steps_behind, stairs_length - steps_behind
'for the first attempt, see how the runner is doing after ten minutes
wend
seconds_tot += seconds 'if the runner escaped, track the time taken and the length of the stairs
steps_tot += stairs_length
next trial
 
print "Average time taken: ";seconds_tot/10000; " seconds."
print "Average final staircase length: ";steps_tot/10000; " steps."
'if you noticed that last number is about 100*exp(5), that's no coincidence</lang>
{{out}}<pre>
Seconds steps behind steps ahead
600 2032 1068
601 2038 1067
602 2042 1068
603 2045 1070
604 2048 1072
605 2053 1072
606 2055 1075
607 2060 1075
608 2064 1076
609 2068 1077
Average time taken: 2921.9457 seconds.
Average final staircase length: 14709.7285 steps.
</pre>
 
=={{header|GW-BASIC}}==
<lang gwbasic>
10 RANDOMIZE TIMER
20 TIMET = 0 : STEPST = 0
30 FOR TRIAL = 1 TO 10000
40 TIME = 0
50 SBEH = 0
60 SLEN = 100
70 SBEH = SBEH + 1
80 IF SBEH >= SLEN THEN GOTO 160
90 FOR WIZ = 1 TO 5
100 IF INT(RND*SLEN)<SBEH THEN SBEH = SBEH + 1
110 SLEN = SLEN + 1
120 NEXT WIZ
130 TIME = TIME + 1
140 IF TRIAL = 1 AND 599<TIME AND TIME <610 THEN PRINT TIME, SBEH, SLEN-SBEH
150 GOTO 70
160 TIMET = TIMET+TIME+1
170 STEPST = STEPST + SLEN
180 NEXT TRIAL
190 PRINT TIMET/10000
200 PRINT STEPST/10000</lang>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 326 ⟶ 766:
fmt.Println("\nAverage secs taken:", float64(totalSecs)/10000)
fmt.Println("Average final length of staircase:", float64(totalSteps)/10000)
}</langsyntaxhighlight>
 
{{out}}
Line 347 ⟶ 787:
Average final length of staircase: 14689.519
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">stairsim=:{{
t=. 0 NB. fifths of a second
echo ;_8 _16 _20{.each 'seconds';'steps to top';'steps from bottom'
for_trial. i.1e4 do.
loc=. {.'min max'=. 0 100
while. loc < max do.
if. 0=5|t=.t+1 do.
loc=. loc+1
if. 0=trial do.
if. 1=2999 3046 I.t do.
echo 8 16 20":(t%5),(max-loc),(loc-min)
end.
end.
end.
ins=. min+?max-min
if. ins < loc do. min=. min-1 else. max=. max+1 end.
end.
end.
echo 'Average steps taken: ',":t%5e4
echo 'Average length of staircase: ',":100+t%1e4
}}</syntaxhighlight>
 
Example run:<syntaxhighlight lang="j"> stairsim''
seconds steps to top steps from bottom
600 1142 1957
601 1142 1962
602 1141 1968
603 1141 1973
604 1142 1977
605 1143 1981
606 1143 1986
607 1143 1991
608 1144 1995
609 1144 2000
Average steps taken: 3026.43
Average length of staircase: 15232.2</syntaxhighlight>
 
=={{header|jq}}==
Line 362 ⟶ 840:
For simplicity and since the intermediate output has the character of
a probe, the program uses `debug` statements to show the intermediate results.
<langsyntaxhighlight lang="jq"># 0 <= output < $n
def rand($n):
 
Line 401 ⟶ 879:
| "\nAverage secs taken over \($n) trials: \(.totalSecs/$n)",
"Average final length of staircase: \(.totalSteps/$n)"
)</langsyntaxhighlight>
{{out}}
<pre>
Line 420 ⟶ 898:
Average final length of staircase: 16159.81
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">""" https://rosettacode.org/wiki/Long_stairs """
 
using Statistics
Line 465 ⟶ 942:
end
@show mean(times), mean(heights)
</langsyntaxhighlight>{{out}}
<pre>
Seconds Behind Ahead
Line 480 ⟶ 957:
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>
 
Line 486 ⟶ 1,018:
Trying to calculate results of multiple random rounds.Now a partial step forward 1/CntOfSpellStairs for every spell stair.<BR>
Now results are much closer.
<langsyntaxhighlight lang="pascal">program WizardStaircase;
const
StartStairLength = 100;
Line 588 ⟶ 1,120:
For i := 1 to 10 do
CheckDouble(i);
end.</langsyntaxhighlight>
{{Out|@ TIO.RUN}}
<pre>
Line 627 ⟶ 1,159:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (nb expect blank screen for ~30s)</span>
Line 655 ⟶ 1,187:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Average seconds: %f, average steps: %f\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">total_seconds</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">total_steps</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 675 ⟶ 1,207:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Long_stairs
Line 700 ⟶ 1,232:
}
printf "\naverage stair length %d average seconds %d\n",
$sumsizes / $runs, $sumseconds / $runs;</langsyntaxhighlight>
{{out}}
<pre>
Line 718 ⟶ 1,250:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">""" https://rosettacode.org/wiki/Long_stairs """
 
from numpy import mean
Line 755 ⟶ 1,287:
 
print("Mean time:", mean(times), "secs. Mean height:", mean(heights))
</langsyntaxhighlight>{{out}}
<pre>
Seconds Behind Ahead
Line 772 ⟶ 1,304:
Ten thousand trials to top:
Mean time: 2780.502 secs. Mean height: 14002.51
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ 0 100 0
[ 5 times
[ 2dup swap
random > +
dip 1+ ]
1+
rot 1+ unrot
2dup < until ]
drop ] is test ( --> n n )
 
( and again, with progress report )
 
[ 0 100 0
[ rot dup dip unrot
dup 600 610 within iff
[ say "After " echo
say " seconds, "
2dup dup
say "Behind: "
echo
say " Ahead: "
- echo cr ]
else drop
5 times
[ 2dup swap
random > +
dip 1+ ]
1+
rot 1+ unrot
2dup < until ]
drop 2drop ] is progress ( --> )
 
progress
cr
0 0 10000 times
[ test rot +
unrot + swap ]
swap
say "Mean time: "
number$ char . swap -4 stuff echo$ cr
say "Mean stairs: "
number$ char . swap -4 stuff echo$ cr</syntaxhighlight>
 
{{out}}
 
<pre>After 600 seconds, Behind: 2187 Ahead: 913
After 601 seconds, Behind: 2191 Ahead: 914
After 602 seconds, Behind: 2197 Ahead: 913
After 603 seconds, Behind: 2200 Ahead: 915
After 604 seconds, Behind: 2204 Ahead: 916
After 605 seconds, Behind: 2208 Ahead: 917
After 606 seconds, Behind: 2211 Ahead: 919
After 607 seconds, Behind: 2214 Ahead: 921
After 608 seconds, Behind: 2218 Ahead: 922
After 609 seconds, Behind: 2224 Ahead: 921
 
Mean time: 3063.7808
Mean stairs: 15418.9040
</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my ($trials, $t-total, $s-total) = 10000;
 
say 'Seconds steps behind steps ahead';
Line 799 ⟶ 1,393:
}
 
say "Average seconds: {$t-total/$trials}, Average steps: {$s-total/$trials}";</langsyntaxhighlight>
{{out|Sample output}}
<pre>Seconds steps behind steps ahead
Line 814 ⟶ 1,408:
Average seconds: 2716.0197, Average steps: 13677.143</pre>
 
=={{header|VlangRPL}}==
« 0 (100,100)
'''DO''' SWAP 1 +
SWAP 1 -
1 5 '''START'''
RAND OVER RE LASTARG IM / <
1 R→C +
'''NEXT'''
'''IF''' OVER 599 > 3 PICK 610 < AND '''THEN''' OVER 1 DISP DUP 2 DISP '''END'''
'''UNTIL''' DUP RE NOT '''END'''
IM "steps" →TAG SWAP "secs" →TAG
» '<span style="color:blue">STAIRS</span>' STO
{{out}}
<pre>
2: steps:16135
1: secs:3207
</pre>
Executing 10,000 tests is far beyond the computing power of a basic calculator.
 
=={{header|V (Vlang)}}==
{{trans|Wren}}
{{libheader|Vlang-rand}}
<syntaxhighlight lang="v (vlang)">import rand
fn main() {
Line 846 ⟶ 1,459:
println("\nAverage secs taken: ${total_secs/10000}")
println("Average final length of staircase: ${total_steps/10000}")
}</langsyntaxhighlight>
 
{{out}}
Line 871 ⟶ 1,484:
{{trans|C}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "random" for Random
import "./fmt" for Fmt
 
Line 898 ⟶ 1,511:
}
Fmt.print("\nAverage secs taken: $h", totalSecs/10000)
Fmt.print("Average final length of staircase: $h", totalSteps/10000)</langsyntaxhighlight>
 
{{out}}
Line 918 ⟶ 1,531:
Average secs taken: 2914.465
Average final length of staircase: 14672.325
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
int Trial, SecsTotal, StepsTotal; \keep track of time and steps over all trials
int SBehind, SLen, Wiz, Secs; \all variables related to individual trial
[SecsTotal:= 0; StepsTotal:= 0;
Print("Seconds steps behind steps ahead\n");
for Trial:= 1 to 10000 do \10000 attempts for the runner
[SBehind:= 0; SLen:= 100; Secs:= 0; \initialise this Trial
while SBehind < SLen do \as long as runner is still on stairs
[SBehind:= SBehind+1; \runner climbs a step
for Wiz:= 1 to 6 do \evil Wizard conjures five new steps
[if Ran(SLen) < SBehind then
SBehind:= SBehind+1; \maybe a new step is behind us
SLen:= SLen+1; \either way, the staircase is longer
];
Secs:= Secs+1; \one second has passed
if Trial=1 & 599<Secs & Secs<610 then
Print("%d %d %d\n", Secs, SBehind, SLen-SBehind);
];
SecsTotal:= SecsTotal + Secs;
StepsTotal:= StepsTotal + SLen;
];
Print("Average seconds taken: %f\n", float(SecsTotal)/10000.0);
Print("Average final length of staircase: %f\n", float(StepsTotal)/10000.0);
]</syntaxhighlight>
{{out}}
<pre>
Seconds steps behind steps ahead
600 2275 1425
601 2278 1428
602 2283 1429
603 2289 1429
604 2294 1430
605 2299 1431
606 2304 1432
607 2306 1436
608 2311 1437
609 2316 1438
Average seconds taken: 6625.81730
Average final length of staircase: 39854.90380
</pre>
1,150

edits