Mind-Boggling Card Trick
September 4, 2018
This is straight forward:
(define (trick) (let ((pack (shuffle (append (make-list 26 'B) (make-list 26 'R))))) (display "Pack: ") (display pack) (newline) (let loop ((pack pack) (blacks (list)) (reds (list)) (discards (list))) (if (pair? pack) (if (eq? (car pack) 'B) (loop (cddr pack) (cons (cadr pack) blacks) reds (cons (car pack) discards)) (loop (cddr pack) blacks (cons (cadr pack) reds) (cons (car pack) discards))) (begin (display "Blacks: ") (display blacks) (newline) (display "Reds: ") (display reds) (newline) (display "Discards: ") (display discards) (newline) (let ((swap (randint (min (length blacks) (length reds))))) (display "Swap size is ") (display swap) (newline) (let* ((blacks (shuffle blacks)) (reds (shuffle reds)) (bs (append (take swap reds) (drop swap blacks))) (rs (append (take swap blacks) (drop swap reds)))) (display "Blacks: ") (display bs) (newline) (display "Reds: ") (display rs) (newline) (let ((black-count (length (filter (lambda (x) (eq? x 'B)) bs))) (red-count (length (filter (lambda (x) (eq? x 'R)) rs)))) (display "Black count is ") (display black-count) (display "; ") (display "red count is ") (display red-count) (display ".") (newline) (if (= black-count red-count) (display "Black count equals red count.") (display "Black count does not equal red count."))))))))))
Here is a sample run:
> (trick) Pack: (B R B R B R R R R B B R B R B B R R R B R R B B B R B R R R B R R B B B R B R R B R B B B B R B R B R B) Blacks: (B B R B R R R B B R R R R R) Reds: (B B B R B B R R B R B R) Discards: (R R R B B B R R B R B R B B B R R R B B B R R B B B) Swap size is 11 Blacks: (R B R B R B B B B B R R R B) Reds: (B R B R R B B R R R R R) Black count is 8; red count is 8. Black count equals red count.
You can run the program at https://ideone.com/QCHUms. Can anyone provide an explanation of the trick?
This is indeed a magic trick:
The first phase is not random at all, because the operation ensures that there are the same number of black in the black stack as there are reds in the red stack. Discading the seen cards, ensures that there will remain the exact same number of cards (either of the same color, or the opposite color) to be moved to the other stack.
The second part is just for the spectacle, since it’s an operation that does not change that property (if two cards of the same color are exchanged, then no change; if cards of different colors are exchanged, then either it increments both counts (if the right color leaves each stack), or it decrements both counts (if red leaves the red stack and black leaves the black stack). Randomness does not matter, you could choose the cards you want, as long as you exchange the same number from both stacks :-)
This code almost reads like the assignment to me ;)
A Haskell version.
Here’s a solution in Python.