Divisors
February 14, 2012
We discussed divisors in a previous exercise. There, we factored a number and applied various loops to list the divisors of the number, their count, and their sum. That works fine for one or a few numbers. But if you have to find the divisors of a lot of numbers, it makes sense to compute the solutions all at once using a sieve. Start with an empty array d of the desired size n; then, for each i from 1 to n, mark i as a divisor of each multiple of i. For instance, with an array of 12:
1: 1
2: 1 2
3: 1 3
4: 1 2 4
5: 1 5
6: 1 2 3 6
7: 1 7
8: 1 2 4 8
9: 1 3 9
10: 1 2 5 10
11: 1 11
12: 1 2 3 4 6 12
Depending on your needs, you can make a list of the divisors, or count them, or compute their sum as you go along.
Your task is to write a function that builds a table of divisors as in the sample. Use it to find the perfect numbers and amicable pairs less than 10000, where perfect numbers are those whose divisors sum to twice the number and amicable pairs are those numbers whose sum of divisors (excluding the number itself) are the other number of the pair. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
First time posting something to this website. Here’s a perl solution to the problem – http://pastebin.com/4gaWFTzJ
Paul,
If I’m not mistaken, it doesn’t look like your perl solution is actually seiving in order to determine the divisors of each number, which was the point of the exercise. It looks instead like you are performing modular arithmetic on each number $n and checking for a remainder against each possible divisor, $_. Am I mistaken?
Here’s a java solution: http://pastebin.com/6xL9BwS0
Thanks.
Comprehensions :) With multiple filters. (Ignore the element at index 0.)
The divisor lists are redundant. Their sums do the work. In amicable, the condition to exclude double numbers and duplicates also excludes the double zero. In perfect, the 0 is excluded by its very own condition.
This site seems to have attracted mainly a crowd of functional programmers with crazy powerful language facilities at their disposal.
We need some more love given to the gritty implementation details of these solutions (particularly when the posed problem is simple).
But enough trying to justify my approach..
No fancy data structures, no analytical computations.. purely imperative number crunching:
I would be interested in seeing some of the functional solutions’ performance!
Ardnew: I added timing code at http://programmingpraxis.codepad.org/WlLh3Gfx. Time was 10ms to calculate the perfect numbers and 90ms to calculate the amicable pairs, giving a total 100ms which barely beats your 104ms. I ran out of fingers before I finished counting your lines of code, which might say something about functional programmers with crazy powerful language facilities at their disposal.
haha, you’ll need all the fingers and toes of everyone on the block to count my lines of code
And I would like to point out the timing result I posted is for process invocation, building the divisors table, finding both perfects and amicable pairs, and then printing to screen.
Timing only the amicable pair and perfect number calls (as your scheme implementation does) results in significantly less time:
Actually, both perfect and amicable call update, so the divisor table is built twice in the timings I gave. A version that extracts the calculation of the divisors table is given at http://programmingpraxis.codepad.org/TLMWMAkg. The time to calculate 50000 entries in the divisors tables is 80ms, and the time to compute the perfect numbers and amicable pairs is 0.
Very nice, cache hits always help runtime
And, that’s it! :)
Output:
[…] find the divisors of a bunch of numbers, in sequence, you can sieve for them; we also did that in a previous exercise. Once you know the divisor-count for each number from 1 to n, a simple sequential scan looking for […]