Created
January 29, 2015 14:21
-
-
Save JeffBezanson/7c1f4e10e761e7195add to your computer and use it in GitHub Desktop.
factor function
This file contains hidden or 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
function factor{T<:Integer}(n::T) | |
0 < n || error("number to be factored must be positive") | |
h = Dict{T,Int}() | |
n == 1 && return h | |
local s::T, p::T | |
s = isqrt(n) | |
p = 2 | |
while p <= s | |
if n % p == 0 | |
while n % p == 0 | |
h[p] = get(h,p,0) + 1 | |
n = div(n,p) | |
end | |
s = isqrt(n) | |
end | |
p += (p==2 ? 1 : 2) | |
end | |
return h | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment