Long stairs: Difference between revisions

added RPL
m (→‎{{header|Wren}}: Changed to Wren S/H)
(added RPL)
 
(4 intermediate revisions by one other user not shown)
Line 186:
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}}==
Line 235 ⟶ 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}}==
Line 352 ⟶ 727:
3722083 / 250
</pre>
 
=={{header|FreeBASIC}}==
 
<syntaxhighlight 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</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|GW-BASIC}}==
<syntaxhighlight 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</syntaxhighlight>
 
=={{header|Go}}==
Line 1,100 ⟶ 1,407:
609 2187 958
Average seconds: 2716.0197, Average steps: 13677.143</pre>
 
=={{header|RPL}}==
« 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)}}==
Line 1,154 ⟶ 1,480:
Average final length of staircase: 14672.325
</pre>
 
=={{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|Wren}}==
1,150

edits