Perhaps someone has already done this. The sensor on my bike outputs both cadence (crank speed in revs/sec) and wheel speed (revs/sec). I'm trying to infer which gear the bike is in by looking at the ratio of these two numbers. My plan was to use the equivalent of Scheme's "rationalize" function (which uses a continued fraction algorithm internally), which produces an exact fraction which is within some epsilon of a given number -- in this case the wheel/crank speed. This should give me an approximation to the ratio (#front teeth)/(#rear teeth). http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html Unfortunately, there is enough error in the measurements of the crank speed and the wheel speed so that the output isn't stable. So far, we haven't utilized any information about which chainrings (front teeth) and cogs (rear teeth) are mounted on the bike, except for the fact that each must have an integer # of teeth. Of course, I can always make up a table of all of the gears and simply find the closest one & output it. But if the number of cogs grows large and the number of chainrings grows large (we're mathematicians, after all!), we'd rather not search a large table. Suppose now that we have upper & lower limits on the teeth on the chainrings (28 to 52) and the cogs (12 to 26), but otherwise don't know how many chainrings or cogs are mounted on the bike. How do we modify the continued fraction algorithm used within rationalize to produce fractions with numerators and denominators in the appropriate range ?