OSDN Git Service

fix quadratic_equation generator
authorTomohiro Nishimura <tomohiro68@gmail.com>
Thu, 18 Mar 2010 06:21:14 +0000 (15:21 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Thu, 18 Mar 2010 06:21:14 +0000 (15:21 +0900)
lib/mint/generator/quadratic_equation.rb
spec/generator/quadratic_equation_spec.rb

index 462b93c..b6fe44c 100644 (file)
@@ -37,10 +37,14 @@ module Mint::Generator
   # == r, p, q について
   # r, p, q を用いて 係数 a, b, c を生成します.
   # それぞれ以下の式で求めることができます.
-  # - a = r^2       / gcd(r, p, q)
-  # - b = -2pr      / gcd(r, p, q)
-  # - c = (p^2 - q) / gcd(r, p, q)
-  #
+  # - a' = r^2
+  # - b' = -2pr
+  # - c' = (p^2 - q)
+  # - gcd = GCD(a', b', c')
+  # とすると
+  # - a = a' / gcd
+  # - b = b' / gcd
+  # - c = c' / gcd
   #
   class QuadraticEquation < Base
 
@@ -117,15 +121,11 @@ module Mint::Generator
 
     def generate_r
       min = [1, options[:r_min]].max
-      create_integer(min, options[:r_max], false) {|ra|
-        !Prime.prime?(ra)
-      }
+      create_integer(min, options[:r_max], false)
     end
 
     def generate_p
-      create_integer(options[:p_min], options[:p_max], options[:p_minus]) {|pa|
-        !Prime.prime?(pa)
-      }
+      create_integer(options[:p_min], options[:p_max], options[:p_minus])
     end
 
     def rational_q
@@ -136,16 +136,13 @@ module Mint::Generator
       min = [0, options[:q_min]].max
       squares = squares(options[:q_min], options[:q_max])
       create_integer(min, options[:q_max], false) {|qa|
-        !squares.include?(qa) &&
-        !Prime.prime?(qa)
+        !squares.include?(qa)
       }
     end
 
     def imaginary_q
       min = [1, options[:q_min]].max
-      -create_integer(min, options[:q_max], false) {|qa|
-        !Prime.prime?(qa)
-      }
+      -create_integer(min, options[:q_max], false)
     end
 
     def squares(min, max)
index febce27..28ea348 100644 (file)
@@ -72,7 +72,6 @@ module Mint::Generator
             }
             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 { (@options[:r_min]..@options[:r_max]).should include(subject.__send__(:generate_r)) }
@@ -83,12 +82,10 @@ module Mint::Generator
             subject.__send__(:options=, subject.__send__(:options).merge(:p_minus => false))
           end
 
-          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 { (@options[:q_min]..@options[:q_max]).should include(subject.__send__(:irrational_q)) }
-          it { @prime_numbers.should_not include(subject.__send__(:imaginary_q)) }
-          it { (@options[:q_min]..@options[:q_max]).should include(-subject.__send__(:imaginary_q)) }
+           it { @squares.should include(subject.__send__(:rational_q)) }
+           it { @squares.should_not include(subject.__send__(:irrational_q)) }
+           it { (@options[:q_min]..@options[:q_max]).should include(subject.__send__(:irrational_q)) }
+           it { (@options[:q_min]..@options[:q_max]).should include(-subject.__send__(:imaginary_q)) }
         end
       end
     end
@@ -116,6 +113,7 @@ module Mint::Generator
             @params = subject.__send__(:generate_coefficients, r, p, q)
           end
           it { discriminant(*@params).should > 0 }
+          it { Math.sqrt(discriminant(*@params)).should_not be_an_instance_of(Fixnum) }
         end
 
         context 'rational' do
@@ -124,6 +122,7 @@ module Mint::Generator
             @params = subject.__send__(:generate_coefficients, r, p, q)
           end
           it { discriminant(*@params).should > 0 }
+          it { Math.sqrt(discriminant(*@params)).should be_an_instance_of(Fixnum) }
         end
 
         context 'imaginary' do