No BSD License  

Highlights from
Amicable Number

3.0

3.0 | 3 ratings Rate this file 1 Download (last 30 days) File Size: 2.76 KB File ID: #21484

Amicable Number

by Abhisek Ukil

 

19 Sep 2008 (Updated 26 Sep 2008)

Function to compute amicable pairs between two intergers, upper & lower limits.

| Watch this File

File Information
Description

Amicable numbers are two different numbers so related that the sum of the proper divisors of the one is equal to the other, one being considered as a proper divisor but not the number itself. Such a pair is (220, 284); for the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71, and 142, of which the sum is 220.

Check: http://en.wikipedia.org/wiki/Amicable_number

This is a function to compute amicable pairs between two intergers, upper & lower limits. Put the 'amicable.m' file into your MATLAB path or working directory and call the following syntax.

Function syntax:
output=amicable(N2,N1)

where, N2 is the upper limit and N1 the lower limit, both positive integers.

If only one number is given, it is considered to be the upper limit and
the default lower limit is then 220 (as no amicable pair exists below 220).
If two numbers are given, the upper limit (1st number) must be greater than
the lower limit (2nd number).

The 'output' would be a matrix having two columns, for the amicable pairs.
The rows show the unique pairs. If no such pairs are found empty matrix is returned.

MATLAB release MATLAB 7.3 (R2006b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
19 Sep 2008 Jos x@y.z

hmmm, it returns non-amicable pairs as well ([3 4],[7 8], etc.)

Also, why provide such poor help within the m-file itself ("help amicable" does not return anything useful). You'd better copy the description given here into the m-file.

Finally, all factors F of a number N can be easily, and much faster(!), found using:
N = 220 ; F = 1:(N/2) ; t = N ./ F ; F = F(t==fix(t))

24 Sep 2008 Jos #1094450

The file and description do not match after the update. The present submission does not return amicable numbers upto a number, but returns the proper divisors of a number, which are needed to find amicable pairs. And it would be nice to give some credits when credits are due ...

24 Sep 2008 John D'Errico

Jos is correct of course. The author failed to include the rest of his code in the update. Even with that...

timeit(@() allfactor(29913660))
ans =
          3.58543410710021

timeit(@() aliquotparts(29913660))
ans =
       0.00491466028164719

Both functions find the same 71 distinct proper divisors of course, but I was surprised at the difference in times - John

25 Sep 2008 John D'Errico

Working now, but slow. It took over 80 seconds to (correctly) find the 8 smallest amicable pairs of numbers. This is REALLY slow.

tic,ap = amicable(20000);toc
Elapsed time is 80.180711 seconds.
ap =
         220 284
        1184 1210
        2620 2924
        5020 5564
        6232 6368
       10744 10856
       12285 14595
       17296 18416

The help is good. It is readable, explaining what an amicable pair is and provides a Wikipedia link to explain more for the interested student.

I found error checks on the only parameter. I found an H1 line. A couple of mlint flags that were not really of major significance. The fact that the output result is not preallocated in the code is not important here, as the code is so slow that nobody will ever see any serious time penalty because of the tiny amount of array growth.

Please disregard my initial rating (dated 9/22), which I have requested be removed by the administrator.

26 Sep 2008 Jos x@y.z

I second John review of 9/25/08). It now works, has help and comments, but is indeed too slow. Perhaps you could consider to include a lower bound so that one could look for amicable numbers between N1 and N2?

29 Sep 2008 Jos x@y.z

Although the algorithm is still too slow, this author should be credited for the improvements in programming skills. The error checks, help and comments make it both user and debug friendly. Well done!

Please login to add a comment or rating.
Updates
23 Sep 2008

Bugfix to remove non-amicables reported. Upgradation for faster factor calculation.

25 Sep 2008

Wrong file upload

26 Sep 2008

Major upgrade to include the upper and lower limits, so that one can search for amicable pairs within these bounds, imrpoving the speed.

Tag Activity for this File
Tag Applied By Date/Time
amicable number Abhisek Ukil 22 Oct 2008 10:20:08
number theory Abhisek Ukil 22 Oct 2008 10:20:08
mathematics Abhisek Ukil 22 Oct 2008 10:20:08
proper divisor Abhisek Ukil 22 Oct 2008 10:20:08

Contact us at files@mathworks.com