MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_071/sol1.py:22–44  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

20
21
22def 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
47if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected