Problem 51580. Construct a continued fraction for a square root

Numbers can be expressed as continued fractions of the form
x = a_0 + 1/(a_1 + 1/(a_2 + 1/(a_3 + 1/...)))
Some continued fractions—such as those for e and π--continue forever without a discernable pattern in the coefficients, while the coefficients of continued fractions for square roots eventually repeat. For example,
sqrt(2) = 1+(1/(2+1/(2+1/(2+1/...))))
or
sqrt(14) = 3+1/(1+1/(2+1/(1+1/(6+1/...))))
Write a function that takes a non-square integer and returns the values of until the values repeat.
This problem celebrates my finally cracking Cody Problem 1215 by James. If you struggle with Test 8, as I did, remember that MATLAB cannot represent decimals with infinite precision.

Solution Stats

19.19% Correct | 80.81% Incorrect
Last Solution submitted on Aug 30, 2024

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers12

Suggested Problems

More from this Author281

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!