Search

Search
WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release
of Delphi (Delphi
10.1 Berlin Starter Edition ). There
are a few restrictions, but it is a welcome step toward making
more programmers aware of the joys of Delphi. They do say
"Offer may be withdrawn at any time", so don't delay if you want
to check it out. Please use the
feedback
link to let me know if the link stops working.

Support DFF - Shop

If you shop at Amazon anyway, consider
using this link.

We receive a few cents from each
purchase. Thanks

Support DFF - Donate

If you benefit from the website, in terms of
knowledge, entertainment value, or something otherwise useful,
consider making a donation via PayPal to help defray the
costs. (No PayPal account necessary to donate via credit
card.) Transaction is secure.

For over 15 years
Mensa Page-A-Day calendars have provided several puzzles a year
for my programming pleasure. Coding "solvers" is most fun,
but many programs also allow user solving, convenient for "fill
in the blanks" type. Below are Amazon links to the
two most recent years.

(Hint: If you can
wait, current year calendars are usually on sale in January.)

Contact

Feedback:Send an
e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

We'll each take a 52-card deck, shuffle it thoroughly and place our decks
face down on the table. Now we'll start turning cards face up, one from
the top of each deck on each turn. If we go through the entire deck
without two identical cards being turned, player A wins. If an identical
pair is turned, player B wins. Do you want to be player A or
B?

Some knowledge of derangements may help you choose.

A derangement of a set of numbers is a permutation in which no number
appears in its natural order. So, for example,
{5,4,1,2,3} is a derangement of {1,2,3,4,5}.
The number of derangements for a set of n objects is sometimes call the
subfactorial of n and denoted as !n.

There are number of algorithms for evaluating !n. The
simplest is derived from the equation: !n= n! x sumk=0 to n ((-1)k/k!)
where n! is n factorial, the product of integers 1
through n. It just so happens that the series
represented by sumk=0 to n ((-1)k/k!)
approaches 1/e as k approaches infinity (yup, the
same irrational number e (2.718281...) that is the base of natural
logarithms).

So the easy way to calculate !n is as round(n!/e), n
factorial divided by e rounded to the nearest integer.

Delphi does not have e as a predefined constant (it does have pi,
but discriminates against e for some reason). It has the Exp(x)
function, e raised to the x power, though. So defining
e as exp(1.0) is pretty trivial.

Back to the original card game problem. The "natural order"
of a set can be any predetermined order. In this case we'll let deck
number 1 define the natural order, and deck number 2 will be a derangement if no
card in deck 2 matches it's position in deck 1. Player A wins
in this case, Player B wins in deck 2 is not a derangement of deck
1. The probability of an event is the number of
"successful" outcomes divided by the total number of possible
outcomes. In this case the total possible outcomes number 52!
and the successful (derangement) outcomes is round(52!/e) which will be
very close to 52!/e. So the probability of no match will be
close to (52!/e)/52! = 1/e or about 1/2.71828=0.36788.
We can therefore conclude that A has about a 37% chance of winning and, since
somebody will win, B has a 63% chance of winning. I did
the proof, so I get to choose first whether to be A or B and you are probably
going to lose!

According to our Big Factorials program
52! has 68 digits, so I used a smaller problem in the program to reach the same
conclusion. The program provided, calculates actual derangement counts for
N in the range of 1 to 10 and points out that the probability approaches 1/e as
N gets larger.

The program will also display the first 100 derangements for the selected
N. I could find no reference to a function that would generate
derangements directly. So I fell back on our old faithful UCombos
unit to generate permutations and then just weed out those that are not
derangements.