Divisors of a number, perfect, amicable, and sociable number

版本 1.2.0.0 (20.4 KB) 作者: John D'Errico
Tools to work (and play) with the divisors of a number
2.5K 次下载
更新时间 2019/1/31

查看许可证

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

aliquotparts(36)
ans =
1 2 3 4 6 9 12 18

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

The aliquot sum is

aliquotsum(36)
ans =
55

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

aliquotsum(720,0)
ans =
29

Amicable pairs of numbers are generated via the call:

amicablecycles(10000,2)
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:

amicablecycles(10000,1)
ans =
6
28
496
8128

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.

amicablecycles(2:2:40000,5)
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.

引用格式

John D'Errico (2024). Divisors of a number, perfect, amicable, and sociable number (https://www.mathworks.com/matlabcentral/fileexchange/21498-divisors-of-a-number-perfect-amicable-and-sociable-number), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2007b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Random Number Generation 的更多信息
致谢

启发作品: factorpairs

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.2.0.0

Unique must have changed the shape of the third output at some point. This version will be insensitive to release in that respect.

1.1.0.0

Speed enhancement for aliquotparts