12
12
end
13
13
14
14
using Base: BitSigned
15
- using Base. Checked. checked_neg
15
+ using Base. Checked: checked_neg
16
16
17
17
export ismersenneprime, isrieselprime, nextprime, prevprime, prime, prodfactors, radical, totient
18
18
@@ -101,7 +101,7 @@ function primesmask(lo::Int, hi::Int)
101
101
end
102
102
return sieve
103
103
end
104
- primesmask {T<:Integer} (lo:: T , hi:: T ) = lo ≤ hi ≤ typemax (Int) ? primesmask (Int (lo), Int (hi)) :
104
+ primesmask (lo:: Integer , hi:: Integer ) = lo ≤ hi ≤ typemax (Int) ? primesmask (Int (lo), Int (hi)) :
105
105
throw (ArgumentError (" Both endpoints of the interval to sieve must be ≤ $(typemax (Int)) , got $lo and $hi ." ))
106
106
107
107
primesmask (limit:: Int ) = primesmask (1 , limit)
@@ -240,7 +240,7 @@ isprime(n::Int128) = n < 2 ? false :
240
240
# https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm
241
241
# http://maths-people.anu.edu.au/~brent/pub/pub051.html
242
242
#
243
- function factor! {T<:Integer,K<:Integer} (n:: T , h:: Associative {K,Int} )
243
+ function factor! (n:: T , h:: AbstractDict {K,Int} ) where {T <: Integer ,K <: Integer }
244
244
# check for special cases
245
245
if n < 0
246
246
h[- 1 ] = 1
@@ -303,14 +303,14 @@ julia> collect(factor(0))
303
303
0=>1
304
304
```
305
305
"""
306
- factor {T<:Integer} (n:: T ) = factor! (n, Factorization {T} ())
306
+ factor (n:: T ) where {T <: Integer } = factor! (n, Factorization {T} ())
307
307
308
308
309
309
"""
310
310
factor(ContainerType, n::Integer) -> ContainerType
311
311
312
312
Return the factorization of `n` stored in a `ContainerType`, which must be a
313
- subtype of `Associative ` or `AbstractArray`, a `Set`, or an `IntSet `.
313
+ subtype of `AbstractDict ` or `AbstractArray`, a `Set`, or an `BitSet `.
314
314
315
315
```julia
316
316
julia> factor(DataStructures.SortedDict, 100)
@@ -342,18 +342,18 @@ julia> factor(Set, 100)
342
342
Set([2,5])
343
343
```
344
344
"""
345
- factor {T<:Integer, D<:Associative} (:: Type{D} , n:: T ) = factor! (n, D (Dict {T,Int} ()))
346
- factor {T<:Integer, A<:AbstractArray} (:: Type{A} , n:: T ) = A (factor (Vector{T}, n))
347
- factor {T<:Integer} (:: Type{Vector{T}} , n:: T ) =
345
+ factor (:: Type{D} , n:: T ) where {T <: Integer , D <: AbstractDict } = factor! (n, D (Dict {T,Int} ()))
346
+ factor (:: Type{A} , n:: T ) where {T <: Integer , A <: AbstractArray } = A (factor (Vector{T}, n))
347
+ factor (:: Type{Vector{T}} , n:: T ) where {T <: Integer } =
348
348
mapreduce (collect, vcat, Vector {T} (), [repeated (k, v) for (k, v) in factor (n)])
349
- factor {T <:Integer, S<:Union{Set,IntSet}} ( :: Type{S} , n :: T ) = S (keys (factor (n)))
350
- factor {T<:Any} (:: Type{T} , n) = throw (MethodError (factor, (T, n)))
349
+ factor ( :: Type{S} , n :: T ) where {T <: Integer , S<: Union{Set,BitSet} } = S (keys (factor (n)))
350
+ factor (:: Type{T} , n) where {T <: Any } = throw (MethodError (factor, (T, n)))
351
351
352
352
"""
353
353
prodfactors(factors)
354
354
355
355
Compute `n` (or the radical of `n` when `factors` is of type `Set` or
356
- `IntSet `) where `factors` is interpreted as the result of
356
+ `BitSet `) where `factors` is interpreted as the result of
357
357
`factor(typeof(factors), n)`. Note that if `factors` is of type
358
358
`AbstractArray` or `Primes.Factorization`, then `prodfactors` is equivalent
359
359
to `Base.prod`.
@@ -365,8 +365,8 @@ julia> prodfactors(factor(100))
365
365
"""
366
366
function prodfactors end
367
367
368
- prodfactors {K} (factors:: Associative {K} ) = isempty (factors) ? one (K) : prod (p^ i for (p, i) in factors)
369
- prodfactors (factors:: Union{AbstractArray, Set, IntSet } ) = prod (factors)
368
+ prodfactors (factors:: AbstractDict {K} ) where {K} = isempty (factors) ? one (K) : prod (p^ i for (p, i) in factors)
369
+ prodfactors (factors:: Union{AbstractArray, Set, BitSet } ) = prod (factors)
370
370
371
371
"""
372
372
Base.prod(factors::Primes.Factorization{T}) -> T
@@ -388,7 +388,7 @@ julia> radical(2*2*3)
388
388
"""
389
389
radical (n) = prod (factor (Set, n))
390
390
391
- function pollardfactors! {T<:Integer,K<:Integer} (n:: T , h:: Associative {K,Int} )
391
+ function pollardfactors! (n:: T , h:: AbstractDict {K,Int} ) where {T <: Integer ,K <: Integer }
392
392
while true
393
393
c:: T = rand (1 : (n - 1 ))
394
394
G:: T = 1
@@ -510,7 +510,7 @@ Compute the Euler totient function of the number whose prime factorization is
510
510
given by `f`. This method may be preferable to [`totient(::Integer)`](@ref)
511
511
when the factorization can be reused for other purposes.
512
512
"""
513
- function totient {T <: Integer} (f:: Factorization{T} )
513
+ function totient (f:: Factorization{T} ) where T <: Integer
514
514
if ! isempty (f) && first (first (f)) == 0
515
515
throw (ArgumentError (" ϕ(0) is not defined" ))
516
516
end
@@ -541,9 +541,9 @@ function add_!(n::BigInt, x::Int)
541
541
# TODO : Change `Any` to `Ref{BigInt}` when 0.6 support is dropped.
542
542
# The two have the same effect but `Ref{BigInt}` won't be optimized on 0.6.
543
543
if x < 0
544
- ccall ((:__gmpz_sub_ui , :libgmp ), Void , (Any, Any, Culong), n, n, - x)
544
+ ccall ((:__gmpz_sub_ui , :libgmp ), Cvoid , (Any, Any, Culong), n, n, - x)
545
545
else
546
- ccall ((:__gmpz_add_ui , :libgmp ), Void , (Any, Any, Culong), n, n, x)
546
+ ccall ((:__gmpz_add_ui , :libgmp ), Cvoid , (Any, Any, Culong), n, n, x)
547
547
end
548
548
n
549
549
end
@@ -638,7 +638,7 @@ function prevprime(n::Integer, i::Integer=1)
638
638
end
639
639
640
640
"""
641
- prime{T} (::Type{T }=Int, i::Integer)
641
+ prime(::Type{<:Integer }=Int, i::Integer)
642
642
643
643
The `i`-th prime number.
644
644
@@ -651,7 +651,7 @@ julia> prime(3)
651
651
652
652
```
653
653
"""
654
- prime {T<:Integer} (:: Type{T} , i:: Integer ) = i < 0 ? throw (DomainError (i)) : nextprime (T (2 ), i)
654
+ prime (:: Type{T} , i:: Integer ) where {T <: Integer } = i < 0 ? throw (DomainError (i)) : nextprime (T (2 ), i)
655
655
prime (i:: Integer ) = prime (Int, i)
656
656
657
657
end # module
0 commit comments