From f51911d3f4f8090c4c9bc963ebfcef2e657cfa99 Mon Sep 17 00:00:00 2001 From: Shikhar Rai <34543293+kakashi215@users.noreply.github.com> Date: Sat, 24 Oct 2020 18:07:02 +0530 Subject: [PATCH 1/4] Added solution to Euler 64. Added Python solution to Project Euler Problem 64. Added a folder problem_064. Added __init__.py file. Added sol1.py file. --- project_euler/problem_064/__init__.py | 0 project_euler/problem_064/sol1.py | 76 +++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 project_euler/problem_064/__init__.py create mode 100644 project_euler/problem_064/sol1.py diff --git a/project_euler/problem_064/__init__.py b/project_euler/problem_064/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_064/sol1.py b/project_euler/problem_064/sol1.py new file mode 100644 index 000000000000..3fe3a78d5c79 --- /dev/null +++ b/project_euler/problem_064/sol1.py @@ -0,0 +1,76 @@ +""" +Project Euler Problem 64: https://projecteuler.net/problem=64 + +All square roots are periodic when written as continued fractions. +For example, let us consider sqrt(23). +It can be seen that the sequence is repeating. +For conciseness, we use the notation sqrt(23)=[4;(1,3,1,8)], +to indicate that the block (1,3,1,8) repeats indefinitely. +Exactly four continued fractions, for N<=13, have an odd period. +How many continued fractions for N<=10000 have an odd period? + +References: +- https://en.wikipedia.org/wiki/Continued_fraction +""" + +from math import sqrt, floor + + +def continuous_fraction_period(n: int) -> int: + """ + Returns the continued fraction period of a number n. + + >>> continuous_fraction_period(2) + 1 + >>> continuous_fraction_period(5) + 1 + >>> continuous_fraction_period(7) + 4 + >>> continuous_fraction_period(11) + 2 + >>> continuous_fraction_period(13) + 5 + """ + numerator = 0.0 + denominator = 1.0 + ROOT = int(sqrt(n)) + an = ROOT + period = 0 + while an != 2*ROOT: + numerator = denominator*an - numerator + denominator = (n - numerator**2)/denominator + an = int((ROOT + numerator)/denominator) + period += 1 + return period + + +def solution(n: int = 10000) -> int: + """ + Returns the count of numbers <= 10000 with odd periods. + This function calls continuous_fraction_period for numbers which are + not perfect squares. + This is checked in if sr - floor(sr) != 0 statement. + If an odd period is returned by continuous_fraction_period, + count_odd_periods is increased by 1. + + >>> solution(2) + 1 + >>> solution(5) + 2 + >>> solution(7) + 2 + >>> solution(11) + 3 + >>> solution(13) + 4 + """ + count_odd_periods = 0 + for i in range(2, n+1): + sr = sqrt(i) + if sr - floor(sr) != 0: + if continuous_fraction_period(i) % 2 == 1: + count_odd_periods += 1 + return count_odd_periods + +if __name__ == "__main__": + print(f"{solution(int(input().split()))}") From 35fefe8b64981f4fd7eff72a56c2d4c6bec2f77d Mon Sep 17 00:00:00 2001 From: Shikhar Rai <34543293+kakashi215@users.noreply.github.com> Date: Sat, 24 Oct 2020 18:24:11 +0530 Subject: [PATCH 2/4] Update sol1.py Made formatting changes as mentioned by pre-commit --- project_euler/problem_064/sol1.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_064/sol1.py b/project_euler/problem_064/sol1.py index 3fe3a78d5c79..fab0a07ce434 100644 --- a/project_euler/problem_064/sol1.py +++ b/project_euler/problem_064/sol1.py @@ -13,7 +13,7 @@ - https://en.wikipedia.org/wiki/Continued_fraction """ -from math import sqrt, floor +from math import floor, sqrt def continuous_fraction_period(n: int) -> int: @@ -36,10 +36,10 @@ def continuous_fraction_period(n: int) -> int: ROOT = int(sqrt(n)) an = ROOT period = 0 - while an != 2*ROOT: - numerator = denominator*an - numerator - denominator = (n - numerator**2)/denominator - an = int((ROOT + numerator)/denominator) + while an != 2 * ROOT: + numerator = denominator * an - numerator + denominator = (n - numerator ** 2) / denominator + an = int((ROOT + numerator) / denominator) period += 1 return period @@ -65,12 +65,13 @@ def solution(n: int = 10000) -> int: 4 """ count_odd_periods = 0 - for i in range(2, n+1): + for i in range(2, n + 1): sr = sqrt(i) if sr - floor(sr) != 0: if continuous_fraction_period(i) % 2 == 1: count_odd_periods += 1 return count_odd_periods + if __name__ == "__main__": print(f"{solution(int(input().split()))}") From 74d91b463284c765132e1a38df9cdd835b4a28dd Mon Sep 17 00:00:00 2001 From: Shikhar Rai <34543293+kakashi215@users.noreply.github.com> Date: Mon, 2 Nov 2020 12:57:25 +0530 Subject: [PATCH 3/4] Update sol1.py Minor changes to variable naming and function calling as mentioned by @ruppysuppy --- project_euler/problem_064/sol1.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/project_euler/problem_064/sol1.py b/project_euler/problem_064/sol1.py index fab0a07ce434..fd857c472f1f 100644 --- a/project_euler/problem_064/sol1.py +++ b/project_euler/problem_064/sol1.py @@ -34,12 +34,12 @@ def continuous_fraction_period(n: int) -> int: numerator = 0.0 denominator = 1.0 ROOT = int(sqrt(n)) - an = ROOT + integer_part = ROOT period = 0 - while an != 2 * ROOT: - numerator = denominator * an - numerator + while integer_part != 2 * ROOT: + numerator = denominator * integer_part - numerator denominator = (n - numerator ** 2) / denominator - an = int((ROOT + numerator) / denominator) + integer_part = int((ROOT + numerator) / denominator) period += 1 return period @@ -74,4 +74,4 @@ def solution(n: int = 10000) -> int: if __name__ == "__main__": - print(f"{solution(int(input().split()))}") + print(f"{solution()=}") From 17264b9b9b9c23c14a52fba4eaa3a9a1bc6a020e Mon Sep 17 00:00:00 2001 From: Shikhar Rai <34543293+kakashi215@users.noreply.github.com> Date: Mon, 2 Nov 2020 13:12:08 +0530 Subject: [PATCH 4/4] Update sol1.py Changes to function call as mentioned by @cclauss --- project_euler/problem_064/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_064/sol1.py b/project_euler/problem_064/sol1.py index fd857c472f1f..69e3f6d97580 100644 --- a/project_euler/problem_064/sol1.py +++ b/project_euler/problem_064/sol1.py @@ -74,4 +74,4 @@ def solution(n: int = 10000) -> int: if __name__ == "__main__": - print(f"{solution()=}") + print(f"{solution(int(input().strip()))}")