<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: When is a Number Perfect?</title>
	<atom:link href="http://blogs.mathworks.com/loren/2012/10/10/when-is-a-number-perfect/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2012/10/10/when-is-a-number-perfect/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Wed, 12 Jun 2013 12:35:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
	<item>
		<title>By: Martin Cohen</title>
		<link>http://blogs.mathworks.com/loren/2012/10/10/when-is-a-number-perfect/#comment-33204</link>
		<dc:creator>Martin Cohen</dc:creator>
		<pubDate>Thu, 11 Oct 2012 22:05:02 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=552#comment-33204</guid>
		<description><![CDATA[Of course any method that uses factorization to find perfect numbers is about as efficient as sorting an array by generating all permutations and stopping when one is found that is in order.]]></description>
		<content:encoded><![CDATA[<p>Of course any method that uses factorization to find perfect numbers is about as efficient as sorting an array by generating all permutations and stopping when one is found that is in order.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren Shure</title>
		<link>http://blogs.mathworks.com/loren/2012/10/10/when-is-a-number-perfect/#comment-33202</link>
		<dc:creator>Loren Shure</dc:creator>
		<pubDate>Thu, 11 Oct 2012 11:57:33 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=552#comment-33202</guid>
		<description><![CDATA[Thanks, John.

Very nice solution.  I wonder if the unique once vs. every loop isn&#039;t a gain because divs generally doesn&#039;t grow that large, even when there are duplicates. 

--Loren]]></description>
		<content:encoded><![CDATA[<p>Thanks, John.</p>
<p>Very nice solution.  I wonder if the unique once vs. every loop isn&#8217;t a gain because divs generally doesn&#8217;t grow that large, even when there are duplicates. </p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John D'Errico</title>
		<link>http://blogs.mathworks.com/loren/2012/10/10/when-is-a-number-perfect/#comment-33201</link>
		<dc:creator>John D'Errico</dc:creator>
		<pubDate>Wed, 10 Oct 2012 20:46:35 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=552#comment-33201</guid>
		<description><![CDATA[Hi Loren,

A nice trick for finding the list of all divisors of an integer that is more efficient than the symengine solution is to use kron.


n = 10581480;


n has a lot of divisors, including 17, a very interesting number for some of us, and 1729, a number that others consider even more interesting. Including n itself, there are 384 distinct integer divisors. It takes a bit to compute the list of all divisors.


tic,double(feval(symengine,&#039;numlib::divisors&#039;, n));toc
Elapsed time is 0.024904 seconds.


Here is a simple solution in MATLAB.


tic
divs = 1;
for f = factor(n)
  divs = unique(kron(divs,[1,f]));
end
toc
Elapsed time is 0.004309 seconds.


Interestingly, avoiding the multiple calls to unique is not hard, but it does not seem to save any time.


tic
f = factor(n);
k = [0,find(diff(f)),numel(f)];
divs = 1;
for i = 2:numel(k)
  divs = kron(divs,f(k(i)).^(0:(k(i) - k(i-1))));
end
toc
Elapsed time is 0.005534 seconds.


Either case is faster than the symbolic solution though.

John]]></description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>A nice trick for finding the list of all divisors of an integer that is more efficient than the symengine solution is to use kron.</p>
<p>n = 10581480;</p>
<p>n has a lot of divisors, including 17, a very interesting number for some of us, and 1729, a number that others consider even more interesting. Including n itself, there are 384 distinct integer divisors. It takes a bit to compute the list of all divisors.</p>
<p>tic,double(feval(symengine,&#8217;numlib::divisors&#8217;, n));toc<br />
Elapsed time is 0.024904 seconds.</p>
<p>Here is a simple solution in MATLAB.</p>
<p>tic<br />
divs = 1;<br />
for f = factor(n)<br />
  divs = unique(kron(divs,[1,f]));<br />
end<br />
toc<br />
Elapsed time is 0.004309 seconds.</p>
<p>Interestingly, avoiding the multiple calls to unique is not hard, but it does not seem to save any time.</p>
<p>tic<br />
f = factor(n);<br />
k = [0,find(diff(f)),numel(f)];<br />
divs = 1;<br />
for i = 2:numel(k)<br />
  divs = kron(divs,f(k(i)).^(0:(k(i) &#8211; k(i-1))));<br />
end<br />
toc<br />
Elapsed time is 0.005534 seconds.</p>
<p>Either case is faster than the symbolic solution though.</p>
<p>John</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brett Shoelson</title>
		<link>http://blogs.mathworks.com/loren/2012/10/10/when-is-a-number-perfect/#comment-33200</link>
		<dc:creator>Brett Shoelson</dc:creator>
		<pubDate>Wed, 10 Oct 2012 19:47:51 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=552#comment-33200</guid>
		<description><![CDATA[Thanks, Loren. Fun blog today! You missed a function on the File Exchange that&#039;s useful here, but that doesn&#039;t require anything other than core MATLAB. I posted FACTOR2 back in 2003 to provide easy computation of all factors of x, including x itself:

&lt;pre class=&quot;code&quot;&gt;
factor2(28)
ans =
     1     2     4     7    14    28


So:


trimFactors = @(x) x(1:end-1);
isperfect = @(x) sum(trimFactors(factor2(x)))==x;
isperfect(5)
isperfect(6)
isperfect(28)
ans =
     0
ans =
     1
ans =
     1
&lt;/pre&gt;

Cheers!
Brett]]></description>
		<content:encoded><![CDATA[<p>Thanks, Loren. Fun blog today! You missed a function on the File Exchange that&#8217;s useful here, but that doesn&#8217;t require anything other than core MATLAB. I posted FACTOR2 back in 2003 to provide easy computation of all factors of x, including x itself:</p>
<pre class="code">
factor2(28)
ans =
     1     2     4     7    14    28


So:


trimFactors = @(x) x(1:end-1);
isperfect = @(x) sum(trimFactors(factor2(x)))==x;
isperfect(5)
isperfect(6)
isperfect(28)
ans =
     0
ans =
     1
ans =
     1
</pre>
<p>Cheers!<br />
Brett</p>
]]></content:encoded>
	</item>
</channel>
</rss>
