Create a gist now

Instantly share code, notes, and snippets.

二次体 Q(√m) に付随する指標を計算する Ruby のスクリプトです
# coding: utf-8
require 'prime'
# Legendre symbol
def legendre(a, p)
p.times do |x|
if (x*x - a) % p == 0 then
return 1
end
end
return -1
end
# Jacobi symbol
def jacobi(a, n)
divisions = Prime.prime_division(n)
jacobi_val = 1
divisions.each do |tuple|
p = tuple[0]
e = tuple[1]
jacobi_val *= (legendre(a, p)**e)
end
return jacobi_val
end
# Dirichlet's chi character
# p が素数のとき
def chi_p(d, p)
if 2*d % p != 0
return legendre(d, p)
elsif p == 2 && d%2 != 0
return jacobi(2, d.abs.to_i)
else
return 0
end
end
# a が合成数のとき
def chi(d, a)
if a == 0
return 0
end
divisions = Prime.prime_division(a)
chi_val = 1
divisions.each do |tuple|
p = tuple[0]
e = tuple[1]
chi_val *= (chi_p(d, p)**e)
end
return chi_val
end
# 判別式
def discriminant m
if m % 4 == 1
return m
elsif m % 4 == 2 || m % 4 == 3
return 4*m
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment