<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Proving Primality</title>
	<atom:link href="http://programmingpraxis.com/2010/02/02/proving-primality/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2010/02/02/proving-primality/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:48:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Mike</title>
		<link>http://programmingpraxis.com/2010/02/02/proving-primality/#comment-1160</link>
		<dc:creator><![CDATA[Mike]]></dc:creator>
		<pubDate>Fri, 16 Apr 2010 05:44:33 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1946#comment-1160</guid>
		<description><![CDATA[Here&#039;s a corrected version of the python code for proving a number is prime using the Lucas algorithm. 

[sourcecode lang=&quot;python&quot;]

from itertools import chain, cycle

def factors_of(n):
    f = 2
    for step in chain([0,1,2,2], cycle([4,2,4,2,4,6,2,6])):
        f += step

        if f*f &gt; n:
            if n != 1:
                yield n
            break

        if n%f == 0:
            yield f
            while n%f == 0:
                n /= f

def is_prime(n):
    &#039;&#039;&#039;proves primality of n using Lucas test.&#039;&#039;&#039;
    x = n-1

    if n%2 == 0:
        return n == 2

    factors = list( factors_of(x))

    b = 2
    while b &lt; n:
        if pow( b, x, n ) == 1:

            for q in factors:
                if pow( b, x/q, n ) == 1:
                    break
            else:
                return True

        b += 1

    return False
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Here&#8217;s a corrected version of the python code for proving a number is prime using the Lucas algorithm. </p>
<pre class="brush: python;">

from itertools import chain, cycle

def factors_of(n):
    f = 2
    for step in chain([0,1,2,2], cycle([4,2,4,2,4,6,2,6])):
        f += step

        if f*f &gt; n:
            if n != 1:
                yield n
            break

        if n%f == 0:
            yield f
            while n%f == 0:
                n /= f

def is_prime(n):
    '''proves primality of n using Lucas test.'''
    x = n-1

    if n%2 == 0:
        return n == 2

    factors = list( factors_of(x))

    b = 2
    while b &lt; n:
        if pow( b, x, n ) == 1:

            for q in factors:
                if pow( b, x/q, n ) == 1:
                    break
            else:
                return True

        b += 1

    return False
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://programmingpraxis.com/2010/02/02/proving-primality/#comment-1158</link>
		<dc:creator><![CDATA[Mike]]></dc:creator>
		<pubDate>Thu, 15 Apr 2010 15:47:06 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1946#comment-1158</guid>
		<description><![CDATA[D&#039;oh...guess that&#039;ll teach me not to edit a routine and not test it again before posting.
Is there a way to edit my earlier post fix lines 22 and 23?  Or should I just repost a correct version?

My confusion was over the word &quot;any&quot; in 

&quot;... if b^((n-1)/q) != 1 (mod n) for *any* prime divisor q of n-1, n is prime.&quot;

At first, I read the phrase to mean if I found any one q for which the non-congruence was true, then n is prime.  However, the correct reading is that the non-congruence holds no matter which q you try.  Therefore, you have to try every q to prove n is prime.  Perhaps using &quot;every&quot; instead of &quot;any&quot; would be clearer.]]></description>
		<content:encoded><![CDATA[<p>D&#8217;oh&#8230;guess that&#8217;ll teach me not to edit a routine and not test it again before posting.<br />
Is there a way to edit my earlier post fix lines 22 and 23?  Or should I just repost a correct version?</p>
<p>My confusion was over the word &#8220;any&#8221; in </p>
<p>&#8220;&#8230; if b^((n-1)/q) != 1 (mod n) for *any* prime divisor q of n-1, n is prime.&#8221;</p>
<p>At first, I read the phrase to mean if I found any one q for which the non-congruence was true, then n is prime.  However, the correct reading is that the non-congruence holds no matter which q you try.  Therefore, you have to try every q to prove n is prime.  Perhaps using &#8220;every&#8221; instead of &#8220;any&#8221; would be clearer.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: programmingpraxis</title>
		<link>http://programmingpraxis.com/2010/02/02/proving-primality/#comment-1156</link>
		<dc:creator><![CDATA[programmingpraxis]]></dc:creator>
		<pubDate>Wed, 14 Apr 2010 14:46:44 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1946#comment-1156</guid>
		<description><![CDATA[It wasn&#039;t confusing, it was wrong.  Thanks for pointing out the bug.  I&#039;ve updated the solution and added a test function.

By the way, your version of is_prime incorrectly reports that 2 is composite.  You should change the test at lines 22 and 23 to read:

&lt;code&gt;if n%2 == 0:
&#160;&#160;&#160;&#160;return n == 2&lt;/code&gt;

Don&#039;t feel bad; Jon Bentley got that wrong in one of his books, so you&#039;re in good company.]]></description>
		<content:encoded><![CDATA[<p>It wasn&#8217;t confusing, it was wrong.  Thanks for pointing out the bug.  I&#8217;ve updated the solution and added a test function.</p>
<p>By the way, your version of is_prime incorrectly reports that 2 is composite.  You should change the test at lines 22 and 23 to read:</p>
<p><code>if n%2 == 0:<br />
&nbsp;&nbsp;&nbsp;&nbsp;return n == 2</code></p>
<p>Don&#8217;t feel bad; Jon Bentley got that wrong in one of his books, so you&#8217;re in good company.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://programmingpraxis.com/2010/02/02/proving-primality/#comment-1155</link>
		<dc:creator><![CDATA[Mike]]></dc:creator>
		<pubDate>Wed, 14 Apr 2010 06:14:46 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1946#comment-1155</guid>
		<description><![CDATA[I found the description of the algorithm confusing.  When I implemented it, the routine would report some multiples of 5 (e.g., 15 and 35) to be prime.

Studying the proof at the mathpages.com link, given above, I think the algorithm should be to find a b so that b^(x-1) == 1 (mod n) then, if b^((n-1)/q) != 1 (mod n) for *all* prime divisors q of n-1, n is prime.  If b^((n-1)/q) == 1 (mod n) for some q, then try a new value for b.  This matches the pseudocode at wikipedia.

My python code is below:
[sourcecode lang=&quot;python&quot;]
from itertools import chain, cycle

def factors_of(n):
    f = 2
    for step in chain([0,1,2,2], cycle([4,2,4,2,4,6,2,6])):
        f += step
        
        if f*f &gt; n:
            if n != 1:
                yield n
            break
        
        if n%f == 0:
            yield f
            while n%f == 0:
                n /= f

                
def is_prime(n):
    &#039;&#039;&#039;proves primality of n using Lucas test.&#039;&#039;&#039;
    x = n-1

    if n == 2 or n%2 == 0:
        return False

    factors = list( factors_of(x))
    
    b = 2
    while b &lt; n:
        if pow( b, x, n ) == 1:

            for q in factors:
                if pow( b, x/q, n ) == 1:
                    break
            else:
                return True
            
        b += 1
        
    return False
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>I found the description of the algorithm confusing.  When I implemented it, the routine would report some multiples of 5 (e.g., 15 and 35) to be prime.</p>
<p>Studying the proof at the mathpages.com link, given above, I think the algorithm should be to find a b so that b^(x-1) == 1 (mod n) then, if b^((n-1)/q) != 1 (mod n) for *all* prime divisors q of n-1, n is prime.  If b^((n-1)/q) == 1 (mod n) for some q, then try a new value for b.  This matches the pseudocode at wikipedia.</p>
<p>My python code is below:</p>
<pre class="brush: python;">
from itertools import chain, cycle

def factors_of(n):
    f = 2
    for step in chain([0,1,2,2], cycle([4,2,4,2,4,6,2,6])):
        f += step

        if f*f &gt; n:
            if n != 1:
                yield n
            break

        if n%f == 0:
            yield f
            while n%f == 0:
                n /= f

def is_prime(n):
    '''proves primality of n using Lucas test.'''
    x = n-1

    if n == 2 or n%2 == 0:
        return False

    factors = list( factors_of(x))

    b = 2
    while b &lt; n:
        if pow( b, x, n ) == 1:

            for q in factors:
                if pow( b, x/q, n ) == 1:
                    break
            else:
                return True

        b += 1

    return False
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2010/02/02/proving-primality/#comment-989</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Tue, 02 Feb 2010 10:18:53 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1946#comment-989</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2010/02/02/programming-praxis-proving-primality/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
import Data.Bits
import Data.List

tdFactors :: Integer -&gt; [Integer]
tdFactors n = f n [2..floor . sqrt $ fromIntegral n] where
    f _ []     = []
    f r (x:xs) &#124; mod r x == 0 = x : f (div r x) (x:xs)
               &#124; otherwise    = f r xs

expm :: Integer -&gt; Integer -&gt; Integer -&gt; Integer
expm b e m = foldl&#039; (\r (b&#039;, _) -&gt; mod (r * b&#039;) m) 1 .
             filter (flip testBit 0 . snd) .
             zip (iterate (flip mod m . (^ 2)) b) .
             takeWhile (&gt; 0) $ iterate (`shiftR` 1) e

isPrime :: Integer -&gt; Bool
isPrime n = f 2 $ tdFactors (n - 1) where
    f _ []     = False
    f b (q:qs) &#124; expm b (n - 1)         n /= 1 = f (b + 1) (q:qs)
               &#124; expm b (n - 1 `div` q) n /= 1 = True
               &#124; otherwise                     = f b qs
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2010/02/02/programming-praxis-proving-primality/" rel="nofollow">http://bonsaicode.wordpress.com/2010/02/02/programming-praxis-proving-primality/</a> for a version with comments):</p>
<pre class="brush: css;">
import Data.Bits
import Data.List

tdFactors :: Integer -&gt; [Integer]
tdFactors n = f n [2..floor . sqrt $ fromIntegral n] where
    f _ []     = []
    f r (x:xs) | mod r x == 0 = x : f (div r x) (x:xs)
               | otherwise    = f r xs

expm :: Integer -&gt; Integer -&gt; Integer -&gt; Integer
expm b e m = foldl' (\r (b', _) -&gt; mod (r * b') m) 1 .
             filter (flip testBit 0 . snd) .
             zip (iterate (flip mod m . (^ 2)) b) .
             takeWhile (&gt; 0) $ iterate (`shiftR` 1) e

isPrime :: Integer -&gt; Bool
isPrime n = f 2 $ tdFactors (n - 1) where
    f _ []     = False
    f b (q:qs) | expm b (n - 1)         n /= 1 = f (b + 1) (q:qs)
               | expm b (n - 1 `div` q) n /= 1 = True
               | otherwise                     = f b qs
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Proving Primality &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2010/02/02/proving-primality/#comment-988</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Proving Primality &#171; Bonsai Code]]></dc:creator>
		<pubDate>Tue, 02 Feb 2010 10:18:22 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1946#comment-988</guid>
		<description><![CDATA[[...] Praxis &#8211; Proving&#160;Primality By Remco Niemeijer  In today&#8217;s Programming Praxis exercise we have to implement an algorithm to prove the primality of a number. [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; Proving&nbsp;Primality By Remco Niemeijer  In today&#8217;s Programming Praxis exercise we have to implement an algorithm to prove the primality of a number. [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

