[Blog Post] A Fun Exploration of Perfect, Abundant, and Deficient Numbers

yes, but of course they are much slower than native machine types (e.g. Int64/Float64)

I have recoded the first part of your post using them… I am not a “big” user of them, so I do not think all the specification I added are needed, I think Julia automatically recast operations between Int and BigInt to a BigInt for example, still… this seems to work:

using Pkg
Pkg.activate(temp=true)
Pkg.add("Primes")

import Primes: factor

function divisors(n)
    d = BigInt[BigInt(1)]
    for (p, e) in factor(n)
        t = BigInt[]
        r = BigInt(1)
        for i in BigInt(1):e
            r *= p
            for u in d
                push!(t, u * r)
            end
	    end
	append!(d, t)
    end
    return sort!(d)
end

i = BigInt(1) 
deficient_numbers = BigInt[]
abundant_numbers = BigInt[]
while true
  divisor_sum = divisors(i)[1:end-1] |> sum
  if divisor_sum < i && length(deficient_numbers) != 1000
    push!(deficient_numbers, i)
  elseif divisor_sum > i && length(abundant_numbers) != 1000
    push!(abundant_numbers, i)
  end
  i += BigInt(1)
  length(abundant_numbers) == 1000 && length(deficient_numbers) == 1000 ? break : continue
end
1 Like