Next: , Previous: , Up: Answers to Exercises   [Contents][Index]

2.7.22 List Tutorial Exercise 4

A number ‘j’ is a divisor of ‘n’ if ‘n % j = 0’. The first step is to get a vector that identifies the divisors.

2:  30                  2:  [0, 0, 0, 2, ...]    1:  [1, 1, 1, 0, ...]
1:  [1, 2, 3, 4, ...]   1:  0                        .
    .                       .

 30 RET v x 30 RET   s 1    V M %  0                 V M a =  s 2

This vector has 1’s marking divisors of 30 and 0’s marking non-divisors.

The zeroth divisor function is just the total number of divisors. The first divisor function is the sum of the divisors.

1:  8      3:  8                    2:  8                    2:  8
           2:  [1, 2, 3, 4, ...]    1:  [1, 2, 3, 0, ...]    1:  72
           1:  [1, 1, 1, 0, ...]        .                        .
               .

   V R +       r 1 r 2                  V M *                  V R +

Once again, the last two steps just compute a dot product for which a simple * would have worked equally well.