Jump to content

Factors of an integer: Difference between revisions

m
→‎{{header|Julia}}: use eachfactor
m (→‎{{header|Phix}}: erm, there be 3 of em)
m (→‎{{header|Julia}}: use eachfactor)
Line 3,627:
<syntaxhighlight lang="julia">using Primes
 
""" Return the factors of n, including 1, n """
function factors(n)
function factors(n::T)::Vector{T} where T <: Integer
f = [one(n)]
sort(vec(map(prod, Iterators.product((p.^(0:m) for (p,e m) in factoreachfactor(n))...))))
f = reduce(vcat, [f*p^j for j in 1:e], init=f)
end
return length(f) == 1 ? [one(n), n] : sort!(f)
end
 
Line 3,649 ⟶ 3,646:
The factors of 53 are: [1, 53]
0.000102 seconds (35 allocations: 1.516 KiB)
 
 
The factors of 64 are: [1, 2, 4, 8, 16, 32, 64]
0.000093 seconds (56 allocations: 3.172 KiB)
4,105

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.