Skip to content

Instantly share code, notes, and snippets.

@JeffBezanson
Created January 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffBezanson/7c1f4e10e761e7195add to your computer and use it in GitHub Desktop.
Save JeffBezanson/7c1f4e10e761e7195add to your computer and use it in GitHub Desktop.
factor function
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