Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Mar 19, 2018
1 parent e16d3d7 commit 1e5b19a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/Primes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ else
end

using Base: BitSigned
using Base.Checked.checked_neg
using Base.Checked: checked_neg

export ismersenneprime, isrieselprime, nextprime, prevprime, prime, prodfactors, radical, totient

Expand Down Expand Up @@ -240,7 +240,7 @@ isprime(n::Int128) = n < 2 ? false :
# https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm
# http://maths-people.anu.edu.au/~brent/pub/pub051.html
#
function factor!{T<:Integer,K<:Integer}(n::T, h::Associative{K,Int})
function factor!(n::T, h::AbstractDict{K,Int}) where {T<:Integer,K<:Integer}
# check for special cases
if n < 0
h[-1] = 1
Expand Down Expand Up @@ -310,7 +310,7 @@ factor(n::T) where {T<:Integer} = factor!(n, Factorization{T}())
factor(ContainerType, n::Integer) -> ContainerType
Return the factorization of `n` stored in a `ContainerType`, which must be a
subtype of `Associative` or `AbstractArray`, a `Set`, or an `IntSet`.
subtype of `AbstractDict` or `AbstractArray`, a `Set`, or an `BitSet`.
```julia
julia> factor(DataStructures.SortedDict, 100)
Expand Down Expand Up @@ -342,7 +342,7 @@ julia> factor(Set, 100)
Set([2,5])
```
"""
factor(::Type{D}, n::T) where {T<:Integer, D<:Associative} = factor!(n, D(Dict{T,Int}()))
factor(::Type{D}, n::T) where {T<:Integer, D<:AbstractDict} = factor!(n, D(Dict{T,Int}()))
factor(::Type{A}, n::T) where {T<:Integer, A<:AbstractArray} = A(factor(Vector{T}, n))
factor(::Type{Vector{T}}, n::T) where {T<:Integer} =
mapreduce(collect, vcat, Vector{T}(), [repeated(k, v) for (k, v) in factor(n)])
Expand All @@ -353,7 +353,7 @@ factor(::Type{T}, n) where {T<:Any} = throw(MethodError(factor, (T, n)))
prodfactors(factors)
Compute `n` (or the radical of `n` when `factors` is of type `Set` or
`IntSet`) where `factors` is interpreted as the result of
`BitSet`) where `factors` is interpreted as the result of
`factor(typeof(factors), n)`. Note that if `factors` is of type
`AbstractArray` or `Primes.Factorization`, then `prodfactors` is equivalent
to `Base.prod`.
Expand All @@ -365,7 +365,7 @@ julia> prodfactors(factor(100))
"""
function prodfactors end

prodfactors(factors::Associative{K}) where {K} = isempty(factors) ? one(K) : prod(p^i for (p, i) in factors)
prodfactors(factors::AbstractDict{K}) where {K} = isempty(factors) ? one(K) : prod(p^i for (p, i) in factors)
prodfactors(factors::Union{AbstractArray, Set, BitSet}) = prod(factors)

"""
Expand All @@ -388,7 +388,7 @@ julia> radical(2*2*3)
"""
radical(n) = prod(factor(Set, n))

function pollardfactors!(n::T, h::Associative{K,Int}) where {T<:Integer,K<:Integer}
function pollardfactors!(n::T, h::AbstractDict{K,Int}) where {T<:Integer,K<:Integer}
while true
c::T = rand(1:(n - 1))
G::T = 1
Expand Down
4 changes: 2 additions & 2 deletions src/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ struct Factorization{T<:Integer} <: AbstractDict{T, Int}
Factorization{T}() where {T<:Integer} = new{T}(Vector{Pair{T, Int}}())
end

function Factorization{T}(d::Associative) where T<:Integer
function Factorization{T}(d::AbstractDict) where T<:Integer
f = Factorization{T}()
append!(f.pe, sort!(collect(d)))
f
end

Base.convert(::Type{Factorization}, d::Associative{T}) where {T} = Factorization{T}(d)
Base.convert(::Type{Factorization}, d::AbstractDict{T}) where {T} = Factorization{T}(d)

Base.start(f::Factorization) = start(f.pe)
Base.next(f::Factorization, i) = next(f.pe, i)
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ end

# factor sets
@test factor(Set, 100) == Set([2, 5])
@test factor(IntSet, 100) == IntSet([2, 5])
@test factor(BitSet, 100) == BitSet([2, 5])

# factor other things and fail
@test_throws MethodError factor(Int, 10)
Expand Down Expand Up @@ -255,7 +255,7 @@ ismersenneprime(9, check=false)
@test isrieselprime(3, BigInt(2)^607 - 1) # Case 2
@test_throws ErrorException isrieselprime(20, 31) # Case `else`

# @testset "Factorization{$T} as an Associative" for T = (Int, UInt, BigInt)
# @testset "Factorization{$T} as an AbstractDict" for T = (Int, UInt, BigInt)
for T = (Int, UInt, BigInt)
d = Dict(map(Pair, rand(T(1):T(100), 30), 1:30))
f = Factorization{T}(d)
Expand Down

0 comments on commit 1e5b19a

Please sign in to comment.