Long stairs: Difference between revisions

Add Factor
(Add Factor)
Line 57:
Average final length of staircase: 14707.466000
</pre>
 
=={{header|Factor}}==
<lang factor>USING: combinators.random io kernel math math.order prettyprint ;
 
: position ( -- n ) 0 ;
: stairs ( -- n ) 100 ;
: new ( -- m n ) position stairs ;
: incd ( m n -- o p ) swap 1 + swap ;
: seconds ( m -- n ) 100 - 5 / ;
: window? ( n -- ? ) seconds 600 609 between? ;
: zap ( m n -- o p ) 2dup / [ incd ] whenp 1 + ;
: barrage ( m n -- o p ) 5 [ zap ] times ;
: .n ( n -- ) pprint 7 [ bl ] times ;
: .stats ( m n -- ) dup seconds .n over .n swap - .n nl ;
: .header ( -- ) "Seconds behind ahead" print ;
: ?.status ( m n -- o p ) dup window? [ 2dup .stats ] when ;
: tick ( m n -- o p ) incd barrage ;
: demo ( m n -- o p ) tick ?.status ;
: sim-with ( q -- n ) new rot [ 2dup < ] swap while drop ; inline
: first ( -- n ) [ demo ] sim-with ;
: sim ( -- n ) [ tick ] sim-with ;
: sims ( -- n ) 0 first + 9999 [ sim + ] times ;
: steps ( m -- n ) "Avg. steps: " write 10000 / dup . ;
: time ( n -- ) "Avg. seconds: " write seconds . ;
: main ( -- ) .header sims nl steps time ;
 
main</lang>
{{out}}
<pre>
Seconds behind ahead
600 2067 1033
601 2073 1032
602 2078 1032
603 2082 1033
604 2087 1033
605 2091 1034
606 2096 1034
607 2099 1036
608 2103 1037
609 2108 1037
 
Avg. steps: 14731+23/200
Avg. seconds: 2926+223/1000
</pre>
And for fun, a version without stack effects.
<lang factor>USING: combinators.random effects.parser io kernel math
math.order parser prettyprint stack-checker words ;
 
<< SYNTAX: .: [ scan-new-word parse-definition dup infer ]
with-definition define-declared ; >>
 
.: position 0 ;
.: stairs 100 ;
.: new position stairs ;
.: incd swap 1 + swap ;
.: seconds 100 - 5 / ;
.: window? seconds 600 609 between? ;
.: zap 2dup / [ incd ] whenp 1 + ;
.: barrage 5 [ zap ] times ;
.: .n pprint 7 [ bl ] times ;
.: .stats dup seconds .n over .n swap - .n nl ;
.: .header "Seconds behind ahead" print ;
.: ?.status dup window? [ 2dup .stats ] when ;
.: tick incd barrage ;
.: demo tick ?.status ;
.: first new [ 2dup < ] [ demo ] while drop ;
.: sim new [ 2dup < ] [ tick ] while drop ;
.: sims 0 first + 9999 [ sim + ] times ;
.: steps "Avg. steps: " write 10000 / dup . ;
.: time "Avg. seconds: " write seconds . ;
.: main .header sims nl steps time ;
 
main</lang>
 
=={{header|Fermat}}==
1,808

edits