10 # [_term_number_] 生成する項の数 (ex. 2)
11 # [_operators_] 使用する演算子 (ex. %w[ + - ])
12 # [_min_] 各項の最小値 (ex. 0)
13 # [_max_] 各項の最大値 (ex. 30)
14 # [_single_term_min_] 各項の最小項数 (ex. 1)
15 # [_single_term_max_] 各項の最大項数 (ex. 1)
16 # [_use_coefficient_] 真なら係数をつける (ex. true)
17 # [_use_power_] 真なら2乗した項を作る (ex. false)
19 class SquareRootArithmetic < Arithmetic
21 root_pattern = /[2-9]?sqrt\((?:[2-9]|\d\d)\)/
22 term_pattern = /#{root_pattern} [+\-] #{root_pattern}/
23 validation /\A\(#{root_pattern}\)\^2\z/
24 validation /\A#{term_pattern}\z/
25 validation /\A\(#{term_pattern}\)(?:\^2)? [+\-] \(#{term_pattern}(?:\^2)?\)\z/
26 validation /\A\(#{term_pattern}\) [\*\/] \(#{term_pattern}\)\z/
32 :min => 0, :max => 30,
33 :operators => %w[ + - ],
35 :single_term_min => 1,
36 :single_term_max => 2,
42 max = options.values_at(:single_term_min, :single_term_max).max
43 amount_seed = (options[:single_term_min]..max).to_a
44 amount = amount_seed.sample
45 amount = 1 if amount <= 0
52 results << create_square_root_number
53 results << %w[ + - ].sample
55 result = results[0..-2].join(' ')
56 result = "(#{result})" if results.size > 2
57 if options[:use_power]
60 result = "#{result}^2"
66 def create_square_root_number
67 number = create_integer(options[:min], options[:max], false)
69 need_coefficient = options[:use_coefficient] ? rand(2) == 0 : false
70 if need_coefficient || [0, 1].include?(number)
72 coefficient = (min + rand(10 - min)).to_s
74 root = [0, 1].include?(number) ? '' : "sqrt(#{number})"
75 "#{coefficient}#{root}"