I\'ve written a program that attempts to find Amicable Pairs. This requires finding the sums of the proper divisors of numbers.
Here is my current sumOfDiviso
The method will work, but will be slow. "How big are your numbers?" determines the method to use:
The question in the title (and the last line) seems to have little to do with the actual body of the question. If you're trying to find amicable pairs, or computing the sum of divisors for many numbers, then separately factorising each number (even with the fastest possible algorithm) is absolutely an inefficient way to do it.
The sum-of-divisors function, σ(n) = (sum of divisors of n)
, is a multiplicative function: for relatively prime m and n, we have σ(mn) = σ(m)σ(n)
, so
σ(p1k1…prkr) = [(p1k1+1-1)/(p1-1)]…[(prkr+1-1)/(pr-1)].
So you would use any simple sieve (e.g. an augmented version of the Sieve of Eratosthenes) to find the primes up to n
, and, in the process, the factorisation of all numbers up to n. (For example, as you do your sieve, store the smallest prime factor of each n. Then you can later factorize any number n
by iterating.) This would be faster (overall) than using any separate factorization algorithm several times.
BTW: several known lists of amicable pairs already exist (see e.g. here and the links at MathWorld) – so are you trying to extend the record, or doing it just for fun?