Let's say a person with perfect memory is playing the card-matching game Concentration. He/she randomly lays out 2n cards (ie. n card pairs) sorted randomly and repeatedly takes turns consisting of flipping over two cards, without looking at the first card. When the cards match they are removed from play. When cards do not match, they are turned back over. The player wins when all pairs are found and all cards are removed.
A perfect game is played in just n turns, but even a perfect player (one with perfect memory) couldn't hope to come close to a perfect game as there would still be a lot of guessing required.
How to you calculate the expected number of turns required to win for player with perfect memory?
EDIT 2 -- clarifying the rules
Since all the early answers seem to read this question as you CANNOT look at the first card, I'm editing that into the rules.
I originally was thinking you CAN look at the first card (since that's how I'd learned the game and, to my knowledge, that is how it is usually played.
For that rule adjustment, I've posted a new question here: How many turns, on average, does it take for a perfect player to win Concentration (adjusted)?
EDIT -- Checking answers against a simulation
The strategy proposed in AlgorithmsX's answer seems to make sense to me - that is, the game takes place in two distinct stages.
Stage 1: flip all cards to learn where they are, and...
Stage 2: clean up/perfectly find all matches (since you know where they are).
Thus improving on a $2n$ turn game is pure chance; how many matches do you find in Stage 1?
To check a given answer, I wrote the below code to simulate the problem. It just randomly shuffles an array with pairs from $0$ to $n-1$ and checks for adjacent pairs that would come up in the sweep during Stage 1 (so a 0-1 pair is a match, but 1-2 is not). This seems like a good way to check answers because (for me personally, at least) it's easier to reason about the simulation than the math.
Currently, AlgorithmsX's answer for $n=3$ results in $5.6$, but I would expect it to be $5.4$ barring any errors in my simulation.
Some simulated results (1 million trials)
n | 2 | 3 | 4 | 5 | 6 |
turns | 3.33 | 5.40 | 7.43 | 9.44 | 11.45 |
Code to simulate answer given $n$
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var simulation = function(n, nTrials){
// make array from 0 to n-1 with pairs
var cards = Array.apply(null, {length: n}).map(Number.call, Number);
cards = cards.concat(cards);
var totalTurns = 0;
var trialsLeft = nTrials;
while(trialsLeft>0){
cards = shuffle(cards)
var matches = 0;
for(var i=0; i<n; i++){
if(cards[2*i]===cards[2*i+1]){
matches++
}
}
totalTurns += 2*n-matches;
trialsLeft--
}
return totalTurns/nTrials;
}
A player with perfect memory will flip over cards in two distinct stages. First, he will flip over all the cards and find out what they are. Second, he will flip over all the matching cards. There is no difference in flipping over two cards that you know match as soon as you know they match and flipping over two cards that you know match after flipping all the cards over at least once. If he finds no matches in the first stage, then the total number of turns will be $2n$, because he flips over $n$ cards in the first stage and $n$ cards in the second stage. The only way for him to cut this time down is to find a match in the first stage. Ignore Everything Until the Second Edit The probability of this player picking a match on his first stage is the number of matches, $n$, divided by the number of ways to pick two matching cards out of $2n$ cards, which is $\frac{2n*(2n-1)}2=n*(2n-1)$. The probability of picking one match is therefore: $$\frac1{2n-1}$$ If you pick a match before you are finished with the first stage, then you can consider the same problem, but with $n-1$ instead of $n$ because that match is, in essence, removed. The probability of picking the second match is therefore: $$\frac1{2n-3}*\frac1{2n-1}$$ You can extend this reasoning to finding the third and greater number of matches. The expected number of matches found in the first string is therefore: $$1*\frac1{2n-1}+2*\frac1{2n-3}*\frac1{2n-1}+3*\frac1{2n-5}*\frac1{2n-3}*\frac1{2n-1}...$$ This means that you have to find the expected number of matches fewer than $n$ in the second stage, so the expected number of turns is: $$2n-(1*\frac1{2n-1}+2*\frac1{2n-3}*\frac1{2n-1}+3*\frac1{2n-5}*\frac1{2n-3}*\frac1{2n-1}...)$$ Finally, it is important to realize that if you find $n-1$ out of $n$ matches, you will have the last match left, so as soon as you reach the $(n-1)$th case, add one to the number of matches. For $n=2$, instead of having $2n-(1*\frac1{2n-1})$, you would have $2n-(2*\frac1{2n-1})$. For $n=3$, you would have $$2n-(1*\frac1{2n-1}+3*\frac1{2n-3}*\frac1{2n-1})$$
Edit: In my example where $n=3$, the $3*\frac1{2n-3}*\frac1{2n-1}$ part is accurate. However, the $1*\frac1{2n-1}$ part is not because I forgot to account for how many different ways there are to arrange the unmatched pairs. In this case, the number of possible combinations would be $4$, as Jens pointed out. I do not yet have a way to adjust for this.
Edit: Based on your results from your simulation, it seems that the expected number of turns has a formula of $$2n-\frac n{2n-1}$$ but I have no idea how to prove that.
Edit: This algorithm should work for the case in which a player can look at a card before flipping it over.
This algorithm should use up the fewest turns possible for this set up. The probability that the first card in one turn matches any of the previous cards is $\frac d m$, where $d$ is the number of distinct, unmatched cards already flipped over and $m$ is the number of remaining cards at the star of each turn. The probability that the second card matches the first card is $\frac 1 {m-1}$. The probability that the second card matched any of the previous cards but not the first card is $\frac {d-2} {m-1}$. The variable $d$ starts at $0$, increases by $1$ every time a card is flipped over and decreases by $2$ every time a match is found. The variable $m$ starts out as $2n$ and decreases by $1$ whenever a card is flipped over. From this, the question becomes "How many matches will we find on the first flip of a turn and how many matches will we find in a single turn?" Every match we find on the first flip or during a turn lowers the number of turns by $1$.