fix incorrect folder name for julia-0.6.x
Former-commit-id: ef2c7401e0876f22d2f7762d182cfbcd5a7d9c70
This commit is contained in:
27
julia-0.6.3/share/doc/julia/examples/ModInts.jl
Normal file
27
julia-0.6.3/share/doc/julia/examples/ModInts.jl
Normal file
@@ -0,0 +1,27 @@
|
||||
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
module ModInts
|
||||
export ModInt
|
||||
|
||||
import Base: +, -, *, /, inv
|
||||
|
||||
struct ModInt{n} <: Integer
|
||||
k::Int
|
||||
ModInt{n}(k) where n = new(mod(k,n))
|
||||
end
|
||||
|
||||
Base.show{n}(io::IO, k::ModInt{n}) =
|
||||
print(io, get(io, :compact, false) ? k.k : "$(k.k) mod $n")
|
||||
|
||||
+{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)
|
||||
-{n}(a::ModInt{n}) = ModInt{n}(-a.k)
|
||||
|
||||
inv{n}(a::ModInt{n}) = ModInt{n}(invmod(a.k, n))
|
||||
/{n}(a::ModInt{n}, b::ModInt{n}) = a*inv(b) # broaden for non-coprime?
|
||||
|
||||
Base.promote_rule{n}(::Type{ModInt{n}}, ::Type{Int}) = ModInt{n}
|
||||
Base.convert{n}(::Type{ModInt{n}}, i::Int) = ModInt{n}(i)
|
||||
|
||||
end # module
|
||||
Reference in New Issue
Block a user