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: longwordCardinal;
begin
trials := 0;
Line 329 ⟶ 331:
dec(infront);
//spell
for i := 1 to StairsPerSpell do;
beginrepeat
if random(total)+1 <= behind then
inc(behind)
else
inc(infront);
inc(total);
end dec(i);
until i=0;
if WithOutput then
begin
Line 346 ⟶ 349:
end;
 
procedure CheckDouble(StairsPerSpell:Cardinal);
//instead of random use ratio behind/total
var
behind,infront,relpos,total,One,relSpell : double;
//xmm in freepascal using SSE
i : nativeIntLongInt;
behind,infront,relpos,total,One : double;
i : nativeInt;
begin
write(StairsPerSpell:3);
One := 1.0;
behind := 0.0;
inFront := StartStairLength;
total := StartStairLength;
For irelSpell := One/StairsPerSpell-1 downto 0 do;
repeat
//firsti := doing the stepStairsPerSpell;
//doing a partial move per one stair per spell
behind += One;
inFront -= One;repeat
relpos := (behind+relSpell)/total;
//second: doing the spell
behind := behind + relpos +relSpell;
For i := StairsPerSpell-1 downto 0 do
inFront := inFront+(One-relpos)-relSpell;
begin
relpostotal := behind/total+One;
behind += relposdec(i);
until inFront +i= (One-relpos)0;
total += One;
end;
until infront < One;
writeln((total-StartStairLength)/StairsPerSpell:10:20,total:14:0);
end;
 
var
i,
mySpell,
attempt,
minStairs,
Line 380 ⟶ 382:
total : longword;
begin
minStairs := High(minStairs);
maxStairs := low(maxStairs);
randomize;
writeln('Seconds steps total behind ahead');
total := OneRun(StairsPerSpell,true);
 
total := 0;
writeln(' average stairs needed steps minimum maximum');
For i := rounds-1 downto 0 do
for mySpell := 1 to 7 do
begin
attemptwrite(mySpell:=3,' OneRun(false ');
behindtotal +:= One0;
if minStairs>attempt then
minstairsminStairs := attemptHigh(minStairs);
if maxStairs<attempt then:= low(maxStairs);
For maxstairsi := attempt;rounds-1 downto 0 do
begin
inc(total,attempt);
attempt:= OneRun(mySpell,false);
if minStairs>attempt then
minstairs := attempt;
if maxStairs<attempt then
maxstairs := attempt;
inc(total += One,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((total-StartStairLength)/rounds/StairsPerSpell:10:3,' average needed seconds');
writeln(' calculated ');
For i := rounds-1 downtoto 010 do
writeln(' stairs without random ');
CheckDouble(i);
end.</lang>
</lang>
{{Out|@ TIO.RUN}}
<pre>
 
Seconds steps total behind ahead
600 3100 20212260 1079 840
601 3105 20272264 1078 841
602 3110 20312269 1079 841
603 3115 20362275 1079 840
604 3120 20392281 1081 839
605 3125 20432285 1082 840
606 3130 20482291 1082 839
607 3135 20522295 1083 840
608 3140 20552300 1085 840
609 3145 20592302 1086 843
average stairs needed steps minimum maximum
1 14691 271.605090 271.100 239 7655 26770313
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
2938.319 average needed seconds
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>
stairs without random
14470.00
</pre>
 
=={{header|Phix}}==
Anonymous user