-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Added Continued fractions #6846
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
tianyizheng02
merged 6 commits into
TheAlgorithms:master
from
Saksham1970:continuous-fractions
Aug 16, 2023
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4540cfb
updating DIRECTORY.md
379716e
added continued fractions
Saksham1970 ba2da7c
updating DIRECTORY.md
e18b2fe
Update maths/continued_fraction.py
Saksham1970 fd1cbe4
Update maths/continued_fraction.py
tianyizheng02 12cbf85
Merge branch 'master' into continuous-fractions
tianyizheng02 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
This file contains 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
This file contains 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,52 @@ | ||
""" | ||
Finding the continuous fraction for a rational number using python | ||
|
||
https://en.wikipedia.org/wiki/Continued_fraction | ||
""" | ||
|
||
|
||
from fractions import Fraction | ||
|
||
|
||
def continued_fraction(num: Fraction) -> list[int]: | ||
""" | ||
:param num: | ||
Fraction of the number whose continued fractions to be found. | ||
Use Fraction(str(number)) for more accurate results due to | ||
float inaccuracies. | ||
|
||
:return: | ||
The continued fraction of rational number. | ||
It is the all commas in the (n + 1)-tuple notation. | ||
|
||
>>> continued_fraction(Fraction(2)) | ||
[2] | ||
>>> continued_fraction(Fraction("3.245")) | ||
[3, 4, 12, 4] | ||
>>> continued_fraction(Fraction("2.25")) | ||
[2, 4] | ||
>>> continued_fraction(1/Fraction("2.25")) | ||
[0, 2, 4] | ||
>>> continued_fraction(Fraction("415/93")) | ||
[4, 2, 6, 7] | ||
""" | ||
numerator, denominator = num.as_integer_ratio() | ||
continued_fraction_list: list[int] = [] | ||
while True: | ||
integer_part = int(numerator / denominator) | ||
continued_fraction_list.append(integer_part) | ||
numerator = numerator - integer_part * denominator | ||
Saksham1970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if numerator == 0: | ||
break | ||
numerator, denominator = denominator, numerator | ||
|
||
return continued_fraction_list | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
print("Continued Fraction of 0.84375 is: ", continued_fraction(Fraction("0.84375"))) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs tests for 0 and for negative numbers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added tests for 0 and negative numbers in #8985