@@ -910,72 +910,71 @@ prevprimes(start::T, n::Integer) where {T<:Integer} =
910
910
"""
911
911
divisors(n::T) -> Vector{T}
912
912
913
- Return a vector of all positive divisors of an integer `n`.
913
+ Return a vector with the positive divisors of `n`.
914
914
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)`
916
916
returns a vector of length (k₁ + 1)⋯(kₘ + 1) containing the divisors of `n` in
917
917
lexicographic (rather than numerical) order.
918
918
919
- ```julia
919
+ `divisors(-n)` is equivalent to `divisors(n)`.
920
+
921
+ # Example
922
+
923
+ ```jldoctest
920
924
julia> divisors(60)
921
925
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
934
938
```
935
-
936
- `divisors(-n)` is equivalent to `divisors(n)`.
937
-
938
- `divisors(0)` returns `[]`.
939
939
"""
940
940
function divisors (n:: T ):: Vector{T} where {T<: Integer }
941
+ n:: T = abs (n)
941
942
if iszero (n)
942
- return T[]
943
+ throw ( ArgumentError ( " divisors function is not defined for 0 " ))
943
944
elseif isone (n)
944
945
return T[n]
945
- elseif n < 0
946
- return divisors (abs (n))
947
946
else
948
947
return divisors (factor (n))
949
948
end
950
949
end
951
950
952
951
"""
953
- divisors(factors ::Factorization{T}) -> Vector{T}
952
+ divisors(f ::Factorization{T}) -> Vector{T}
954
953
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 `.
956
955
Divisors are sorted lexicographically, rather than numerically.
957
956
"""
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)
960
959
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
967
966
return T[one (T)]
968
967
else
969
- pe = pe[ 2 : end ]
968
+ deleteat! (factors, 1 )
970
969
end
971
970
end
972
971
973
972
i:: Int = 1
974
973
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 ))
976
975
divs[i] = one (T)
977
976
978
- for (p, k) in pe
977
+ for (p, k) in factors
979
978
i = 1
980
979
for _ in 1 : k
981
980
for j in i: (i+ m- 1 )
0 commit comments