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
(n: int)
| 17 | |
| 18 | |
| 19 | def continuous_fraction_period(n: int) -> int: |
| 20 | """ |
| 21 | Returns the continued fraction period of a number n. |
| 22 | |
| 23 | >>> continuous_fraction_period(2) |
| 24 | 1 |
| 25 | >>> continuous_fraction_period(5) |
| 26 | 1 |
| 27 | >>> continuous_fraction_period(7) |
| 28 | 4 |
| 29 | >>> continuous_fraction_period(11) |
| 30 | 2 |
| 31 | >>> continuous_fraction_period(13) |
| 32 | 5 |
| 33 | """ |
| 34 | numerator = 0.0 |
| 35 | denominator = 1.0 |
| 36 | root = int(sqrt(n)) |
| 37 | integer_part = root |
| 38 | period = 0 |
| 39 | while integer_part != 2 * root: |
| 40 | numerator = denominator * integer_part - numerator |
| 41 | denominator = (n - numerator**2) / denominator |
| 42 | integer_part = int((root + numerator) / denominator) |
| 43 | period += 1 |
| 44 | return period |
| 45 | |
| 46 | |
| 47 | def solution(n: int = 10000) -> int: |