File Exchange

image thumbnail

The divisors of a number, perfect, amicable, and sociable numbers

version 1.1 (14.1 KB) by

Tools to work (and play) with the divisors of a number



View License

This submission was written purely for fun, as an introduction to some simple things you can do with integers in MATLAB. The interested student might find some of the tricks interesting, for example, how might you efficiently find all of the proper divisors of a number? Aliquotparts uses a kron trick to do this quite efficiently. The aliquotsum code is also neatly vectorized.

The aliquots of a number, or aliquot parts, are all of the proper divisors of that number. The aliquot sum is the sum of those divisors. From here we can generate perfect numbers, abundant numbers, deficient numbers, amicable numbers, sociable numbers, etc. Who knows, maybe even quasi-perfect numbers?

These tools are vectorized and fairly efficient. For example, the aliquot parts of a number are given by

ans =
1 2 3 4 6 9 12 18

(36 is a divisor of itself but not a proper divisor.)

The aliquot sum is

ans =

We can count the number of proper divisors using aliquotsum too.

ans =

Amicable pairs of numbers are generated via the call:

ans =
220 284
1184 1210
2620 2924
5020 5564
6232 6368

Perfect numbers less than 10000? Since they must be self-amicable numbers, just do this:

ans =

Sociable numbers are those which form a clique of length greater than 2 such that the sums of their divisors are the next element from the clique, and the last member of the clique wraps around to the start. The first such sociable cycle has length 5, and starts with 12496.

ans =
12496 14288 15472 14536 14264

These tools are quite efficient. A quick test shows that it took less than a second (on my slow, old cpu) to find all distinct amicable pairs of numbers with the lower element less than 20000.

See the demo for many other examples.

Comments and Ratings (3)

Kenneth Eaton

Kenneth Eaton (view profile)

Very interesting, even if it was written purely for fun.

John Galt

Well written, well documented, many examples, and VERY fast.



Speed enhancement for aliquotparts

Added more to the demo, plus additional functionality to amicablecycles.

Title change. Who knows what an alioquot sum is anyway?

Additional functionality - p'th powers of the divisors.
Also added more demos, as well as suggested student problems.

MATLAB Release
MATLAB 7.5 (R2007b)

Inspired: factorpairs

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video