1
1
# This file includes code that was formerly a part of Julia. License is MIT: http://julialang.org/license
2
2
module Primes
3
3
4
- using Base. Iterators: repeated
4
+ using Base. Iterators: repeated, rest
5
5
6
6
import Base: iterate, eltype, IteratorSize, IteratorEltype
7
7
using Base: BitSigned
@@ -591,7 +591,7 @@ given by `f`. This method may be preferable to [`totient(::Integer)`](@ref)
591
591
when the factorization can be reused for other purposes.
592
592
"""
593
593
function totient (f:: Factorization{T} ) where T <: Integer
594
- if ! isempty (f) && first ( first ( f)) == 0
594
+ if iszero ( sign ( f))
595
595
throw (ArgumentError (" ϕ(0) is not defined" ))
596
596
end
597
597
result = one (T)
@@ -908,7 +908,7 @@ prevprimes(start::T, n::Integer) where {T<:Integer} =
908
908
collect (T, Iterators. take (prevprimes (start), n))
909
909
910
910
"""
911
- divisors(n::T ) -> Vector{T}
911
+ divisors(n::Integer ) -> Vector
912
912
913
913
Return a vector with the positive divisors of `n`.
914
914
@@ -918,6 +918,8 @@ lexicographic (rather than numerical) order.
918
918
919
919
`divisors(-n)` is equivalent to `divisors(n)`.
920
920
921
+ For convenience, `divisors(0)` returns `[]`.
922
+
921
923
# Example
922
924
923
925
```jldoctest
@@ -935,46 +937,49 @@ julia> divisors(60)
935
937
15 # 5 * 3
936
938
30 # 5 * 3 * 2
937
939
60 # 5 * 3 * 2 * 2
940
+
941
+ julia> divisors(-10)
942
+ 4-element Vector{Int64}:
943
+ 1
944
+ 2
945
+ 5
946
+ 10
947
+
948
+ julia> divisors(0)
949
+ Int64[]
938
950
```
939
951
"""
940
- function divisors (n:: T ):: Vector{T} where {T<: Integer }
941
- n:: T = abs (n)
952
+ function divisors (n:: T ) where {T<: Integer }
953
+ n = abs (n)
942
954
if iszero (n)
943
- throw ( ArgumentError ( " divisors function is not defined for 0 " ))
955
+ return T[]
944
956
elseif isone (n)
945
- return T [n]
957
+ return [n]
946
958
else
947
959
return divisors (factor (n))
948
960
end
949
961
end
950
962
951
963
"""
952
- divisors(f::Factorization{T} ) -> Vector{T}
964
+ divisors(f::Factorization) -> Vector
953
965
954
966
Return a vector with the positive divisors of the number whose factorization is `f`.
955
967
Divisors are sorted lexicographically, rather than numerically.
956
968
"""
957
- function divisors (f:: Factorization{T} ):: Vector{T} where {T<: Integer }
958
- factors = collect (f)
959
-
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
966
- return T[one (T)]
967
- else
968
- deleteat! (factors, 1 )
969
- end
969
+ function divisors (f:: Factorization{T} ) where {T<: Integer }
970
+ sgn = sign (f)
971
+ if iszero (sgn) # n == 0
972
+ return T[]
973
+ elseif isempty (f) || length (f) == 1 && sgn < 0 # n == 1 or n == -1
974
+ return [one (T)]
970
975
end
971
976
972
- i:: Int = 1
973
- m :: Int = 1
974
- divs = Vector {T} (undef, prod (x -> x. second + 1 , factors ))
977
+ i = m = 1
978
+ fs = rest (f, 1 + (sgn < 0 ))
979
+ divs = Vector {T} (undef, prod (x -> x. second + 1 , fs ))
975
980
divs[i] = one (T)
976
981
977
- for (p, k) in factors
982
+ for (p, k) in fs
978
983
i = 1
979
984
for _ in 1 : k
980
985
for j in i: (i+ m- 1 )
0 commit comments