mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Compare commits
No commits in common. "5c276a8377b9f4139dac9cfff83fd47b88511a40" and "f66568e981edf5e384fe28a357daee3e13f16de9" have entirely different histories.
5c276a8377
...
f66568e981
@ -555,7 +555,6 @@
|
|||||||
* [Chudnovsky Algorithm](maths/chudnovsky_algorithm.py)
|
* [Chudnovsky Algorithm](maths/chudnovsky_algorithm.py)
|
||||||
* [Collatz Sequence](maths/collatz_sequence.py)
|
* [Collatz Sequence](maths/collatz_sequence.py)
|
||||||
* [Combinations](maths/combinations.py)
|
* [Combinations](maths/combinations.py)
|
||||||
* [Continued Fraction](maths/continued_fraction.py)
|
|
||||||
* [Decimal Isolate](maths/decimal_isolate.py)
|
* [Decimal Isolate](maths/decimal_isolate.py)
|
||||||
* [Decimal To Fraction](maths/decimal_to_fraction.py)
|
* [Decimal To Fraction](maths/decimal_to_fraction.py)
|
||||||
* [Dodecahedron](maths/dodecahedron.py)
|
* [Dodecahedron](maths/dodecahedron.py)
|
||||||
|
@ -1,51 +0,0 @@
|
|||||||
"""
|
|
||||||
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 -= integer_part * denominator
|
|
||||||
if numerator == 0:
|
|
||||||
break
|
|
||||||
numerator, denominator = denominator, numerator
|
|
||||||
|
|
||||||
return continued_fraction_list
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import doctest
|
|
||||||
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
||||||
print("Continued Fraction of 0.84375 is: ", continued_fraction(Fraction("0.84375")))
|
|
@ -226,7 +226,7 @@ def plot(
|
|||||||
No doctest provided since this function does not have a return value.
|
No doctest provided since this function does not have a return value.
|
||||||
"""
|
"""
|
||||||
fig = plt.figure()
|
fig = plt.figure()
|
||||||
fig.canvas.manager.set_window_title(title)
|
fig.canvas.set_window_title(title)
|
||||||
ax = plt.axes(
|
ax = plt.axes(
|
||||||
xlim=(x_start, x_end), ylim=(y_start, y_end)
|
xlim=(x_start, x_end), ylim=(y_start, y_end)
|
||||||
) # Set section to be plotted
|
) # Set section to be plotted
|
||||||
|
Loading…
x
Reference in New Issue
Block a user