-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
HACKTOBERFEST - Added solution to Euler 64. #3706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()))}") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.