Skip to content

Instantly share code, notes, and snippets.

@brv00
Created March 25, 2020 04:36
Show Gist options
  • Save brv00/912d7609d715c9b42fcf16280e092917 to your computer and use it in GitHub Desktop.
Save brv00/912d7609d715c9b42fcf16280e092917 to your computer and use it in GitHub Desktop.
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