|
| 1 | +""" |
| 2 | +Problem: |
| 3 | +
|
| 4 | +The fraction 49/98 is a curious fraction, as an inexperienced |
| 5 | +mathematician in attempting to simplify it may incorrectly believe |
| 6 | +that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s. |
| 7 | +
|
| 8 | +We shall consider fractions like, 30/50 = 3/5, to be trivial examples. |
| 9 | +
|
| 10 | +There are exactly four non-trivial examples of this type of fraction, |
| 11 | +less than one in value, and containing two digits in the numerator |
| 12 | +and denominator. |
| 13 | +
|
| 14 | +If the product of these four fractions is given in its lowest common |
| 15 | +terms, find the value of the denominator. |
| 16 | +""" |
| 17 | + |
| 18 | + |
| 19 | +def isDigitCancelling(num, den): |
| 20 | + if num != den: |
| 21 | + if num % 10 == den // 10: |
| 22 | + if (num // 10) / (den % 10) == num / den: |
| 23 | + return True |
| 24 | + |
| 25 | + |
| 26 | +def solve(digit_len: int) -> str: |
| 27 | + """ |
| 28 | + >>> solve(2) |
| 29 | + '16/64 , 19/95 , 26/65 , 49/98' |
| 30 | + >>> solve(3) |
| 31 | + '16/64 , 19/95 , 26/65 , 49/98' |
| 32 | + >>> solve(4) |
| 33 | + '16/64 , 19/95 , 26/65 , 49/98' |
| 34 | + >>> solve(0) |
| 35 | + '' |
| 36 | + >>> solve(5) |
| 37 | + '16/64 , 19/95 , 26/65 , 49/98' |
| 38 | + """ |
| 39 | + solutions = [] |
| 40 | + den = 11 |
| 41 | + last_digit = int("1" + "0" * digit_len) |
| 42 | + for num in range(den, last_digit): |
| 43 | + while den <= 99: |
| 44 | + if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): |
| 45 | + if isDigitCancelling(num, den): |
| 46 | + solutions.append("{}/{}".format(num, den)) |
| 47 | + den += 1 |
| 48 | + num += 1 |
| 49 | + den = 10 |
| 50 | + solutions = " , ".join(solutions) |
| 51 | + return solutions |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + print(solve(2)) |
0 commit comments