|
14 | 14 | using Base: BitSigned
|
15 | 15 | using Base.Checked.checked_neg
|
16 | 16 |
|
17 |
| -export ismersenneprime, isrieselprime, nextprime, prevprime, prime |
| 17 | +export ismersenneprime, isrieselprime, nextprime, prevprime, prime, prodfactors, radical |
18 | 18 |
|
19 | 19 | include("factorization.jl")
|
20 | 20 |
|
@@ -346,6 +346,44 @@ factor{T<:Integer}(::Type{Vector{T}}, n::T) =
|
346 | 346 | factor{T<:Integer, S<:Union{Set,IntSet}}(::Type{S}, n::T) = S(keys(factor(n)))
|
347 | 347 | factor{T<:Any}(::Type{T}, n) = throw(MethodError(factor, (T, n)))
|
348 | 348 |
|
| 349 | +""" |
| 350 | + prodfactors(factors) |
| 351 | +
|
| 352 | +Compute `n` (or the radical of `n` when `factors` is of type `Set` or |
| 353 | +`IntSet`) where `factors` is interpreted as the result of |
| 354 | +`factor(typeof(factors), n)`. Note that if `factors` is of type |
| 355 | +`AbstractArray` or `Primes.Factorization`, then `prodfactors` is equivalent |
| 356 | +to `Base.prod`. |
| 357 | +
|
| 358 | +```jldoctest |
| 359 | +julia> prodfactors(factor(100)) |
| 360 | +100 |
| 361 | +``` |
| 362 | +""" |
| 363 | +function prodfactors end |
| 364 | + |
| 365 | +prodfactors(factors::Associative) = prod(p^i for (p, i) in factors) |
| 366 | +prodfactors(factors::Union{AbstractArray, Set, IntSet}) = prod(factors) |
| 367 | + |
| 368 | +""" |
| 369 | + Base.prod(factors::Primes.Factorization{T}) -> T |
| 370 | +
|
| 371 | +Compute `n` where `factors` is interpreted as the result of `factor(n)`. |
| 372 | +""" |
| 373 | +Base.prod(factors::Factorization) = prodfactors(factors) |
| 374 | + |
| 375 | +""" |
| 376 | + radical(n::Integer) |
| 377 | +
|
| 378 | +Compute the radical of `n`, i.e. the largest square-free divisor of `n`. |
| 379 | +This is equal to the product of the distinct prime numbers dividing `n`. |
| 380 | +
|
| 381 | +```jldoctest |
| 382 | +julia> radical(2*2*3) |
| 383 | +6 |
| 384 | +``` |
| 385 | +""" |
| 386 | +radical(n) = prod(factor(Set, n)) |
349 | 387 |
|
350 | 388 | function pollardfactors!{T<:Integer,K<:Integer}(n::T, h::Associative{K,Int})
|
351 | 389 | while true
|
|
0 commit comments