forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modint.jl
24 lines (17 loc) · 695 Bytes
/
modint.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# This file is a part of Julia. License is MIT: https://julialang.org/license
module ModInts
export ModInt
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