From 589b45f790b28eb25fb1e285d76feccee0bc2490 Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Tue, 13 Oct 2020 14:43:49 +0200 Subject: [PATCH 1/4] include solution for problem 57 --- project_euler/problem_57/__init__.py | 0 project_euler/problem_57/sol1.py | 45 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 project_euler/problem_57/__init__.py create mode 100644 project_euler/problem_57/sol1.py diff --git a/project_euler/problem_57/__init__.py b/project_euler/problem_57/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_57/sol1.py b/project_euler/problem_57/sol1.py new file mode 100644 index 000000000000..d4cdc33e9f62 --- /dev/null +++ b/project_euler/problem_57/sol1.py @@ -0,0 +1,45 @@ +""" +Problem 57: Square root convergents +It is possible to show that the square root of two can be expressed as an infinite continued fraction. + +sqrt(2) = 1 + 1 / (2 + 1 / (2 + 1 / (2 + ...))) + +By expanding this for the first four iterations, we get: +1 + 1 / 2 = 3 / 2 = 1.5 +1 + 1 / (2 + 1 / 2} = 7 / 5 = 1.4 +1 + 1 / (2 + 1 / (2 + 1 / 2)) = 17 / 12 = 1.41666... +1 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / 2))) = 41/ 29 = 1.41379... + +The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, +1393/985, is the first example where the number of digits in the numerator exceeds +the number of digits in the denominator. + +In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator? +""" + + +def solution(n: int = 1000) -> int: + """ + returns number of fractions containing a numerator with more digits than the denominator in the first n expansions + >>> solution(14) + 2 + >>> solution(100) + 15 + >>> solution(10000) + 1508 + """ + a, b = 1, 1 + res = [] + for i in range(1, n + 1): + numerator = a + 2 * b + denominator = a + b + if len(str(numerator)) > len(str(denominator)): + res.append(i) + a = numerator + b = denominator + + return len(res) + + +if __name__ == "__main__": + print(f"{solution(10000) = }") From 1653eb050cc5e8a6e8598ab74e35ed2c21f83f66 Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Tue, 13 Oct 2020 15:04:42 +0200 Subject: [PATCH 2/4] fix line to long errors --- project_euler/problem_57/sol1.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_57/sol1.py b/project_euler/problem_57/sol1.py index d4cdc33e9f62..84d0044a64a5 100644 --- a/project_euler/problem_57/sol1.py +++ b/project_euler/problem_57/sol1.py @@ -1,6 +1,7 @@ """ Problem 57: Square root convergents -It is possible to show that the square root of two can be expressed as an infinite continued fraction. +It is possible to show that the square root of two can be expressed as an infinite +continued fraction. sqrt(2) = 1 + 1 / (2 + 1 / (2 + 1 / (2 + ...))) @@ -14,13 +15,15 @@ 1393/985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator. -In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator? +In the first one-thousand expansions, how many fractions contain a numerator with +more digits than the denominator? """ def solution(n: int = 1000) -> int: """ - returns number of fractions containing a numerator with more digits than the denominator in the first n expansions + returns number of fractions containing a numerator with more digits than + the denominator in the first n expansions. >>> solution(14) 2 >>> solution(100) From 1307efe22be292ef98b56a9ff51a614797ffd2ae Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Fri, 16 Oct 2020 11:14:16 +0200 Subject: [PATCH 3/4] update filenames and code to comply with new regulations --- project_euler/{problem_57 => problem_057}/__init__.py | 0 project_euler/{problem_57 => problem_057}/sol1.py | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename project_euler/{problem_57 => problem_057}/__init__.py (100%) rename project_euler/{problem_57 => problem_057}/sol1.py (93%) diff --git a/project_euler/problem_57/__init__.py b/project_euler/problem_057/__init__.py similarity index 100% rename from project_euler/problem_57/__init__.py rename to project_euler/problem_057/__init__.py diff --git a/project_euler/problem_57/sol1.py b/project_euler/problem_057/sol1.py similarity index 93% rename from project_euler/problem_57/sol1.py rename to project_euler/problem_057/sol1.py index 84d0044a64a5..ee5cbcd33e29 100644 --- a/project_euler/problem_57/sol1.py +++ b/project_euler/problem_057/sol1.py @@ -1,5 +1,5 @@ """ -Problem 57: Square root convergents +Project Euler Problem 57: https://projecteuler.net/problem=57 It is possible to show that the square root of two can be expressed as an infinite continued fraction. @@ -45,4 +45,4 @@ def solution(n: int = 1000) -> int: if __name__ == "__main__": - print(f"{solution(10000) = }") + print(f"{solution() = }") From ca62468bf553afed2ab80e20c35b85f463e37e2d Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Fri, 16 Oct 2020 17:37:02 +0200 Subject: [PATCH 4/4] more descriptive local variables --- project_euler/problem_057/sol1.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/project_euler/problem_057/sol1.py b/project_euler/problem_057/sol1.py index ee5cbcd33e29..04b6199f4717 100644 --- a/project_euler/problem_057/sol1.py +++ b/project_euler/problem_057/sol1.py @@ -31,17 +31,17 @@ def solution(n: int = 1000) -> int: >>> solution(10000) 1508 """ - a, b = 1, 1 - res = [] + prev_numerator, prev_denominator = 1, 1 + result = [] for i in range(1, n + 1): - numerator = a + 2 * b - denominator = a + b + numerator = prev_numerator + 2 * prev_denominator + denominator = prev_numerator + prev_denominator if len(str(numerator)) > len(str(denominator)): - res.append(i) - a = numerator - b = denominator + result.append(i) + prev_numerator = numerator + prev_denominator = denominator - return len(res) + return len(result) if __name__ == "__main__":