Skip to content

Commit e7021b6

Browse files
authored
add prodfactors and radical functions (#42)
1 parent e3cc1d1 commit e7021b6

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

docs/src/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
```@docs
66
Primes.factor
7+
Primes.prodfactors
8+
Primes.radical
79
```
810

911
## Generating prime numbers

src/Primes.jl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ end
1414
using Base: BitSigned
1515
using Base.Checked.checked_neg
1616

17-
export ismersenneprime, isrieselprime, nextprime, prevprime, prime
17+
export ismersenneprime, isrieselprime, nextprime, prevprime, prime, prodfactors, radical
1818

1919
include("factorization.jl")
2020

@@ -346,6 +346,44 @@ factor{T<:Integer}(::Type{Vector{T}}, n::T) =
346346
factor{T<:Integer, S<:Union{Set,IntSet}}(::Type{S}, n::T) = S(keys(factor(n)))
347347
factor{T<:Any}(::Type{T}, n) = throw(MethodError(factor, (T, n)))
348348

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))
349387

350388
function pollardfactors!{T<:Integer,K<:Integer}(n::T, h::Associative{K,Int})
351389
while true

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,18 @@ end
339339
@test prime(T, n) == p
340340
end
341341
end
342+
343+
for T in (Int, UInt, BigInt)
344+
for n in rand(T(1):T(100000), 10)
345+
for C = (Factorization, Vector, Dict)
346+
@test prodfactors(factor(C, n)) == n
347+
end
348+
if Primes.radical(n) == n
349+
for C = (Set, IntSet)
350+
@test prodfactors(factor(C, n)) == n
351+
end
352+
end
353+
end
354+
@test prodfactors(factor(Set, T(123456))) == 3858
355+
@test prod(factor(T(123456))) == 123456
356+
end

0 commit comments

Comments
 (0)