OSDN Git Service

use auto-generated square numbers
authorTomohiro Nishimura <tomohiro68@gmail.com>
Thu, 11 Mar 2010 02:28:00 +0000 (11:28 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Thu, 11 Mar 2010 02:28:00 +0000 (11:28 +0900)
lib/mint/generator/quadratic_equation.rb
spec/generator/quadratic_equation_spec.rb

index 291ef89..60a8634 100644 (file)
@@ -22,6 +22,7 @@ module Mint::Generator
     option :r_max,       20
     option :p_max,       12
     option :q_max,       40
+    option :q_min,       0
 
     def generate_problem
       expression
@@ -74,46 +75,45 @@ module Mint::Generator
     end
 
     def generate_quadeqn_params(type)
-      squares = [0,1,4,9,16,25,36,49,64,81,100,121,144]
-
       r = generate_r
       p = generate_p
-      q = __send__(:"#{type}_q", squares)
+      q = __send__(:"#{type}_q")
 
       [r, p, q]
     end
 
     def generate_r
-      ra = create_integer(1, options[:p_max], false) {|ra|
+      create_integer(1, options[:p_max], false) {|ra|
         !Prime.prime?(ra)
       }
-      ra
     end
 
     def generate_p
-      pa = create_integer(-options[:p_max], options[:p_max], false) {|pa|
+      create_integer(-options[:p_max], options[:p_max], false) {|pa|
         !Prime.prime?(pa)
       }
-      pa
     end
 
-    def rational_q(squares)
-      squares.dup.sample
+    def rational_q
+      squares(options[:q_min], options[:q_max]).sample
     end
 
-    def irrational_q(squares)
-      qa = create_integer(0, options[:q_max], false) {|qa|
-        !squares.include?(qa) &&
+    def irrational_q
+      create_integer(0, options[:q_max], false) {|qa|
+        !squares(options[:q_min], options[:q_max]).include?(qa) &&
         !Prime.prime?(qa)
       }
-      qa
     end
 
-    def imaginary_q(squares)
-      qa = create_integer(1, options[:q_max], false) {|qa|
+    def imaginary_q
+      -create_integer(1, options[:q_max], false) {|qa|
         !Prime.prime?(qa)
       }
-      -qa
+    end
+
+    def squares(min, max)
+      min, max = [min, max].sort
+      (min..max).to_a.map {|i| i * i }
     end
   end
 end
index d0ed2dc..4613b2d 100644 (file)
@@ -10,10 +10,7 @@ module Mint::Generator
 
     context 'problems' do
 
-      before do
-        options = {}
-        @problem = subject.generate(options)
-      end
+      before { @problem = subject.generate }
 
       it { @problem.should have(1).problem }
       it { @problem.first.scan('x').should have(2).variables }
@@ -38,13 +35,21 @@ module Mint::Generator
     context 'generate params' do
 
       before do
-        @squares = (1..10).to_a
-        subject.__send__(
-          :options=,
-          {:r_max => 10, :p_max => 10, :q_max => 10}
-        )
+        @options = {
+          :r_min => 1,
+          :r_max => 10,
+          :p_min => -10,
+          :p_max => 10,
+          :q_max => 10,
+          :q_min => 0,
+        }
+        subject.__send__(:options=, @options)
+        @squares = subject.__send__(:squares, @options[:q_min], @options[:q_max])
+        @prime_numbers = (@options[:q_min]..@options[:q_max]).to_a.select {|i| Prime.prime?(i) }
       end
 
+      it { subject.__send__(:squares, 0, 10).should == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] }
+
       [:rational, :irrational, :imaginary].each do |type|
         it do
           subject.should_receive(:generate_r).and_return(1)
@@ -54,12 +59,13 @@ module Mint::Generator
         end
       end
 
-      it { (1..10).should include(subject.__send__(:generate_r)) }
-      it { (-10..10).should include(subject.__send__(:generate_p))}
+      it { (@options[:r_min]..@options[:r_max]).should include(subject.__send__(:generate_r)) }
+      it { (@options[:p_min]..@options[:p_max]).should include(subject.__send__(:generate_p))}
 
-      it { (1..10).should include(subject.__send__(:"rational_q", @squares)) }
-      it { subject.__send__(:"irrational_q", @squares).should == 0 }
-      it { (-10..-1).should include(subject.__send__(:"imaginary_q", @squares)) }
+      it { @squares.should include(subject.__send__(:"rational_q")) }
+      it { @squares.should_not include(subject.__send__(:"irrational_q")) }
+      it { @prime_numbers.should_not include(subject.__send__(:"irrational_q")) }
+      it { @prime_numbers.should_not include(subject.__send__(:"imaginary_q")) }
     end
 
     context 'generate coefficient' do