Anonymous user
Long stairs: Difference between revisions
m
→{{header|Free Pascal}}: new way of calculation without random.Fits much better to random results
(Python example) |
m (→{{header|Free Pascal}}: new way of calculation without random.Fits much better to random results) |
||
Line 305:
=={{header|Pascal}}==
==={{header|Free Pascal}}===
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.
<lang pascal>program WizardStaircase;
const
Line 316 ⟶ 318:
end;
function OneRun(StairsPerSpell:Cardinal;WithOutput:boolean):longword;
var
inFront,behind,total,i,trials:
begin
trials := 0;
Line 329 ⟶ 331:
dec(infront);
//spell
if random(total)
inc(behind)
else
inc(infront);
inc(total);
until i=0;
if WithOutput then
begin
Line 346 ⟶ 349:
end;
procedure CheckDouble(StairsPerSpell:Cardinal);
var
behind,infront,relpos,total,One,relSpell : double;▼
▲ behind,infront,relpos,total,One : double;
▲ i : nativeInt;
begin
write(StairsPerSpell:3);
One := 1.0;
behind := 0.0;
inFront := StartStairLength;
total := StartStairLength;
repeat
//doing a partial move per one stair per spell
behind += One;▼
relpos := (behind+relSpell)/total;
behind := behind + relpos +relSpell;
▲ For i := StairsPerSpell-1 downto 0 do
inFront := inFront+(One-relpos)-relSpell;
begin▼
until
total += One;▼
end;▼
until infront < One;
writeln((total-StartStairLength)/StairsPerSpell:10:
end;
var
i,
mySpell,
attempt,
minStairs,
Line 380 ⟶ 382:
total : longword;
begin
randomize;
writeln('Seconds steps total behind ahead');
total := OneRun(StairsPerSpell,true);
writeln(' average stairs needed steps minimum maximum');▼
For i := rounds-1 downto 0 do▼
for mySpell := 1 to 7 do
begin
if minStairs>attempt then▼
For
▲ begin
attempt:= OneRun(mySpell,false);
▲ if minStairs>attempt then
minstairs := attempt;
if maxStairs<attempt then
maxstairs := attempt;
▲ end;
writeln((total-StartStairLength)/rounds/mySpell:12:3,total/rounds:14:3,minStairs:9,maxStairs:10);▼
end;
▲ writeln(' average stairs minimum maximum');
▲ writeln(total/rounds:14:3,minStairs:9,maxStairs:10);
writeln;
writeln(' calculated ');
CheckDouble(i);
end.</lang>
{{Out|@ TIO.RUN}}
<pre>
Seconds steps total behind ahead
600 3100
601 3105
602 3110
603 3115
604 3120
605 3125
606 3130
607 3135
608 3140
609 3145
1
2 367.570 735.150 540 954
3 663.998 1992.003 1234 2911
4 1353.416 5413.674 2748 8980
5 2933.418 14667.099 6715 27210
6 6648.557 39891.354 18394 80050
7 15550.874 108856.130 42380 218892
calculated
1 170 270
2 317 734
3 633 1999
4 1333 5432
5 2933 14765
6 6673 40138
7 15573 109111
8 37063 296604
9 89573 806257
10 219154 2191640
Real time: 55.269 s</pre>
=={{header|Phix}}==
|