## Divisors

### February 14, 2012

We sieve the list of divisors, their count and their sum all in the same double-nested loops:

`(define (divisors n)`

(let ((divs (make-vector (+ n 1) (list)))

(cnts (make-vector (+ n 1) 0))

(sums (make-vector (+ n 1) 0)))

(do ((i 1 (+ i 1))) ((<= n i) (values divs cnts sums))

(do ((j i (+ j i))) ((<= n j))

(vector-set! divs j (cons i (vector-ref divs j)))

(vector-set! cnts j (+ 1 (vector-ref cnts j)))

(vector-set! sums j (+ i (vector-ref sums j)))))))

We will store the lists of divisors, counts and sums in global variables that are updated to the maximum size desired:

`(define divs #f)`

(define cnts #f)

(define sums #f)

(define max-d 0)

`(define (update n)`

(call-with-values

(lambda () (divisors n))

(lambda (d c s)

(set! divs d)

(set! cnts c)

(set! sums s)))

(set! max-d n))

Thus, `(vector-ref divs 360)`

is a list of the divisors of 360, `(vector-ref cnts 360)`

is their count, and `(vector-ref sums 360)`

is their sum:

`> (reverse (vector-ref divs 360))`

(1 2 3 4 5 6 8 9 10 12 15 18 20 24 30 36 40 45 60 72 90 120 180 360)

> (vector-ref cnts 360)

24

> (vector-ref sums 360)

1170

Note that the sum includes the number itself. Often, what is wanted is the sum excluding the number itself, which is computed as `(- (vector-ref sums n) n)`

.

Perfect numbers, which were studied by the ancient Greek mathematician Pythagoras circa 500 B.C., are those with the sum of the divisors, less the number itself, equal to the number itself; here we compute the perfect numbers less than *n*:

`(define (perfect n)`

(define (s n) (- (vector-ref sums n) n))

(when (< max-d n) (update n))

(let ((ps (list)))

(do ((p 1 (+ p 1)))

((< n p) (reverse ps))

(when (= p (s p))

(set! ps (cons p ps))))))

`> (perfect 10000)`

(6 28 496 8128)

Amicable pairs are pairs of numbers that are each the sum of the other’s divisors (excluding the numbers themselves). For instance, the sum of the divisors of 220 is 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284, and the sum of the divisors of 284 is 1 + 2 + 4 + 71 + 142 = 220, so the numbers 220 and 284 form an amicable pair. Here we compute the amicable pairs less than *n*, showing only the smaller of the pair:

`(define (amicable n)`

(define (s n) (- (vector-ref sums n) n))

(when (< max-d (* 5 n)) (update (* 5 n)))

(let loop ((a 1) (as (list)))

(cond ((< n a) (reverse as))

((and (= a (s (s a))) (< a (s a)))

(loop (+ a 1) (cons a as)))

(else (loop (+ a 1) as)))))

`> (display (amicable 10000))`

(220 1184 2620 5020 6232)

We multiply by 5 because sometimes the other half of an amicable pair is larger than the starting half, and 5 seems to give sufficient slop, as determined by experiment.

You can run the program at http://programmingpraxis.codepad.org/3y4c9Ybl. And if you wish, you can now easily solve Problem 21 at Project Euler.

Pages: 1 2

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 […]