<?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: Primality Checking, Revisited</title>
	<atom:link href="http://programmingpraxis.com/2010/01/26/primality-checking-revisited/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2010/01/26/primality-checking-revisited/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +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/01/26/primality-checking-revisited/#comment-1151</link>
		<dc:creator><![CDATA[Mike]]></dc:creator>
		<pubDate>Mon, 12 Apr 2010 23:55:55 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1926#comment-1151</guid>
		<description><![CDATA[In python:

[sourcecode lang=&quot;python&quot;]
from math import sqrt

def bits_of( n ):
    &#039;&#039;&#039;generator that returns the bits in n, from MSB to LSB.&#039;&#039;&#039;
    
    bit = 1
    while bit &lt; n:
        bit &lt;&lt;= 8
    while bit &gt; n:
        bit &gt;&gt;= 1

    while bit:
        yield 1 if ( bit &amp; n ) else 0
        bit &gt;&gt;= 1

def euclid(a, b):
    &#039;&#039;&#039;Extended Euclid&#039;s algorithm.&#039;&#039;&#039;
    lastx, x = 1, 0
    lasty, y = 0, 1
    
    while b:
        q = a/b
        a, b = b, a%b
        lastx, x = x, lastx - q*x
        lasty, y = y, lasty - q*y

    return lastx, lasty, a

def gcd(m, n):
    &#039;&#039;&#039;Return greatest common divisor or m and n.&#039;&#039;&#039;
    
    if m &lt; n:
        m,n = n,m
        
    while n:
        m, n = n, m % n

    return m


def inverse(x, m):
    a, b, g = euclid(x, m)
    if g != 1:
        raise ValueError(&quot;x and m must be coprime.&quot;)

    return a % m

def is_square( xx ):
    if xx&amp;7==1 or xx&amp;31==4 or xx&amp;127==16 or xx&amp;191==0:
        x = sqrt( xx )
        
        return x == int( x )
            
    return False

def jacobi(a, n):
    if a == 0:
        return 0
    elif a == 1:
        return 1
    elif a == 2:
        return 1 if (n%8) in (1,7) else -1
    elif a&amp;1 == 0:
        return jacobi(2,n) * jacobi(a/2, n)
    elif n &lt; a:
        return jacobi(a%n, n)
    elif a%4 == 3 and n%4 == 3:
        return -jacobi(n, a)
    else:
        return jacobi(n, a)

legendre = jacobi       # well, for odd primes anyway

def lucas_test( n ):
    a, b = 11, 7
    while True:
        d = a * a - 4 * b
        if is_square(d):
            a += 2,
            b += 1

        elif gcd( n, 2 * a * b * d ) != 1:
            a += 2
            b += 2

        else:
            break

    x1 = ( a * a * inverse(b, n) - 2 ) % n
    m = ( n - legendre(d, n) ) / 2

    u = 2
    v = x1
    for bit in bits_of( m ):
        if bit:
            u = ( u * v - x1 ) %n
            v = ( v * v - 2 ) % n
        else:
            v = ( u * v - x1 ) %n
            u = ( u * u - 2 ) % n

    return ( x1 * u - 2 * v ) % n == 0

def miller_rabin_test(n, a):
    &#039;&#039;&#039;checks if n is prime base a.

    returns True is a is likely a composite.
    &#039;&#039;&#039;
    r, s = 0, n-1
    while s&amp;1 == 0:
        r += 1
        s &gt;&gt;= 1
        
    if pow(a, s, n) == 1:
        return True      # possibly prime

    for j in range(r):
        if pow(a, s, n) == n-1:
            return True  # possibly prime
        s *= 2
        
    return False  # definitely composite


small_primes = set([  2,  3,  5,  7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
                     43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ])

def is_prime(n):
	if n &lt; 100:
		return n in small_primes
	
	return all(n%p for p in small_primes) and \
	       miller_rabin_test( n, 2 ) and \
	       miller_rabin_test( n, 3 ) and \
	       lucas_test( n )

[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>In python:</p>
<pre class="brush: python;">
from math import sqrt

def bits_of( n ):
    '''generator that returns the bits in n, from MSB to LSB.'''
    
    bit = 1
    while bit &lt; n:
        bit &lt;&lt;= 8
    while bit &gt; n:
        bit &gt;&gt;= 1

    while bit:
        yield 1 if ( bit &amp; n ) else 0
        bit &gt;&gt;= 1

def euclid(a, b):
    '''Extended Euclid's algorithm.'''
    lastx, x = 1, 0
    lasty, y = 0, 1
    
    while b:
        q = a/b
        a, b = b, a%b
        lastx, x = x, lastx - q*x
        lasty, y = y, lasty - q*y

    return lastx, lasty, a

def gcd(m, n):
    '''Return greatest common divisor or m and n.'''
    
    if m &lt; n:
        m,n = n,m
        
    while n:
        m, n = n, m % n

    return m


def inverse(x, m):
    a, b, g = euclid(x, m)
    if g != 1:
        raise ValueError(&quot;x and m must be coprime.&quot;)

    return a % m

def is_square( xx ):
    if xx&amp;7==1 or xx&amp;31==4 or xx&amp;127==16 or xx&amp;191==0:
        x = sqrt( xx )
        
        return x == int( x )
            
    return False

def jacobi(a, n):
    if a == 0:
        return 0
    elif a == 1:
        return 1
    elif a == 2:
        return 1 if (n%8) in (1,7) else -1
    elif a&amp;1 == 0:
        return jacobi(2,n) * jacobi(a/2, n)
    elif n &lt; a:
        return jacobi(a%n, n)
    elif a%4 == 3 and n%4 == 3:
        return -jacobi(n, a)
    else:
        return jacobi(n, a)

legendre = jacobi       # well, for odd primes anyway

def lucas_test( n ):
    a, b = 11, 7
    while True:
        d = a * a - 4 * b
        if is_square(d):
            a += 2,
            b += 1

        elif gcd( n, 2 * a * b * d ) != 1:
            a += 2
            b += 2

        else:
            break

    x1 = ( a * a * inverse(b, n) - 2 ) % n
    m = ( n - legendre(d, n) ) / 2

    u = 2
    v = x1
    for bit in bits_of( m ):
        if bit:
            u = ( u * v - x1 ) %n
            v = ( v * v - 2 ) % n
        else:
            v = ( u * v - x1 ) %n
            u = ( u * u - 2 ) % n

    return ( x1 * u - 2 * v ) % n == 0

def miller_rabin_test(n, a):
    '''checks if n is prime base a.

    returns True is a is likely a composite.
    '''
    r, s = 0, n-1
    while s&amp;1 == 0:
        r += 1
        s &gt;&gt;= 1
        
    if pow(a, s, n) == 1:
        return True      # possibly prime

    for j in range(r):
        if pow(a, s, n) == n-1:
            return True  # possibly prime
        s *= 2
        
    return False  # definitely composite


small_primes = set([  2,  3,  5,  7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
                     43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ])

def is_prime(n):
	if n &lt; 100:
		return n in small_primes
	
	return all(n%p for p in small_primes) and \
	       miller_rabin_test( n, 2 ) and \
	       miller_rabin_test( n, 3 ) and \
	       lucas_test( n )

</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

