|
1 | 1 | """
|
2 |
| -Problem: |
| 2 | +Problem 33: https://projecteuler.net/problem=33 |
3 | 3 |
|
4 | 4 | The fraction 49/98 is a curious fraction, as an inexperienced
|
5 | 5 | mathematician in attempting to simplify it may incorrectly believe
|
|
14 | 14 | If the product of these four fractions is given in its lowest common
|
15 | 15 | terms, find the value of the denominator.
|
16 | 16 | """
|
| 17 | +from fractions import Fraction |
| 18 | +from typing import List |
17 | 19 |
|
18 | 20 |
|
19 |
| -def isDigitCancelling(num, den): |
| 21 | +def is_digit_cancelling(num: int, den: int) -> bool: |
20 | 22 | if num != den:
|
21 | 23 | if num % 10 == den // 10:
|
22 | 24 | if (num // 10) / (den % 10) == num / den:
|
23 | 25 | return True
|
| 26 | + return False |
24 | 27 |
|
25 | 28 |
|
26 |
| -def solve(digit_len: int) -> str: |
| 29 | +def fraction_list(digit_len: int) -> List[str]: |
27 | 30 | """
|
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' |
| 31 | + >>> fraction_list(2) |
| 32 | + ['16/64', '19/95', '26/65', '49/98'] |
| 33 | + >>> fraction_list(3) |
| 34 | + ['16/64', '19/95', '26/65', '49/98'] |
| 35 | + >>> fraction_list(4) |
| 36 | + ['16/64', '19/95', '26/65', '49/98'] |
| 37 | + >>> fraction_list(0) |
| 38 | + [] |
| 39 | + >>> fraction_list(5) |
| 40 | + ['16/64', '19/95', '26/65', '49/98'] |
38 | 41 | """
|
39 | 42 | solutions = []
|
40 | 43 | den = 11
|
41 | 44 | last_digit = int("1" + "0" * digit_len)
|
42 | 45 | for num in range(den, last_digit):
|
43 | 46 | while den <= 99:
|
44 | 47 | if (num != den) and (num % 10 == den // 10) and (den % 10 != 0):
|
45 |
| - if isDigitCancelling(num, den): |
| 48 | + if is_digit_cancelling(num, den): |
46 | 49 | solutions.append(f"{num}/{den}")
|
47 | 50 | den += 1
|
48 | 51 | num += 1
|
49 | 52 | den = 10
|
50 |
| - solutions = " , ".join(solutions) |
51 | 53 | return solutions
|
52 | 54 |
|
53 | 55 |
|
| 56 | +def solution(n: int = 2) -> int: |
| 57 | + """ |
| 58 | + Return the solution to the problem |
| 59 | + """ |
| 60 | + result = 1.0 |
| 61 | + for fraction in fraction_list(n): |
| 62 | + frac = Fraction(fraction) |
| 63 | + result *= frac.denominator / frac.numerator |
| 64 | + return int(result) |
| 65 | + |
| 66 | + |
54 | 67 | if __name__ == "__main__":
|
55 |
| - print(solve(2)) |
| 68 | + print(solution()) |
0 commit comments