<?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: Prime Numbers In Python</title>
	<atom:link href="http://pthree.org/2007/09/05/prime-numbers-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://pthree.org/2007/09/05/prime-numbers-in-python/</link>
	<description>Linux.  GNU.  Freedom.</description>
	<lastBuildDate>Wed, 08 Feb 2012 02:59:26 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha</generator>
	<item>
		<title>By: nolfonzo</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-115692</link>
		<dc:creator>nolfonzo</dc:creator>
		<pubDate>Sun, 10 Apr 2011 23:26:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-115692</guid>
		<description>Tom I think you may need sqrt(n)+1</description>
		<content:encoded><![CDATA[<p>Tom I think you may need sqrt(n)+1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sanjo</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-115585</link>
		<dc:creator>sanjo</dc:creator>
		<pubDate>Fri, 18 Mar 2011 02:25:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-115585</guid>
		<description>Nice one, thank you. :)</description>
		<content:encoded><![CDATA[<p>Nice one, thank you. <img src='http://pthree.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Jones</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-111555</link>
		<dc:creator>Tom Jones</dc:creator>
		<pubDate>Sun, 19 Dec 2010 00:38:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-111555</guid>
		<description>A for loop is even cleaner

from math import sqrt

def is_prime(n):
    for i in range(2, sqrt(n)):
        if n % i == 0:
            return False
    return True</description>
		<content:encoded><![CDATA[<p>A for loop is even cleaner</p>
<p>from math import sqrt</p>
<p>def is_prime(n):<br />
    for i in range(2, sqrt(n)):<br />
        if n % i == 0:<br />
            return False<br />
    return True</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bismoy</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-111147</link>
		<dc:creator>Bismoy</dc:creator>
		<pubDate>Sun, 05 Sep 2010 18:21:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-111147</guid>
		<description>gr8 stuff...........</description>
		<content:encoded><![CDATA[<p>gr8 stuff&#8230;&#8230;&#8230;..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nolfonzo</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-111146</link>
		<dc:creator>nolfonzo</dc:creator>
		<pubDate>Fri, 03 Sep 2010 18:51:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-111146</guid>
		<description>Using Numpy and a sieve a approach you can generate these very fast.  I wrote about this on my blog recently:  http://rebrained.com/?p=458


import math
import numpy
def prime6(upto):
    primes=numpy.arange(3,upto+1,2)
    isprime=numpy.ones((upto-1)/2,dtype=bool)
    for factor in primes[:int(math.sqrt(upto))]:
        if isprime[(factor-2)/2]: isprime[(factor*3-2)/2:(upto-1)/2:factor]=0
    return numpy.insert(primes[isprime],0,2)
</description>
		<content:encoded><![CDATA[<p>Using Numpy and a sieve a approach you can generate these very fast.  I wrote about this on my blog recently:  <a href="http://rebrained.com/?p=458" rel="nofollow">http://rebrained.com/?p=458</a></p>
<p>import math<br />
import numpy<br />
def prime6(upto):<br />
    primes=numpy.arange(3,upto+1,2)<br />
    isprime=numpy.ones((upto-1)/2,dtype=bool)<br />
    for factor in primes[:int(math.sqrt(upto))]:<br />
        if isprime[(factor-2)/2]: isprime[(factor*3-2)/2:(upto-1)/2:factor]=0<br />
    return numpy.insert(primes[isprime],0,2)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: people</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-110977</link>
		<dc:creator>people</dc:creator>
		<pubDate>Mon, 14 Jun 2010 16:28:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-110977</guid>
		<description>n=input(&quot;Enter any  number-&quot;)
if n==2:
	print &quot;prime&quot;
if n==1:
	print &quot;neither prime nor composite&quot;
if n&gt;2:
	for i in range(2,n):
		if n%i==0:
			print &quot;composite&quot;
			break
			i=i+1	
	else:
		print &quot;prime&quot;
else:
	print &quot;enter correct input&quot;</description>
		<content:encoded><![CDATA[<p>n=input(&#8220;Enter any  number-&#8221;)<br />
if n==2:<br />
	print &#8220;prime&#8221;<br />
if n==1:<br />
	print &#8220;neither prime nor composite&#8221;<br />
if n&gt;2:<br />
	for i in range(2,n):<br />
		if n%i==0:<br />
			print &#8220;composite&#8221;<br />
			break<br />
			i=i+1<br />
	else:<br />
		print &#8220;prime&#8221;<br />
else:<br />
	print &#8220;enter correct input&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kotireddy</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-110797</link>
		<dc:creator>kotireddy</dc:creator>
		<pubDate>Fri, 09 Apr 2010 06:28:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-110797</guid>
		<description>def isPrimenumber(number):
 c=0
 for i in range(number):
  if number%(i+1)==0:
   c=c+1
 if c&gt;2:
  print number,&quot;is not prime number&quot;
 else:
  print number,&quot;is prime number&quot;
num=raw_input(&quot;enter a number:&quot;)
num=int(num)
isPrimenumber(num)</description>
		<content:encoded><![CDATA[<p>def isPrimenumber(number):<br />
 c=0<br />
 for i in range(number):<br />
  if number%(i+1)==0:<br />
   c=c+1<br />
 if c&gt;2:<br />
  print number,&#8221;is not prime number&#8221;<br />
 else:<br />
  print number,&#8221;is prime number&#8221;<br />
num=raw_input(&#8220;enter a number:&#8221;)<br />
num=int(num)<br />
isPrimenumber(num)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Veerabhadra Rao</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-110787</link>
		<dc:creator>Veerabhadra Rao</dc:creator>
		<pubDate>Thu, 01 Apr 2010 14:46:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-110787</guid>
		<description>iam a iiit student in nuzvid from visakhapatnam</description>
		<content:encoded><![CDATA[<p>iam a iiit student in nuzvid from visakhapatnam</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Veerabhadra Rao</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-110786</link>
		<dc:creator>Veerabhadra Rao</dc:creator>
		<pubDate>Thu, 01 Apr 2010 14:29:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-110786</guid>
		<description>how are you</description>
		<content:encoded><![CDATA[<p>how are you</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lizz</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-103465</link>
		<dc:creator>lizz</dc:creator>
		<pubDate>Mon, 23 Jun 2008 12:29:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-103465</guid>
		<description>your story is sux, try to test, for example, 99999999977 or 4740914805200312594461
try to read about miller-tabin algorythm

P.S. sorry for my english ;)</description>
		<content:encoded><![CDATA[<p>your story is sux, try to test, for example, 99999999977 or 4740914805200312594461<br />
try to read about miller-tabin algorythm</p>
<p>P.S. sorry for my english <img src='http://pthree.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johnathan Nguyen</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-86947</link>
		<dc:creator>Johnathan Nguyen</dc:creator>
		<pubDate>Fri, 28 Dec 2007 00:10:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-86947</guid>
		<description>Review this publication: Prime Sequence Homology by Daniel Blake et al; Fourrier University, Grenoble

Dr. Blake&#039;s approach is via a randomization channel.  Code is also presented. I tried to cut and paste but was unsuccessful.</description>
		<content:encoded><![CDATA[<p>Review this publication: Prime Sequence Homology by Daniel Blake et al; Fourrier University, Grenoble</p>
<p>Dr. Blake&#8217;s approach is via a randomization channel.  Code is also presented. I tried to cut and paste but was unsuccessful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Francesc Dorca</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-68151</link>
		<dc:creator>Francesc Dorca</dc:creator>
		<pubDate>Thu, 06 Sep 2007 21:39:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-68151</guid>
		<description>Hi Aaron,

First of all congratulations for your interesting blog.

Let me suggest an improvement to make your code much more efficient.

I do not know Python, but using my knowledge on other programming languages I think that I can understand your code pretty well. 

You can use only integer arithmetic doing two minor changes.

Eliminate the sentence

&lt;code&gt;import math&lt;/code&gt;

and replace the line

&lt;code&gt;while i &lt;= math.sqrt(n):&lt;/code&gt;

by the equivalent sentence

&lt;code&gt;while i*i &lt;= n:&lt;/code&gt;

that uses only integer arithmetics.</description>
		<content:encoded><![CDATA[<p>Hi Aaron,</p>
<p>First of all congratulations for your interesting blog.</p>
<p>Let me suggest an improvement to make your code much more efficient.</p>
<p>I do not know Python, but using my knowledge on other programming languages I think that I can understand your code pretty well. </p>
<p>You can use only integer arithmetic doing two minor changes.</p>
<p>Eliminate the sentence</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">import math</div></td></tr></tbody></table></div>
<p>and replace the line</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">while i &amp;lt;= math.sqrt(n):</div></td></tr></tbody></table></div>
<p>by the equivalent sentence</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">while i*i &amp;lt;= n:</div></td></tr></tbody></table></div>
<p>that uses only integer arithmetics.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-68143</link>
		<dc:creator>Aaron</dc:creator>
		<pubDate>Thu, 06 Sep 2007 19:41:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-68143</guid>
		<description>@Levi-  Fair enough.  My inexperience really shows through on things like this.  I need to spend more time on math-related algorithms analyzing their complexity and what&#039;s going on under the hood I think.

Again, thanks for the info.</description>
		<content:encoded><![CDATA[<p>@Levi-  Fair enough.  My inexperience really shows through on things like this.  I need to spend more time on math-related algorithms analyzing their complexity and what&#8217;s going on under the hood I think.</p>
<p>Again, thanks for the info.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Levi</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-68139</link>
		<dc:creator>Levi</dc:creator>
		<pubDate>Thu, 06 Sep 2007 18:59:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-68139</guid>
		<description>A couple of comments regarding the complexity of this algorithm.

First, you don&#039;t include lower-order factors in big-O notation.  An algorithm that takes n-1 steps is still O(n).  An algorithm that takes n^2+5n+2 steps is O(n^2), or simply Polynomial Time when speaking in terms of complexity classes.

Second, you&#039;re ignoring the fact that, for large numbers, modulo is not a constant time operation.  The O(sqrt(n)) complexity is only correct if n is the size of your number and it fits in the hardware.  This isn&#039;t true in general, so it&#039;s not very useful for the sorts of  reasoning big-O notation was invented for.

To be general, you need to define the algorithm in terms of the number of bits in your input number.  For an integer N, it takes log N + 1 bits to store it. That means that the complexity in terms of the input size is O(2^(n/2)).  This is exponential, i.e. very bad.  A 1024 bit integer gives you a running time of essentially 2^512, which is definitely intractable.

As other people have stated, there are much better algorithms for primality testing, and the one you&#039;re using is worthless for crypto applications.

My reference for this comment: &lt;a href=&quot;http://books.google.com/books?id=3Q0V-t5kJ5sC&amp;pg=RA1-PA14&amp;lpg=RA1-PA14&amp;dq=computational+complexity+%22naive+primality%22&amp;source=web&amp;ots=PPzLiuYi2K&amp;sig=X6ikfwZRoaNWfo9KCs7nH8K-36s#PRA1-PA14,M1&quot; rel=&quot;nofollow&quot;&gt;Complexity and Cryptography: An Introduction&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>A couple of comments regarding the complexity of this algorithm.</p>
<p>First, you don&#8217;t include lower-order factors in big-O notation.  An algorithm that takes n-1 steps is still O(n).  An algorithm that takes n^2+5n+2 steps is O(n^2), or simply Polynomial Time when speaking in terms of complexity classes.</p>
<p>Second, you&#8217;re ignoring the fact that, for large numbers, modulo is not a constant time operation.  The O(sqrt(n)) complexity is only correct if n is the size of your number and it fits in the hardware.  This isn&#8217;t true in general, so it&#8217;s not very useful for the sorts of  reasoning big-O notation was invented for.</p>
<p>To be general, you need to define the algorithm in terms of the number of bits in your input number.  For an integer N, it takes log N + 1 bits to store it. That means that the complexity in terms of the input size is O(2^(n/2)).  This is exponential, i.e. very bad.  A 1024 bit integer gives you a running time of essentially 2^512, which is definitely intractable.</p>
<p>As other people have stated, there are much better algorithms for primality testing, and the one you&#8217;re using is worthless for crypto applications.</p>
<p>My reference for this comment: <a href="http://books.google.com/books?id=3Q0V-t5kJ5sC&amp;pg=RA1-PA14&amp;lpg=RA1-PA14&amp;dq=computational+complexity+%22naive+primality%22&amp;source=web&amp;ots=PPzLiuYi2K&amp;sig=X6ikfwZRoaNWfo9KCs7nH8K-36s#PRA1-PA14,M1" rel="nofollow">Complexity and Cryptography: An Introduction</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cornelius DuLac</title>
		<link>http://pthree.org/2007/09/05/prime-numbers-in-python/#comment-68107</link>
		<dc:creator>Cornelius DuLac</dc:creator>
		<pubDate>Thu, 06 Sep 2007 13:03:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/#comment-68107</guid>
		<description>I don&#039;t think you are using the O notation correctly.  Using binary search to search through a list of n ordered numbers takes O(sqrt(n)).  Here, n is not the number of inputs, it&#039;s the input itself.  

Now, if you were saying n was the size of the input number, I think you&#039;ll find that your notation is still flawed.  This is not a poly time algorithm.  If it were, you would be able to break several encryption algorithms that rely on the hardness of the primality problem, as well (I believe) P=NP.

It&#039;s been a while since I&#039;ve done algorithmics in school, but I think you&#039;ll find I am right.

C.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t think you are using the O notation correctly.  Using binary search to search through a list of n ordered numbers takes O(sqrt(n)).  Here, n is not the number of inputs, it&#8217;s the input itself.  </p>
<p>Now, if you were saying n was the size of the input number, I think you&#8217;ll find that your notation is still flawed.  This is not a poly time algorithm.  If it were, you would be able to break several encryption algorithms that rely on the hardness of the primality problem, as well (I believe) P=NP.</p>
<p>It&#8217;s been a while since I&#8217;ve done algorithmics in school, but I think you&#8217;ll find I am right.</p>
<p>C.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

