Open
Description
Here is a example of two number theory functions made fast by eachfactor
"""
`prime_residues(n)` the numbers less than `n` and prime to `n`
julia> [prime_residues(24)]
1-element Vector{Vector{Int64}}:
[1, 5, 7, 11, 13, 17, 19, 23]
"""
function prime_residues(n)
if n==1 return [0] end
pp=trues(n) # use a sieve to go fast
for (p,np) in eachfactor(n)
pp[p:p:n].=false
end
(1:n)[pp]
end
"""
`divisors(n)` the increasing list of divisors of `n`.
julia> [divisors(24)]
1-element Vector{Vector{Int64}}:
[1, 2, 3, 4, 6, 8, 12, 24]
"""
function divisors(n::Integer)::Vector{Int}
sort(vec(map(prod,Iterators.product((p.^(0:m) for (p,m) in eachfactor(n))...))))
end
Metadata
Metadata
Assignees
Labels
No labels