<?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: Python: Factors of a number</title>
	<atom:link href="http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/</link>
	<description>Android, Linux, Python and stealthcopters</description>
	<lastBuildDate>Thu, 29 Jul 2010 09:28:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Rich</title>
		<link>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/comment-page-1/#comment-771</link>
		<dc:creator>Rich</dc:creator>
		<pubDate>Sun, 28 Mar 2010 12:13:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=195#comment-771</guid>
		<description>Actually, it can be a generator expression instead of a list comprehension, which is better for memory:

set(reduce(list.__add__, ([i, n/i] for i in range(1, sqrt(n) + 1) if n % i == 0)))</description>
		<content:encoded><![CDATA[<p>Actually, it can be a generator expression instead of a list comprehension, which is better for memory:</p>
<p>set(reduce(list.__add__, ([i, n/i] for i in range(1, sqrt(n) + 1) if n % i == 0)))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rich</title>
		<link>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/comment-page-1/#comment-770</link>
		<dc:creator>Rich</dc:creator>
		<pubDate>Sun, 28 Mar 2010 12:10:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=195#comment-770</guid>
		<description>set(reduce(list.__add__, [[i, n/i] for i in range(1, sqrt(n) + 1) if n % i == 0]))</description>
		<content:encoded><![CDATA[<p>set(reduce(list.__add__, [[i, n/i] for i in range(1, sqrt(n) + 1) if n % i == 0]))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mat</title>
		<link>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/comment-page-1/#comment-40</link>
		<dc:creator>mat</dc:creator>
		<pubDate>Thu, 12 Nov 2009 10:48:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=195#comment-40</guid>
		<description>@mike: Thanks for pointing out the error in my code I&#039;ve update the post now. Also the method with the prime factors is clearly much more efficient, thanks :)

@Ryan: I&#039;ve not come across generators before, thanks for your code looks a hell of a lot neater than what I wrote!</description>
		<content:encoded><![CDATA[<p>@mike: Thanks for pointing out the error in my code I&#8217;ve update the post now. Also the method with the prime factors is clearly much more efficient, thanks <img src='http://www.stealthcopter.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>@Ryan: I&#8217;ve not come across generators before, thanks for your code looks a hell of a lot neater than what I wrote!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Hansen</title>
		<link>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/comment-page-1/#comment-39</link>
		<dc:creator>Mike Hansen</dc:creator>
		<pubDate>Thu, 12 Nov 2009 05:18:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=195#comment-39</guid>
		<description>Here are some timings.  I&#039;ve named my function factors and your function slow_factors in the timings below.

Lots of small factors:

sage: %timeit factors(2**40)
10000 loops, best of 3: 44.2 µs per loop
sage: %timeit slow_factors(2**40)
10 loops, best of 3: 255 ms per loop

Product of two primes:

sage: p = next_prime(10^5)*next_prime(10^6); p
100003300009
sage: %timeit slow_factors(p)
10 loops, best of 3: 473 ms per loop
sage: %timeit factors(p)
10 loops, best of 3: 183 ms per loop

Primes:

sage: p = next_prime(10^10); p
10000000019
sage: %timeit factors(p)
10 loops, best of 3: 96.9 ms per loop
sage: %timeit slow_factors(p)
10 loops, best of 3: 144 ms per loop

When there are lots of factors, then the method I posted gets much faster.</description>
		<content:encoded><![CDATA[<p>Here are some timings.  I&#8217;ve named my function factors and your function slow_factors in the timings below.</p>
<p>Lots of small factors:</p>
<p>sage: %timeit factors(2**40)<br />
10000 loops, best of 3: 44.2 µs per loop<br />
sage: %timeit slow_factors(2**40)<br />
10 loops, best of 3: 255 ms per loop</p>
<p>Product of two primes:</p>
<p>sage: p = next_prime(10^5)*next_prime(10^6); p<br />
100003300009<br />
sage: %timeit slow_factors(p)<br />
10 loops, best of 3: 473 ms per loop<br />
sage: %timeit factors(p)<br />
10 loops, best of 3: 183 ms per loop</p>
<p>Primes:</p>
<p>sage: p = next_prime(10^10); p<br />
10000000019<br />
sage: %timeit factors(p)<br />
10 loops, best of 3: 96.9 ms per loop<br />
sage: %timeit slow_factors(p)<br />
10 loops, best of 3: 144 ms per loop</p>
<p>When there are lots of factors, then the method I posted gets much faster.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/comment-page-1/#comment-38</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Wed, 11 Nov 2009 08:21:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=195#comment-38</guid>
		<description>And a better version of above:

def genFactors2(n):
	&quot;&quot;&quot;Generate the factors of an integer.&quot;&quot;&quot;
	yield 1
	for i in xrange(2, math.floor(math.sqrt(n))):
		if n % i == 0:
			yield i
			yield n/i
	yield n

def factors2(n):
	&quot;&quot;&quot;Return the factors of a integer.&quot;&quot;&quot;
	return list(genFactors(n))</description>
		<content:encoded><![CDATA[<p>And a better version of above:</p>
<p>def genFactors2(n):<br />
	&#8220;&#8221;"Generate the factors of an integer.&#8221;"&#8221;<br />
	yield 1<br />
	for i in xrange(2, math.floor(math.sqrt(n))):<br />
		if n % i == 0:<br />
			yield i<br />
			yield n/i<br />
	yield n</p>
<p>def factors2(n):<br />
	&#8220;&#8221;"Return the factors of a integer.&#8221;"&#8221;<br />
	return list(genFactors(n))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/comment-page-1/#comment-37</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Wed, 11 Nov 2009 08:21:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=195#comment-37</guid>
		<description>Here&#039;s a variant that uses a generator expression.

def genFactors(n):
	&quot;&quot;&quot;Generate the factors of an integer.&quot;&quot;&quot;
	yield 1
	for i in xrange(2, n/2 + 1):
		if n % i == 0:
			yield i
	yield n

def factors(n):
	&quot;&quot;&quot;Return the factors of a integer.&quot;&quot;&quot;
	return list(genFactors(n))</description>
		<content:encoded><![CDATA[<p>Here&#8217;s a variant that uses a generator expression.</p>
<p>def genFactors(n):<br />
	&#8220;&#8221;"Generate the factors of an integer.&#8221;"&#8221;<br />
	yield 1<br />
	for i in xrange(2, n/2 + 1):<br />
		if n % i == 0:<br />
			yield i<br />
	yield n</p>
<p>def factors(n):<br />
	&#8220;&#8221;"Return the factors of a integer.&#8221;"&#8221;<br />
	return list(genFactors(n))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Hansen</title>
		<link>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/comment-page-1/#comment-36</link>
		<dc:creator>Mike Hansen</dc:creator>
		<pubDate>Wed, 11 Nov 2009 04:31:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=195#comment-36</guid>
		<description>At the end, you want to do 

fact.sort()
return fact

since the sort method just returns None and not the sorted list.

A more efficient method to find all the factors is to find all of the prime factors and then build up all of the factors from that.  You can do this by say noticing that if p is prime then p%30 must also be prime or one.  Also, p%30 can&#039;t be either a 2, 3, or 5 since 2*3*5 = 30.  Using this information, you can reduce the number of divisions you need to perform.

You can see the code for this at http://sage.math.washington.edu/home/mhansen/factor.py

Also, if you like doing Project Euler problems in Python, then you should try using Sage ( http://www.sagemath.org ) which has a lot of this functionality already built in (and much faster since it interfaces with C libraries).</description>
		<content:encoded><![CDATA[<p>At the end, you want to do </p>
<p>fact.sort()<br />
return fact</p>
<p>since the sort method just returns None and not the sorted list.</p>
<p>A more efficient method to find all the factors is to find all of the prime factors and then build up all of the factors from that.  You can do this by say noticing that if p is prime then p%30 must also be prime or one.  Also, p%30 can&#8217;t be either a 2, 3, or 5 since 2*3*5 = 30.  Using this information, you can reduce the number of divisions you need to perform.</p>
<p>You can see the code for this at <a href="http://sage.math.washington.edu/home/mhansen/factor.py" rel="nofollow">http://sage.math.washington.edu/home/mhansen/factor.py</a></p>
<p>Also, if you like doing Project Euler problems in Python, then you should try using Sage ( <a href="http://www.sagemath.org" rel="nofollow">http://www.sagemath.org</a> ) which has a lot of this functionality already built in (and much faster since it interfaces with C libraries).</p>
]]></content:encoded>
	</item>
</channel>
</rss>
