Skip to content

add kronecker function (useful for lots of number theory stuff) #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "IntegerMathUtils"
uuid = "18e54dd8-cb9d-406c-a71d-865a43cbb235"
version = "0.1.1"
version = "0.1.2"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
58 changes: 53 additions & 5 deletions src/IntegerMathUtils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module IntegerMathUtils
export iroot, ispower, rootrem, find_exponent, is_probably_prime
export iroot, ispower, rootrem, find_exponent, is_probably_prime, kronecker

iroot(x::Integer, n::Integer) = iroot(x, Cint(n))

Expand All @@ -10,7 +10,7 @@ function iroot(x::BigInt, n::Cint)
n <= 0 && throw(DomainError(n, "Exponent must be > 0"))
x <= 0 && iseven(x) && throw(DomainError(n, "This is a math no-no"))
ans = BigInt()
ccall((:__gmpz_root, :libgmp), Cint, (Ref{BigInt}, Ref{BigInt}, Cint), ans, x, n)
@ccall :libgmp.__gmpz_root(ans::Ref{BigInt}, x::Ref{BigInt}, n::Cint)::Cint
ans
end

Expand All @@ -22,15 +22,15 @@ function rootrem(x::T, n::Integer) where {T<:Integer}
x <= 0 && iseven(x) && throw(DomainError(n, "This is a math no-no"))
root = BigInt()
rem = BigInt()
ccall((:__gmpz_rootrem, :libgmp), Nothing,(Ref{BigInt}, Ref{BigInt}, Ref{BigInt}, Int), root, rem, x, n)
@ccall :libgmp.__gmpz_rootrem(root::Ref{BigInt}, rem::Ref{BigInt}, x::Ref{BigInt}, n::Int)::Nothing
return (root, T(rem))
end

# TODO: Add more efficient implimentation for smaller types
ispower(x::Integer) = ispower(big(x))

function ispower(x::BigInt)
return 0 != ccall((:__gmpz_perfect_power_p, :libgmp), Cint, (Ref{BigInt},), x)
return 0 != @ccall :libgmp.__gmpz_perfect_power_p(x::Ref{BigInt})::Cint
end

# TODO: Add more efficient implimentation for smaller types
Expand All @@ -43,7 +43,55 @@ function find_exponent(x::Integer)
end

function is_probably_prime(x::Integer; reps=25)
return ccall((:__gmpz_probab_prime_p, :libgmp), Cint, (Ref{BigInt}, Cint), x, reps) != 0
if !(x isa BigInt)
x = BigInt(x)
end
return 0 != @ccall :libgmp.__gmpz_probab_prime_p(x::Ref{BigInt}, reps::Cint)::Cint
end

function kronecker(a::BigInt, b::Clong)
return @ccall :libgmp.__gmpz_kronecker_si(a::Ref{BigInt}, b::Clong)::Cint
end
function kronecker(a::Clong, b::BigInt)
return @ccall :libgmp.__gmpz_si_kronecker(a::Clong, b::Ref{BigInt})::Cint
end
function kronecker(a, n)
@assert n != -n || n == 0
@assert a != -a || a == 0
t = 1
if iszero(n)
return Int(abs(a) == 1)
end
if n < 0
n = abs(n)
if a < 0
t = -t
end
end
trail = trailing_zeros(n)
if trail > 0
n >>= trail
if iseven(a)
return 0
elseif isodd(trail) && a&7 in (3,5)
t = -t
end
end
a = mod(a, n)
while a != 0
while iseven(a)
a = a >> 1
if n&7 in (3, 5)
t = -t
end
end
a, n = n, a
if a&3 == n&3 == 3
t = -t
end
a = mod(a, n)
end
return n == 1 ? t : 0
end

end
64 changes: 0 additions & 64 deletions tests/runtests.jl

This file was deleted.