Returns the closest numerator of the fraction immediately to the left of given fraction (numerator/denominator) from a list of reduced proper fractions. >>> solution() 428570 >>> solution(3, 7, 8) 2 >>> solution(6, 7, 60) 47
(numerator: int = 3, denominator: int = 7, limit: int = 1000000)
| 20 | |
| 21 | |
| 22 | def solution(numerator: int = 3, denominator: int = 7, limit: int = 1000000) -> int: |
| 23 | """ |
| 24 | Returns the closest numerator of the fraction immediately to the |
| 25 | left of given fraction (numerator/denominator) from a list of reduced |
| 26 | proper fractions. |
| 27 | >>> solution() |
| 28 | 428570 |
| 29 | >>> solution(3, 7, 8) |
| 30 | 2 |
| 31 | >>> solution(6, 7, 60) |
| 32 | 47 |
| 33 | """ |
| 34 | max_numerator = 0 |
| 35 | max_denominator = 1 |
| 36 | |
| 37 | for current_denominator in range(1, limit + 1): |
| 38 | current_numerator = current_denominator * numerator // denominator |
| 39 | if current_denominator % denominator == 0: |
| 40 | current_numerator -= 1 |
| 41 | if current_numerator * max_denominator > current_denominator * max_numerator: |
| 42 | max_numerator = current_numerator |
| 43 | max_denominator = current_denominator |
| 44 | return max_numerator |
| 45 | |
| 46 | |
| 47 | if __name__ == "__main__": |