Long stairs: Difference between revisions

No change in size ,  2 months ago
Grouping BASIC dialects
(Added various BASIC dialects(QBasic, BASIC256, Yabasic and True BASIC)))
(Grouping BASIC dialects)
Line 219:
{{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|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|QBasic}}===
Line 278 ⟶ 345:
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}}===
Line 474 ⟶ 612:
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,276 ⟶ 1,346:
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}}==
2,131

edits