Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update sol1.py
Made formatting changes as mentioned by pre-commit
  • Loading branch information
sudoShikhar committed Oct 24, 2020
commit 35fefe8b64981f4fd7eff72a56c2d4c6bec2f77d
13 changes: 7 additions & 6 deletions project_euler/problem_064/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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()))}")