**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.

**Idea:**

Since this involves two digits, non-trivial, we know to start at 11 and go to 99, skipping numbers multiples of 10. Since the fraction is less than 1, the denominator is always going to be at least numerator + 1.

From there, just need to find duplicate numbers and cancel them, making sure the cancelled fraction is still less than one. Compare the two divisions (tricky given floating point number comparisons, but I ignored it in this case since they’re either exactly the same, or they’re not), and if they’re the same, multiply them to the existing numerators and denominators. After that, just reduce the fraction.

int answer = -1;
int answerTop = 1;
int answerBottom = 1;
for(int top = 11; top < 100; top++) {
if(top % 10 == 0) {
continue;
}
for(int bottom = top+1; bottom < 100; bottom++) {
if(bottom % 10 == 0) {
continue;
}
String sTop = String.valueOf(top);
String sBottom = String.valueOf(bottom);
//41/49 -> 1/9
if(sBottom.indexOf(sTop.charAt(0)) == 0) {
sBottom = sBottom.substring(1);
sTop = sTop.substring(1);
//48/74 -> 8/7
} else if(sBottom.indexOf(sTop.charAt(0)) == 1) {
sBottom = sBottom.substring(0,1);
sTop = sTop.substring(1);
//48/82 -> 4/2
} else if(sBottom.indexOf(sTop.charAt(1)) == 0) {
sBottom = sBottom.substring(1);
sTop = sTop.substring(0,1);
//49/89 -> 4/8
} else if(sBottom.indexOf(sTop.charAt(1)) == 1) {
sBottom = sBottom.substring(0,1);
sTop = sTop.substring(0,1);
//no common characters
} else {
continue;
}
if(sBottom.equals(sTop) || sTop.charAt(0) > sBottom.charAt(0)) {
continue;
}
double divide1 = (top*1.0)/bottom;
double divide2 = Double.parseDouble(sTop) /
Double.parseDouble(sBottom);
if(divide1 == divide2) {
answerTop *= top;
answerBottom *= bottom;
}
}
}
int gcd = BigInteger.valueOf(answerTop)
.gcd(BigInteger.valueOf(answerBottom)).intValue();
answer = answerBottom / gcd;
System.out.println("Answer: "+answer);