Skip to content

Commit 45f42e3

Browse files
committed
Make divisors(0) throw ArgumentError
Also, iterate over factorization using its own api rather than accessing its pe field directly.
1 parent 62c8597 commit 45f42e3

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

src/Primes.jl

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -910,72 +910,71 @@ prevprimes(start::T, n::Integer) where {T<:Integer} =
910910
"""
911911
divisors(n::T) -> Vector{T}
912912
913-
Return a vector of all positive divisors of an integer `n`.
913+
Return a vector with the positive divisors of `n`.
914914
915-
For an integer `n` with prime factorization `n = p₁^k₁ ⋯ pₘ^kₘ`, `divisors(n)`
915+
For a nonzero integer `n` with prime factorization `n = p₁^k₁ ⋯ pₘ^kₘ`, `divisors(n)`
916916
returns a vector of length (k₁ + 1)⋯(kₘ + 1) containing the divisors of `n` in
917917
lexicographic (rather than numerical) order.
918918
919-
```julia
919+
`divisors(-n)` is equivalent to `divisors(n)`.
920+
921+
# Example
922+
923+
```jldoctest
920924
julia> divisors(60)
921925
12-element Vector{Int64}:
922-
1 # 1
923-
2 # 2
924-
4 # 2 * 2
925-
3 # 3
926-
6 # 3 * 2
927-
12 # 3 * 2 * 2
928-
5 # 5
929-
10 # 5 * 2
930-
20 # 5 * 2 * 2
931-
15 # 5 * 3
932-
30 # 5 * 3 * 2
933-
60 # 5 * 3 * 2 * 2
926+
1 # 1
927+
2 # 2
928+
4 # 2 * 2
929+
3 # 3
930+
6 # 3 * 2
931+
12 # 3 * 2 * 2
932+
5 # 5
933+
10 # 5 * 2
934+
20 # 5 * 2 * 2
935+
15 # 5 * 3
936+
30 # 5 * 3 * 2
937+
60 # 5 * 3 * 2 * 2
934938
```
935-
936-
`divisors(-n)` is equivalent to `divisors(n)`.
937-
938-
`divisors(0)` returns `[]`.
939939
"""
940940
function divisors(n::T)::Vector{T} where {T<:Integer}
941+
n::T = abs(n)
941942
if iszero(n)
942-
return T[]
943+
throw(ArgumentError("divisors function is not defined for 0"))
943944
elseif isone(n)
944945
return T[n]
945-
elseif n < 0
946-
return divisors(abs(n))
947946
else
948947
return divisors(factor(n))
949948
end
950949
end
951950

952951
"""
953-
divisors(factors::Factorization{T}) -> Vector{T}
952+
divisors(f::Factorization{T}) -> Vector{T}
954953
955-
Return a vector containing the divisors of the number described by `factors`.
954+
Return a vector with the positive divisors of the number whose factorization is `f`.
956955
Divisors are sorted lexicographically, rather than numerically.
957956
"""
958-
function divisors(factors::Primes.Factorization{T})::Vector{T} where {T<:Integer}
959-
pe = factors.pe
957+
function divisors(f::Factorization{T})::Vector{T} where {T<:Integer}
958+
factors = collect(f)
960959

961-
if isempty(pe)
962-
return T[one(T)] # n == 1
963-
elseif pe[1][1] == 0 # n == 0
964-
return T[]
965-
elseif pe[1][1] == -1 # n < 0
966-
if length(pe) == 1 # n == -1
960+
if isempty(factors) # n == 1
961+
return T[one(T)]
962+
elseif iszero(first(first(factors))) # n == 0
963+
throw(ArgumentError("divisors function is not defined for 0"))
964+
elseif first(first(factors)) == -1 # n < 0
965+
if length(factors) == 1 # n == -1
967966
return T[one(T)]
968967
else
969-
pe = pe[2:end]
968+
deleteat!(factors, 1)
970969
end
971970
end
972971

973972
i::Int = 1
974973
m::Int = 1
975-
divs = Vector{T}(undef, prod(x -> x.second + 1, pe))
974+
divs = Vector{T}(undef, prod(x -> x.second + 1, factors))
976975
divs[i] = one(T)
977976

978-
for (p, k) in pe
977+
for (p, k) in factors
979978
i = 1
980979
for _ in 1:k
981980
for j in i:(i+m-1)

0 commit comments

Comments
 (0)