Permalink
Browse files

Integer#jacobi: Add

  • Loading branch information...
1 parent aae5020 commit 2b4cc8d5f9c3b14ac8834cdb5093169092d48893 @runpaint committed Oct 18, 2010
Showing with 77 additions and 0 deletions.
  1. +12 −0 lib/numb/kronecker.rb
  2. +65 −0 spec/numb/jacobi_spec.rb
View
@@ -98,4 +98,16 @@ def kronecker b
def legendre p
kronecker(p) if p.odd? and p > 2 and p.prime?
end
+
+ # Returns the Jacobi symbol for `a`/`n`
+ #
+ # For any integer `a` and any positive odd integer `n` the Jacobi
+ # symbol is defined as the product of the Legendre symbols
+ # corresponding to the prime factors of `n`.
+ #
+ # @param [Integer] p The "denominator"
+ # @return [Integer, nil] -1, 0, 1, or `nil`
+ def jacobi n
+ kronecker(n) if n.odd? and n > 0
+ end
end
View
@@ -0,0 +1,65 @@
+# -*- coding: utf-8 -*-
+describe Integer, "#jacobi" do
+
+ @seq = {
+ # A165591
+ 59701 => [0,1,-1,1,1,1,-1,-1,-1,1,-1,1,1,-1,1,1,1,-1,-1,-1,
+ 1,-1,-1,1,-1,1,1,1,-1,-1,-1,-1,-1,1,1,-1,1,-1,1,
+ -1,-1,1,1,1,1,1,-1,-1,1,1,-1,-1,-1,-1,-1,1,1,-1,1,
+ -1,1,-1,1,-1,1,-1,-1,1,-1,1,1,-1,-1,-1,1,1,-1,-1,
+ 1,-1,1],
+ # A165596
+ 59881 => [0,1,1,1,1,1,1,-1,1,1,1,-1,1,1,-1,1,1,-1,1,-1,1,
+ -1,-1,1,1,1,1,1,-1,1,1,1,1,-1,-1,-1,1,-1,-1,1,1,1,
+ -1,1,-1,1,1,1,1,1,1,-1,1,1,1,-1,-1,-1,1,-1,1,-1,1,
+ -1,1,1,-1,-1,-1,1,-1,-1,1,-1,-1,1,-1,1,1,-1,1,1,1,
+ 1,-1,-1],
+ }
+
+ @seq.each do |a, ns|
+ ns.each_with_index do |j, n|
+ it "returns #{j} for #{a}.jacobi(#{n})" do
+ n.jacobi(a).should == j
+ end
+ end
+ end
+
+ # A077009
+ [1,-1,1,-1,0,-1,1,1,1,-1,0,-1,0,0,1,-1,-1,-1,1,0,
+ 1,-1,0,-1,0,-1,1,0,0,-1,1,0,-1,-1,1,-1,1,0,1,-1,0,
+ -1,1,1,1,-1,0,1,1,0,1,-1,0,-1,1,0,1,1,0,1,0,-1,0,
+ -1,0,-1,1,0,1,-1,1,-1,-1,0,1,-1,0,0,1,1,1,-1,0,-1,
+ 0,0,1,0,-1,-1,1,0,1,1,0,-1,1,0,1,-1].each_with_index do |j, n|
+ n = (2 * n).succ
+ a = n.φ
+ it "returns #{j} for #{a}.jacobi(#{n})" do
+ a.jacobi(n).should == j
+ end
+ end
+
+ # A077010
+ [1,1,1,1,1,1,1,0,1,1,-1,1,1,1,1,1,0,1,1,-1,1,1,0,
+ 1,1,0,1,1,-1,1,1,-1,-1,1,0,1,1,1,1,1,1,1,1,0,1,0,
+ -1,0,1,0,1,1,0,1,1,-1,1,1,0,1,1,0,1,1,-1,1,1,0,1,
+ 1,0,1,0,0,1,1,0,1,1,0,-1,1,0,1,1,-1,1,-1,0,1,1,-1,
+ 1,1,1,1,1,0,1,1].each_with_index do |j, n|
+ n = (2 * n).succ
+ a = n.σ
+ it "returns #{j} for #{a}.jacobi(#{n})" do
+ a.jacobi(n).should == j
+ end
+ end
+
+ [2, 10, -4, 0].each do |n|
+ 10.times do |a|
+ it "returns nil for #{a}.jacobi(#{n})" do
+ a.jacobi(n).should be_nil
+ end
+ end
+ end
+end
+
+
+
+
+

0 comments on commit 2b4cc8d

Please sign in to comment.