option :denominator_max, 100
def expression
+ return two_term_expression if options[:term_number] == 2
+ some_term_expression
+ end
+
+ def two_term_expression
+ result = []
+ result << operand
+ result << (operator = options[:operators].sample)
+ an, ad = result.first.scan(/(\d+)\/(\d+)/).first.map(&:to_i)
+ begin
+ numerator = numerator_part
+ denominator = denominator_part
+ res_numerator, res_denominator =
+ result_parts(
+ an, ad,
+ numerator, denominator,
+ operator
+ )
+ end while is_lowest_term?(res_numerator, res_denominator)
+ result << "(%s/%s)" % [numerator, denominator]
+ result.join(' ')
+ end
+
+ def result_parts(a, b, c, d, op)
+ lcm = b.lcm(d)
+ case op
+ when '+' then return [a*lcm + c*lcm, lcm]
+ when '-' then return [a*lcm - c*lcm, lcm]
+ when '*' then return [a * c, b * d]
+ when 'div' then return [a * d, b * c]
+ end
+ end
+
+ def some_term_expression
result = []
operator = ''
options[:term_number].times do