Permalink
Browse files

Integer#cubic_residue?: Add

  • Loading branch information...
1 parent 33953d2 commit 63353d46c942aec21d37c7579e53b125110484b7 @runpaint committed Mar 27, 2010
Showing with 50 additions and 1 deletion.
  1. +10 −1 lib/numb/reciprocity.rb
  2. +40 −0 spec/numb/cubic_residue_spec.rb
View
@@ -1,6 +1,15 @@
# coding: utf-8
class Integer
def quadratic_residue?(p)
- (0..p/2).any?{|x| (x**2).modulo(p) == self}
+ residue? p, 2
+ end
+
+ def cubic_residue?(p)
+ coprime?(p) and residue? p, 3
+ end
+
+ private
+ def residue?(p, e=1)
+ (1...p).any?{|x| (x**e).modulo(p) == self}
end
end
@@ -0,0 +1,40 @@
+# coding: utf-8
+describe Integer, "#cubic_residue?" do
+ # A096107
+ @seq = [
+ [1],
+ [1,2],
+ [1,3],
+ [1,2,3,4],
+ [1,5],
+ [1,6],
+ [1,3,5,7],
+ [1,8],
+ [1,3,7,9],
+ [1,2,3,4,5,6,7,8,9,10],
+ [1,5,7,11],
+ [1,5,8,12],
+ [1,13],
+ [1,2,4,7,8,11,13,14],
+ [1,3,5,7,9,11,13,15],
+ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
+ [1,17],
+ [1,7,8,11,12,18],
+ [1,3,7,9,11,13,17,19],
+ [1,8]
+ ]
+
+ @seq.to_enum.with_index(2).each do |values, p|
+ (1...p).each do |n|
+ if values.include?(n)
+ it "returns true for cubic residue (mod #{p}) #{n}" do
+ n.should be_cubic_residue(p)
+ end
+ elsif p != 21
+ it "returns false for cubic non-residue (mod #{p}) #{n}" do
+ n.should_not be_cubic_residue(p)
+ end
+ end
+ end
+ end
+end

0 comments on commit 63353d4

Please sign in to comment.