#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Continued_fraction/Arithmetic/Construct_from_rational_number
#

func r2cf(num, den) {
    func() {
        den.is_zero && return()
        var q = int(num/den)
        (num, den) = (den, num - q*den)
        return q
    }
}

func showcf(f) {
    print "["
    var n = f()
    print "#{n}" if defined(n)
    print "; #{n}" while defined(n = f())
    print "]\n"
}

[
    [1/2, 3/1, 23/8, 13/11, 22/7, -151/77],
    [14142/10000, 141421/100000, 1414214/1000000, 14142136/10000000],
    [314285714/100000000],
].each { |seq|
    seq.each { |r| showcf(r2cf(r.nude)) }
    print "\n"
}
