Created
June 9, 2013 22:40
-
-
Save johnmyleswhite/5745567 to your computer and use it in GitHub Desktop.
PrimeArray: Using arbitrary precision integers to define arrays
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
type PrimeArray | |
n::BigInt | |
end | |
PrimeArray() = PrimeArray(BigInt(1)) | |
function Base.getindex(a::PrimeArray, i::Integer) | |
if i < 0 || i > length(Base.PRIMES) | |
throw(BoundsError()) | |
end | |
tmp = copy(a.n) | |
res = 0 | |
p = Base.PRIMES[i] | |
while mod(tmp, p) == BigInt(0) | |
tmp = fld(tmp, p) | |
res = res + 1 | |
end | |
return res | |
end | |
function Base.setindex!(a::PrimeArray, v::Integer, i::Integer) | |
if i < 0 || i > length(Base.PRIMES) | |
throw(BoundsError()) | |
end | |
p = Base.PRIMES[i] | |
while mod(a.n, p) == BigInt(0) | |
a.n = fld(a.n, p) | |
end | |
a.n = a.n * BigInt(p)^v | |
return v | |
end | |
a = PrimeArray() | |
a[1] = 5 | |
a[2] = 7 | |
a[11] = 15 | |
a[1] | |
a[2] | |
a[11] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment