Rank: 2217 based on 23 downloads (last 30 days) and 2 files submitted
photo

Yash

E-mail

Personal Profile:
Professional Interests:

 

Watch this Author's files

 

Files Posted by Yash View all
Updated   File Tags Downloads
(last 30 days)
Comments Rating
28 Jun 2009 divisor(n) Calculate the distinct divisors of a number not just the prime factors. Author: Yash divisor, factorization, factors, mathematics 22 2
26 Jun 2009 Subfactorial Calculate subfactorial of integers up to 170. Author: Yash subfactorial, mathematics, integer math, factorial, derangement, permutation 1 0
Comments and Ratings by Yash
Updated File Comments Rating
30 Jun 2009 divisor(n) Calculate the distinct divisors of a number not just the prime factors. Author: 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.

Comments and Ratings on Yash's Files View all
Updated File Comment by Comments Rating
30 Jun 2009 divisor(n) Calculate the distinct divisors of a number not just the prime factors. Author: Yash 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.

29 Jun 2009 divisor(n) Calculate the distinct divisors of a number not just the prime factors. Author: Yash Jos (10584)

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

Top Tags Applied by Yash
mathematics, derangement, divisor, factorial, factorization
Files Tagged by Yash View all
Updated   File Tags Downloads
(last 30 days)
Comments Rating
28 Jun 2009 divisor(n) Calculate the distinct divisors of a number not just the prime factors. Author: Yash divisor, factorization, factors, mathematics 22 2
26 Jun 2009 Subfactorial Calculate subfactorial of integers up to 170. Author: Yash subfactorial, mathematics, integer math, factorial, derangement, permutation 1 0

Contact us at files@mathworks.com