Code covered by the BSD License  

Highlights from
divisor(n)

Be the first to rate this file! 22 Downloads (last 30 days) File Size: 1.69 KB File ID: #24500

divisor(n)

by Yash

 

21 Jun 2009 (Updated 28 Jun 2009)

Calculate the distinct divisors of a number not just the prime factors.

| Watch this File

File Information
Description

divisor(n) : row vector of all distinct divisors of a positive integer N, including 1 and N.

Remark:
   This function uses the default factor() routine in Matlab and hence is limited to input values up to 2^32. However if factor() routine does get updated for larger integers, this function will still work fine. Alternately you may comment out the statement that compares the values to 2^32, the factor() routine will work just fine (except maybe it will be bit slower for big numbers).
   Using factor() provides a significant speed improvement over manually searching for the each divisor of n.

Example:
   a = divisor(12);
   returns -> a = [1, 2, 3, 4, 6, 12];

See Also:
  factor, primes

MATLAB release MATLAB 5.2 (R10)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
29 Jun 2009 Jos (10584)

For another implementation, see
http://www.mathworks.com/matlabcentral/fileexchange/3944

30 Jun 2009 Yash

@ Jos : Thank you for pointing that out. Here's a small code that I wrote to compare the two techniques,

N = 100000;
tic
for i = 1:N
    x = factor2(i);
end
toc

tic
for i = 1:N
    x = divisor(i);
end
toc

On my machine i get the following results,
N = 1000
Elapsed time is 0.022092 seconds.
Elapsed time is 0.203660 seconds.
N = 10000
Elapsed time is 0.869049 seconds.
Elapsed time is 0.809918 seconds.
N = 100000
Elapsed time is 63.062130 seconds.
Elapsed time is 8.858714 seconds.

As you see for large N there is significant increase in speed if we use divisor() instead of factor2()

My take at why factor2() is slow for large N is because it creates an array a = [1:floor(k/2)]; and then removes unnecessary value of a, which is very inefficient way of doing it.

At the least the speed and memory requirement can be improved by modifying factor2() by only searching for factors till sqrt(k), i.e.
a = 1:sqrt(k); % no need to floor (sqrt(k))
and then at the end of code add a = [a, k./a(end:-1:1)]
We will need to check if k is perfect square though.
Here's what I am talking about,
sqk = sqrt(k);
a = 1:sqk;
tmp = k./a;
tmp = isinteger(tmp);
a = a(tmp);
if isinteger(sqk)
    a = [a, k./a(end-1:-1:1)];
else
    a = [a, k./a(end:-1:1)];
end

This implementation will be much better than the previous factor2(). In fact it will be much better than divisor() for small N.

Please login to add a comment or rating.
Updates
28 Jun 2009

Removed BSD license. If interested use the WTFPL license.

Tag Activity for this File
Tag Applied By Date/Time
divisor Yash 22 Jun 2009 11:16:36
factors Yash 22 Jun 2009 11:16:36
mathematics Yash 22 Jun 2009 11:16:36
factorization Yash 22 Jun 2009 11:16:36
divisor Alberto 31 Oct 2011 04:56:37

Contact us at files@mathworks.com