TITLE: When the product of dice rolls yields a square
QUESTION [28 upvotes]: Succinct Question:
Suppose you roll a fair six-sided die $n$ times.
What is the probability that the product of the rolls is a square?
Context:
I used this as one question in a course for elementary school teachers when $n=2$, and thought the generalization might be a good follow-up question for secondary school math teachers. But I encountered quite a bit of difficulty in tackling it, and I am wondering if there is a neater solution than what I have already seen, and to what deeper phemonena it connects.
Known:
Since the six sides of a die are $1, 2, 3, 2^2, 5,$ and $2\cdot3$, the product of the rolls is always of the form $2^{A}3^{B}5^{C}$, and the question is now transformed into the probability that $A, B, C$ are all even. The actual "probability" component is mostly for ease of phrasing; its only contribution is a $6^n$ in the denominator, and my true question is of a more combinatorial nature: namely,

In how many ways can the product of $n$ rolls yield a square?

One approach that I have seen involves first creating an $8 \times 8$ matrix corresponding to the eight cases around the parity of $A, B, C$; one can then take the dot product of each roll with this matrix, and hope to spot a pattern. In this way, one may discover the formula:
$$\frac{6^n + 4^n + 3\cdot2^n}{8}$$
and the "probability" version is simply this formula with another $6^n$ multiplied in the denominator.
As for proving this: Some guesswork around linear combinations of the numerator yields a formula for each of the eight cases concerning $A,B,C$ parity, and one can then prove all eight of them by induction. And so I "know" the answer in the sense that I have all eight of the formulae (and the particular one listed above is correct) but they were not found in a particularly organized fashion.

My Actual Question:
What is a systematic way to deduce the formula, given above, for the number of ways the product of $n$ rolls yields a square, and to what deeper phenomena does this connect?

REPLY [5 votes]: There's a slick approach to this based on bijections, though it loses a lot of the generality of the generating function methods.
Let $S=\{1,2,3,6\}$ and $T=\{4,5\}$.  We will divide roll sequences into classes based on whether the sequence contains elements from $S, T,$ or both.
Class 1: Sequences consisting only of rolls in $T$.  Swapping the first die roll between $4$ and $5$ gives a bijection between squares and non-squares, so exactly half the sequences in this class give a square.
Class 2: Sequences consisting only of rolls in $S$.  Now we can divide the sequences into groups of $4$ that share the same last $n-1$ rolls.  Each group has one square product, so exactly $1/4$ the sequences in this class give a square.
Class 3: Sequences containing both a roll in $S$ and a roll in $T$.  To each sequence we assign a "type", consisting of (1): The location of the first roll in $S$, (2): the location of the first roll in $T$, and (3): The remaining $n-2$ rolls.  Once the type is fixed, there's $8$ choices for the remaining roll, and exactly one of them gives a square.  
So the number of square sequences is 
$$\frac{1}{2} |\textrm{Class } 1| + \frac{1}{4} |\textrm{Class } 2| + \frac{1}{8} |\textrm{Class } 3| = \frac{1}{2} 2^n + \frac{1}{4} 4^n + \frac{1}{8} (6^n-2^n-4^n)$$