Robert Eisele
Engineer, Systems Architect and DBA

# Project Euler 33: Digit cancelling fractions

Problem 33

The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.

## Solution

Let the resulting nominator and denominator be $$1\leq n<d\leq 9$$ with $$n,d\in\mathbb{N}$$ and the variable to be canceled $$1\leq c \leq 9$$ with $$c\in\mathbb{N}$$. This ensures that $$\frac{n}{d}<1$$. We can now find four combinations of possible equations:

$$$\frac{10n+c}{10d+c}=\frac{n}{d}$$$ $$$\frac{10c+n}{10c+d}=\frac{n}{d}$$$ $$$\frac{10c+n}{10d+c}=\frac{n}{d}$$$ $$$\frac{10n+c}{10c+d}=\frac{n}{d}$$$

Let's go through these equations:

$\begin{array}{rl} & \frac{10n+c}{10d+c}=\frac{n}{d}\\ \Leftrightarrow & 10dn+cd = 10dn + cn\\ \Leftrightarrow & d = n \, \unicode{x21af} \end{array}$

The first equation doesn't have a solution due to the contradiction occuring because $$n<d$$ from the prerequisite. Going to the second equation:

$\begin{array}{rl} & \frac{10c+n}{10c+d}=\frac{n}{d}\\ \Leftrightarrow & 10cd+dn = 10cn+dn\\ \Leftrightarrow & d=n \, \unicode{x21af} \end{array}$

The second equation doesn't have a solution for the same reason as the first equation. Let's investigate the third equation:

$\begin{array}{rl} & \frac{10c+n}{10d+c}=\frac{n}{d}\\ \Leftrightarrow & 10cd+dn = 10dn+cn\\ \Leftrightarrow & 9dn = 10cd-cn \\ \Leftrightarrow & 9dn = 9cd-c(d-n)\\ \Leftrightarrow & 9d(n-c)=c(d-n) \end{array}$

As $$n< d$$ from the prerequisite must be true, the right hand side must be positive. Therefore the left hand side must also be positive, whereby $$c< n$$. When rearranging the equation again:

$n-c=\frac{c}{9}-\frac{cn}{9d}$

We said the left hand side must be positive and additional an integer. As $$c<9$$ it follows that $$\frac{c}{9}<1$$. Subtracting $$\frac{cn}{9d}$$ from this small value makes it even smaller or negative, from which follows that this equation doen't have a solution as well. Let's investigate the last equation:

$\begin{array}{rl} & \frac{10n+c}{10c+d}=\frac{n}{d}\\ \Leftrightarrow & 10dn+cd = 10cn+dn\\ \Leftrightarrow & 9dn = 10cn-cd\\ \Leftrightarrow & 9n(d-c) = c(n-d) \\ \Leftrightarrow & 9n(c-d) = c(d-n) \end{array}$

The right hand side is the same as before and must be positive again, whereby the left hand side must be positive as well. This spans our solution space to $$n < d < c \leq 9$$. Solving the problem is quite easy now:

function solution() {

var dp = 1;
var np = 1;

for (var c = 1; c <= 9; c++) {
for (var d = 1; d < c; d++) {
for (var n = 1; n < d; n++) {
if ((n * 10 + c) * d === (c * 10 + d) * n) {
np*= n;
dp*= d;
}
}
}
}
return dp / gcd(np, dp);
}


That's pretty nice, we solve the problem with 84 iterations. But we can go even further and solve the whole thing by hand. Starting with our last equation:

$\begin{array}{rl} & 9n(c-d) = c(d-n)\\ \Leftrightarrow & n(c - d) = \frac{1}{9} c(d - n)\\ \end{array}$

In order to make $$c(d - n)$$ divisible by 9, $$c$$ can only be 3, 6 or 9. Let's elaborate on these three cases:

Case 1 if $$c=3$$:
$$d-n$$ can be 3 or 6, which is not possible. Since $$n<d<3$$, only $$n=1, d=2$$ would be possible, which are not.

Case 2 if $$c=6$$:
$$d-n$$ can be 3 or 6. However, $$d - n$$ can't be 6, since $$d<n<6$$. Therefore $$d - n$$ can only be 3, resulting in 2 solutions: $$(n = 1, d = 4), (n = 2, d = 5)$$.

Case 3 if $$c=9$$:

$\begin{array}{rl} & n(9 - d) = \frac{1}{9} 9(d - n)\\ \Leftrightarrow & 9n - nd = d-n\\ \Leftrightarrow & 10n-nd = d\\ \Leftrightarrow & n(10 - d) = d\\ \Leftrightarrow & n = \frac{d}{10-d} \end{array}$

In order to let $$n$$ positive, $$5\leq d\leq 9$$ must be true. The only possible ways to make $$n$$ an integer is $$d=5$$ and $$d=8$$. With that, the following solutions for the whole problem can be collected:

$(c=6, n = 1, d = 4)\\ (c=6, n = 2, d = 5)\\ (c=9, n = 1, d = 5)\\ (c=9, n = 4, d = 8)$

...which results in the following fractions:

$\frac{16}{64} = \frac{1}{4}, \frac{26}{65} = \frac{2}{5}, \frac{19}{95} = \frac{1}{5}, \frac{49}{98} = \frac{4}{8}$

Multiplying the denominators together and divide by the gcd according to the JavaScript implementation above, yields the same result.

Go to overview