Long stairs: Difference between revisions

julia example
(Add Factor)
(julia example)
Line 242:
190 PRINT TIMET/10000
200 PRINT STEPST/10000</lang>
 
=={{header|Julia}}==
<lang julia>""" https://rosettacode.org/wiki/Long_stairs """
 
using Statistics
 
struct LongStairs
startstep::Int
startlength::Int
climbersteps::Int
addsteps::Int
end
 
Base.length(LongStairs) = typemax(Int)
Base.eltype(ls::LongStairs) = Vector{Int}
 
function Base.iterate(s::LongStairs, param = (s.startstep, s.startlength))
pos, len = param
pos += s.climbersteps
pos += sum(pos .> rand(1:len, s.addsteps))
len += s.addsteps
return [pos, len], (pos, len)
end
 
ls = LongStairs(1, 100, 1, 5)
 
println("Seconds Behind Ahead\n----------------------")
for (secs, (pos, len)) in enumerate(collect(Iterators.take(ls, 609))[600:609])
println(secs + 599, " ", pos, " ", len - pos)
end
 
println("Ten thousand trials to top:")
times, heights = Int[], Int[]
for trial in 1:10_000
trialstairs = LongStairs(1, 100, 1, 5)
for (sec, (step, height)) in Iterators.enumerate(trialstairs)
if step >= height
push!(times, sec)
push!(heights, height)
break
end
end
end
@show mean(times), mean(heights)
</lang>{{out}}
<pre>
Seconds Behind Ahead
----------------------
601 1938 1167
602 1944 1166
603 1948 1167
604 1952 1168
605 1956 1169
606 1959 1171
607 1963 1172
608 1967 1173
609 1971 1174
Ten thousand trials to top:
(mean(times), mean(heights)) = (2927.0853, 14735.4265)
</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
4,105

edits