From 7d0d77334f104e8f6f68cfb102ecae3505341e3a Mon Sep 17 00:00:00 2001 From: kalpanajangra <72243101+kalpanajangra@users.noreply.github.com> Date: Thu, 8 Oct 2020 17:25:23 +0530 Subject: [PATCH] Add Project Euler Problem 71: Fixes #2695 (#2785) --- project_euler/problem_71/__init__.py | 0 project_euler/problem_71/sol1.py | 48 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 project_euler/problem_71/__init__.py create mode 100644 project_euler/problem_71/sol1.py diff --git a/project_euler/problem_71/__init__.py b/project_euler/problem_71/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/project_euler/problem_71/sol1.py b/project_euler/problem_71/sol1.py new file mode 100644 index 000000000..415b127e5 --- /dev/null +++ b/project_euler/problem_71/sol1.py @@ -0,0 +1,48 @@ +""" +Ordered fractions +Problem 71 +https://projecteuler.net/problem=71 + +Consider the fraction n/d, where n and d are positive +integers. If n int: + """ + 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 + """ + max_numerator = 0 + max_denominator = 1 + + for current_denominator in range(1, limit + 1): + current_numerator = current_denominator * numerator // denominator + if current_denominator % denominator == 0: + current_numerator -= 1 + if current_numerator * max_denominator > current_denominator * max_numerator: + max_numerator = current_numerator + max_denominator = current_denominator + return max_numerator + + +if __name__ == "__main__": + print(solution(numerator=3, denominator=7, limit=1000000))