Mind boggling card trick: Difference between revisions

Added XPL0 example.
(New post.)
(Added XPL0 example.)
Line 2,234:
The number of black cards in the black stack = 4
So the asssertion is correct!
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
char Deck(52), BlackPile(52), RedPile(52), DiscardPile(52),
BlackBunch(52), RedBunch(52);
int I, J, T, M, X, Y, BP, RP, DP, BB, RB, BC, RC;
 
proc Show;
[Print("Black pile: ");
for I:= 0 to BP-1 do ChOut(0, BlackPile(I));
Print("\nRed pile: ");
for I:= 0 to RP-1 do ChOut(0, RedPile(I));
Print("\nDiscard pile: ");
for I:= 0 to DP-1 do ChOut(0, DiscardPile(I));
Print("\n");
];
 
[for I:= 0 to 26-1 do
[Deck(I):= ^r; Deck(I+26):= ^b];
for I:= 0 to 52-1 do
[Y:= Ran(52); \0..51
T:= Deck(I); Deck(I):= Deck(Y); Deck(Y):= T;
];
BP:= 0; RP:= 0; DP:= 0;
for I:= 0 to 52-1 do
[if Deck(I) = ^b then
[BlackPile(BP):= Deck(I+1); BP:= BP+1]
else
[RedPile (RP):= Deck(I+1); RP:= RP+1];
DiscardPile(DP):= Deck(I); DP:= DP+1;
I:= I+1;
];
Show;
M:= BP;
if RP < M then M:= RP;
X:= Ran(M) + 1;
Print("Swap %d cards between the red and black piles.\n", X);
RB:= 0; BB:= 0;
for I:= 0 to X-1 do
[repeat Y:= Ran(RP); until RedPile(Y) # 0;
RedBunch(RB):= RedPile(Y); RB:= RB+1; RedPile(Y):= 0;
];
for I:= 0 to X-1 do
[repeat Y:= Ran(BP); until BlackPile(Y) # 0;
BlackBunch(BB):= BlackPile(Y); BB:= BB+1; BlackPile(Y):= 0;
];
RB:= 0;
for I:= 0 to X-1 do
[J:= 0;
while BlackPile(J) # 0 do J:= J+1;
BlackPile(J):= RedBunch(RB); RB:= RB+1;
];
BB:= 0;
for I:= 0 to X-1 do
[J:= 0;
while RedPile(J) # 0 do J:= J+1;
RedPile(J):= BlackBunch(BB); BB:= BB+1;
];
Show;
BC:= 0;
for I:= 0 to BP-1 do
if BlackPile(I) = ^b then BC:= BC+1;
RC:= 0;
for I:= 0 to RP-1 do
if RedPile(I) = ^r then RC:= RC+1;
Print("The number of black cards in the black pile is %d.\n", BC);
Print("The number of red cards in the red pile is %d.\n", RC);
Print("The mathematician's assertion is%s correct.\n",
if BC#RC then " not" else "");
]</syntaxhighlight>
{{out}}
<pre>
Black pile: rrrrrbbbrbbbr
Red pile: brrrbbbrrrbbb
Discard pile: brrrrrbbbbrbrrbrbbrbbrbrbr
Swap 11 cards between the red and black piles.
Black pile: bbrrbbbbrbrbr
Red pile: brbrrrrrbrbbr
Discard pile: brrrrrbbbbrbrrbrbbrbbrbrbr
The number of black cards in the black pile is 8.
The number of red cards in the red pile is 8.
The mathematician's assertion is correct.
</pre>
 
295

edits