Revision 0dd43a7483cd676e61d82cdae5e67ebe7f181eb5 authored by Amit Murthy on 15 September 2015, 04:07:16 UTC, committed by Tony Kelman on 17 September 2015, 03:27:26 UTC
(cherry picked from commit 3d6c872f74aae4b81e549d9b75aec1fc0775d5b8)
ref #13134
1 parent 1af871b
Raw File
modint.jl
# This file is a part of Julia. License is MIT: http://julialang.org/license

module ModInts
export ModInt

import Base: +, -, *

immutable ModInt{n} <: Integer
    k::Int
    ModInt(k) = new(mod(k,n))
end

-{n}(a::ModInt{n}) = ModInt{n}(-a.k)
+{n}(a::ModInt{n}, b::ModInt{n}) = ModInt{n}(a.k+b.k)
-{n}(a::ModInt{n}, b::ModInt{n}) = ModInt{n}(a.k-b.k)
*{n}(a::ModInt{n}, b::ModInt{n}) = ModInt{n}(a.k*b.k)

Base.convert{n}(::Type{ModInt{n}}, i::Int) = ModInt{n}(i)
Base.promote_rule{n}(::Type{ModInt{n}}, ::Type{Int}) = ModInt{n}

Base.show{n}(io::IO, k::ModInt{n}) = print(io, "$(k.k) mod $n")
Base.showcompact(io::IO, k::ModInt) = print(io, k.k)

Base.inv{n}(a::ModInt{n}) = ModInt{n}(invmod(a.k, n))

end # module
back to top