Problem 33 in Project Euler says there are 4 non-trivial curious fractions which satisfy the following properties:
where a, b and x are all single digit numbers and a < b
The solution was pretty simple.
Similarly solving for the other form gives the answer
.
Implementing this solution in java:
public class Problem_33 {
/**
* @return All the proper non-trivial curious fractions having numerator, denominator < 100
*/
public static List<Fraction> getCuriousFractions() {
List<Fraction> curiousFractions = new ArrayList<Fraction>();
for (int a = 1; a < 10; a++)
for (int b = a + 1; b < 10; b++)
if (Problem_33.checkCondition(a, b))
curiousFractions.add(new Fraction(a, b));
return curiousFractions;
}
/**
* Checks if the number is of the form xa/bx = a/b or ax/xb = a/b
*
* @param a The numerator of the fraction
* @param b The denominator of the fraction
* @return true if it can be formed into a curious fraction, false otherwise
*/
private static boolean checkCondition(int a, int b) {
int numerator = 9 * a * b;
int denominatorA = 10 * b - a;
int denominatorB = 10 * a - b;
return numerator % denominatorA == 0 && numerator / denominatorA < 10 || numerator % denominatorB == 0
&& numerator / denominatorB < 10 ? true : false;
}
}
Popularity: 9% [?]