<?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: Checking if a number is prime</title>
	<atom:link href="http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/</link>
	<description>Android, Linux, Python and stealthcopters</description>
	<lastBuildDate>Wed, 08 Feb 2012 11:08:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: st0le</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-2132</link>
		<dc:creator>st0le</dc:creator>
		<pubDate>Wed, 26 May 2010 10:44:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-2132</guid>
		<description>if n &lt;= 2 or n % 2 == 0: 
return n == 2
else:
...
...</description>
		<content:encoded><![CDATA[<p>if n &lt;= 2 or n % 2 == 0:<br />
return n == 2<br />
else:<br />
&#8230;<br />
&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-42</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Fri, 13 Nov 2009 02:57:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-42</guid>
		<description>By and large, the extra &#039;else&#039;s don&#039;t make much of a difference in the bytecode, but they do help the readability.  And, frankly, if you&#039;re writing code in python, readability is key.  Optimizations in python are usually of the logical (algorithmic) sort, rather than code optimization.  Really, the inclusion or exclusion of &#039;else&#039;s within the logic is semantic.

I did totally forget about negative numbers, probably because no one cares about negative prime numbers and I wasn&#039;t worried about completeness (hence my ADD-fueled additional posts in what was the middle of the night for me).  If I were writing a library, I&#039;d have tests to prevent this sort of thing.

As for the question of whether you need to search higher than the sqrt of the number, every factor greater than the sqrt of the number will have a match that is less than the sqrt of the number.

Also, your revised program still fails for 2.

If I were to write a &#039;proper&#039; version, it would be:
def prime_test(n):
.. if n &lt; 2 or n % 2 == 0:
.... return False
.. elif n == 2:
.... return True
.. else:
.... for i in range(3, int(math.sqrt(n))+1, 2):
...... if n % i == 0:
........ return False
.... return True

If you want speed, however, find a library where someone else has worried about such things, such as sympy.

%timeit assert sympy.isprime(2**61-1)==True  # 9th Mersenne prime
1000 loops, best of 3: 743 µs per loop

%timeit assert sympy.isprime(2**89-1)==True  # 10th.
1000 loops, best of 3: 1.22 ms per loop

%timeit assert sympy.isprime(2**(2**6)+1)==False  # 6th Fermat number
1000 loops, best of 3: 324 µs per loop

And for reference to my previous post, the redefined prime_test vs. sympy:
%timeit -n1000 -r10 assert prime_test(982451653)==True  # Mine
1000 loops, best of 10: 2.51 ms per loop
%timeit -n1000 -r10 assert sympy.isprime(982451653)==True  # sympy
1000 loops, best of 10: 133 µs per loop</description>
		<content:encoded><![CDATA[<p>By and large, the extra &#8216;else&#8217;s don&#8217;t make much of a difference in the bytecode, but they do help the readability.  And, frankly, if you&#8217;re writing code in python, readability is key.  Optimizations in python are usually of the logical (algorithmic) sort, rather than code optimization.  Really, the inclusion or exclusion of &#8216;else&#8217;s within the logic is semantic.</p>
<p>I did totally forget about negative numbers, probably because no one cares about negative prime numbers and I wasn&#8217;t worried about completeness (hence my ADD-fueled additional posts in what was the middle of the night for me).  If I were writing a library, I&#8217;d have tests to prevent this sort of thing.</p>
<p>As for the question of whether you need to search higher than the sqrt of the number, every factor greater than the sqrt of the number will have a match that is less than the sqrt of the number.</p>
<p>Also, your revised program still fails for 2.</p>
<p>If I were to write a &#8216;proper&#8217; version, it would be:<br />
def prime_test(n):<br />
.. if n &lt; 2 or n % 2 == 0:<br />
&#8230;. return False<br />
.. elif n == 2:<br />
&#8230;. return True<br />
.. else:<br />
&#8230;. for i in range(3, int(math.sqrt(n))+1, 2):<br />
&#8230;&#8230; if n % i == 0:<br />
&#8230;&#8230;.. return False<br />
&#8230;. return True</p>
<p>If you want speed, however, find a library where someone else has worried about such things, such as sympy.</p>
<p>%timeit assert sympy.isprime(2**61-1)==True  # 9th Mersenne prime<br />
1000 loops, best of 3: 743 µs per loop</p>
<p>%timeit assert sympy.isprime(2**89-1)==True  # 10th.<br />
1000 loops, best of 3: 1.22 ms per loop</p>
<p>%timeit assert sympy.isprime(2**(2**6)+1)==False  # 6th Fermat number<br />
1000 loops, best of 3: 324 µs per loop</p>
<p>And for reference to my previous post, the redefined prime_test vs. sympy:<br />
%timeit -n1000 -r10 assert prime_test(982451653)==True  # Mine<br />
1000 loops, best of 10: 2.51 ms per loop<br />
%timeit -n1000 -r10 assert sympy.isprime(982451653)==True  # sympy<br />
1000 loops, best of 10: 133 µs per loop</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles McCreary</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-35</link>
		<dc:creator>Charles McCreary</dc:creator>
		<pubDate>Wed, 11 Nov 2009 03:20:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-35</guid>
		<description>I dug around a year or so ago and came up with what I think is a pretty fast algorithm applying the Miller-Rabin primality test and implemented in python. Testing the 9th Mersenne prime, 2305843009213693951 takes ~0:00:00.002065. Try the 6th Fermat number (18446744073709551617 = 18446744073709551617 = 274177 × 67280421310721). Fermat conjectured they were all prime but Euler factored #5 in 1792. Time to determine not prime: 0:00:10.347881.

http://www.tiawichiresearch.com/?p=44</description>
		<content:encoded><![CDATA[<p>I dug around a year or so ago and came up with what I think is a pretty fast algorithm applying the Miller-Rabin primality test and implemented in python. Testing the 9th Mersenne prime, 2305843009213693951 takes ~0:00:00.002065. Try the 6th Fermat number (18446744073709551617 = 18446744073709551617 = 274177 × 67280421310721). Fermat conjectured they were all prime but Euler factored #5 in 1792. Time to determine not prime: 0:00:10.347881.</p>
<p><a href="http://www.tiawichiresearch.com/?p=44" rel="nofollow">http://www.tiawichiresearch.com/?p=44</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mat</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-34</link>
		<dc:creator>mat</dc:creator>
		<pubDate>Wed, 07 Oct 2009 11:17:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-34</guid>
		<description>Don&#039;t worry about criticism as I&#039;d much prefer to be embarrassed and have learnt something, than proud and ignorant. I am more used to writing code using C style but I&#039;m trying to learn all the loveliness that python offers.

The main speed advantage in your program over mine is provided by the limit (sqrt(number)) which is something I was looking into but I wanted to find a proof for it before using it.

Your program also doesn&#039;t handle numbers below 1. I didn&#039;t realise range had an optional third parameter for steps, this will be very useful for me, thanks!

One thing I don&#039;t understand is why you&#039;d need to use else so much. I.E.
….if num is 2:
……..return True
….else:

as you will return if num is 2, there is no need to use an else statement and it just takes up room. And I&#039;d change this 

…………if num % n:
…………….continue
…………else:
…………….return False

to

…………if not num % n:
…………….return False

and I&#039;d change my original code to:

&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
def isprime(number):
	if number&lt;=1 or number%2==0:
		return 0
	for check in range(3,int(math.sqrt(number))+1,2):
		if number%check==0:
			return 0
	return 1
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Don&#8217;t worry about criticism as I&#8217;d much prefer to be embarrassed and have learnt something, than proud and ignorant. I am more used to writing code using C style but I&#8217;m trying to learn all the loveliness that python offers.</p>
<p>The main speed advantage in your program over mine is provided by the limit (sqrt(number)) which is something I was looking into but I wanted to find a proof for it before using it.</p>
<p>Your program also doesn&#8217;t handle numbers below 1. I didn&#8217;t realise range had an optional third parameter for steps, this will be very useful for me, thanks!</p>
<p>One thing I don&#8217;t understand is why you&#8217;d need to use else so much. I.E.<br />
….if num is 2:<br />
……..return True<br />
….else:</p>
<p>as you will return if num is 2, there is no need to use an else statement and it just takes up room. And I&#8217;d change this </p>
<p>…………if num % n:<br />
…………….continue<br />
…………else:<br />
…………….return False</p>
<p>to</p>
<p>…………if not num % n:<br />
…………….return False</p>
<p>and I&#8217;d change my original code to:</p>
<pre name="code" class="python">
def isprime(number):
	if number< =1 or number%2==0:
		return 0
	for check in range(3,int(math.sqrt(number))+1,2):
		if number%check==0:
			return 0
	return 1
</pre>
</pre>]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-33</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Wed, 07 Oct 2009 07:17:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-33</guid>
		<description>Now I&#039;m really making myself look bad: I forgot to test num is even or 1... So, an additional &#039;elif num is 1 or num % n is 0: return False&#039; is necessary.  Nonetheless, that only boosts the average time per loop by 0.1 ms to 2.79.</description>
		<content:encoded><![CDATA[<p>Now I&#8217;m really making myself look bad: I forgot to test num is even or 1&#8230; So, an additional &#8216;elif num is 1 or num % n is 0: return False&#8217; is necessary.  Nonetheless, that only boosts the average time per loop by 0.1 ms to 2.79.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-32</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Wed, 07 Oct 2009 07:02:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-32</guid>
		<description>d&#039;oh! Off-by-1 error.  Should be int(math.sqrt(num))+1</description>
		<content:encoded><![CDATA[<p>d&#8217;oh! Off-by-1 error.  Should be int(math.sqrt(num))+1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-31</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Wed, 07 Oct 2009 06:54:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-31</guid>
		<description>Okay, I&#039;ve looked through your blog a few times and what you&#039;re writing here is C using python syntax.

I&#039;m sorry to sound so critical, but, in this example in particular, your code is, frankly, terrible python.  And it&#039;s slow.

Try this:

import math

def prime_test(num):
....if num is 2:
........return True
....else:
........for n in range(3, int(math.sqrt(num)), 2):
............if num % n:
................continue
............else:
................return False
........return True

In my ipython shell:

%timeit -n1000 -r10 isprime(982451653) #improved version
1000 loops, best of 10: 6.25 ms per loop

%timeit -n1000 -r10 prime_test(982451653)
1000 loops, best of 10: 2.69 ms per loop

Python has many high level constructs.  They are what make the language so powerful.</description>
		<content:encoded><![CDATA[<p>Okay, I&#8217;ve looked through your blog a few times and what you&#8217;re writing here is C using python syntax.</p>
<p>I&#8217;m sorry to sound so critical, but, in this example in particular, your code is, frankly, terrible python.  And it&#8217;s slow.</p>
<p>Try this:</p>
<p>import math</p>
<p>def prime_test(num):<br />
&#8230;.if num is 2:<br />
&#8230;&#8230;..return True<br />
&#8230;.else:<br />
&#8230;&#8230;..for n in range(3, int(math.sqrt(num)), 2):<br />
&#8230;&#8230;&#8230;&#8230;if num % n:<br />
&#8230;&#8230;&#8230;&#8230;&#8230;.continue<br />
&#8230;&#8230;&#8230;&#8230;else:<br />
&#8230;&#8230;&#8230;&#8230;&#8230;.return False<br />
&#8230;&#8230;..return True</p>
<p>In my ipython shell:</p>
<p>%timeit -n1000 -r10 isprime(982451653) #improved version<br />
1000 loops, best of 10: 6.25 ms per loop</p>
<p>%timeit -n1000 -r10 prime_test(982451653)<br />
1000 loops, best of 10: 2.69 ms per loop</p>
<p>Python has many high level constructs.  They are what make the language so powerful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mat</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-28</link>
		<dc:creator>mat</dc:creator>
		<pubDate>Mon, 21 Sep 2009 09:01:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-28</guid>
		<description>Not in this case, as using the sieve to check a single number is very inefficient and wastes a lot of memory. If I wanted to get a list of all the primes up to a certain number then using the sieve method would be appropriate.</description>
		<content:encoded><![CDATA[<p>Not in this case, as using the sieve to check a single number is very inefficient and wastes a lot of memory. If I wanted to get a list of all the primes up to a certain number then using the sieve method would be appropriate.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: l0nwlf</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-27</link>
		<dc:creator>l0nwlf</dc:creator>
		<pubDate>Sun, 20 Sep 2009 21:11:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-27</guid>
		<description>Use sieve, fast and smooth !!</description>
		<content:encoded><![CDATA[<p>Use sieve, fast and smooth !!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mat</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-17</link>
		<dc:creator>mat</dc:creator>
		<pubDate>Fri, 04 Sep 2009 01:12:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-17</guid>
		<description>Thanks mike, have update the post with your suggestion and some benchmarks!</description>
		<content:encoded><![CDATA[<p>Thanks mike, have update the post with your suggestion and some benchmarks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Meat</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/comment-page-1/#comment-16</link>
		<dc:creator>Meat</dc:creator>
		<pubDate>Thu, 03 Sep 2009 18:40:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78#comment-16</guid>
		<description>This week I have been mostly reading blog.Stealthcopter.com and it&#039;s been really helpful. So thanks for that. Just a point i&#039;ve already let you know about; instead of check += 1 you can do check += 2 as once you&#039;ve eliminated the posibility of an even factor, the rest disapear!
until next time!
- Meat</description>
		<content:encoded><![CDATA[<p>This week I have been mostly reading blog.Stealthcopter.com and it&#8217;s been really helpful. So thanks for that. Just a point i&#8217;ve already let you know about; instead of check += 1 you can do check += 2 as once you&#8217;ve eliminated the posibility of an even factor, the rest disapear!<br />
until next time!<br />
- Meat</p>
]]></content:encoded>
	</item>
</channel>
</rss>

