Created
March 25, 2020 04:36
-
-
Save brv00/912d7609d715c9b42fcf16280e092917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Primes | |
struct ExponentArray | |
maxexponents::Array{Integer} | |
end | |
function (incr!::ExponentArray)(x) | |
for d = 1:lastindex(x) | |
if x[d] ≥ incr!.maxexponents[d] | |
x[d] = zero(x[d]) | |
else | |
x[d] += one(x[d]) | |
break | |
end | |
end | |
x | |
end | |
function alldivisors(x) | |
primefactors = collect(factor(x)) | |
bases = getindex.(primefactors, 1) | |
incr! = ExponentArray(getindex.(primefactors, 2)) | |
exponents = zero.(incr!.maxexponents) | |
ndivisors = prod(1 .+ incr!.maxexponents) | |
ret = Vector{typeof(x)}(undef, ndivisors) | |
for i in 1:ndivisors | |
ret[i] = prod(bases.^exponents) | |
incr!(exponents) | |
end | |
ret | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment