Your challenge is to convert a positive rational number into a finite simple continued fraction., in 2D, with horizontal fraction bars separating numerator from denominator. [A simple continued fraction only has numerators equal to 1.] Your solution should be able to represent a continued fraction with up to 8 terms.
Input: The rational number may be input in any format, including (but not restricted to)
- string: "7/16"
- list: {7, 16}, (7, 16), [7, 16]
- simple ordered pair: 7 16
- function: f[7,16]
- decimal number: 0.657
Output: A continued fraction, in 2D, with horizontal fraction bars separating numerator from denominator. Only continued fractions with numerators equal to 1 are valid. It is not necessary to make the font size vary according to depth. A leading zero (for proper fractions) is optional.
Depth: Your code must be able to display at least 8 levels of depth.
Winning criterion: Shortest code wins. You must include several test cases showing input and output.
Test Examples (Input followed by output)
Input | 2D Output |
---|---|
\$\frac 5 4\$ | \$1 + \cfrac 1 4\$ |
\$\frac 5 3\$ | \$1 + \cfrac 1 {1 + \cfrac 1 2}\$ |
\$\frac 5 7\$ | \$0 + \cfrac 1 {1 + \cfrac 1 {2 + \cfrac 1 2}}\$ |
\$\frac 9 {16}\$ | \$0 + \cfrac 1 {1 + \cfrac 1 {1 + \cfrac 1 {3 + \cfrac 1 2}}}\$ |
\$\frac {89} {150}\$ | \$0 + \cfrac 1 {1 + \cfrac 1 {1 + \cfrac 1 {2 + \cfrac 1 {5 + \cfrac 1 {1 + \cfrac 1 {1 + \cfrac 1 2}}}}}}\$ |
0 + 89 / 250
for the last one? \$\endgroup\$0 + 1 / (1 + 1 / (1 + 1 / (2 + 1 / (3 + 1 / (1 + 1 / (1 + 1 / (2)))))))
? What about without the parenthesis? Or if we just display the blue numbers, like0 1 1 2 5 1 1 2
? \$\endgroup\$