<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>To Hell in a Handbasket &#187; Math</title>
	<atom:link href="http://ericsilva.org/category/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://ericsilva.org</link>
	<description>General Views, Opinions, and Observations in the World Around Me by Eric Silva</description>
	<lastBuildDate>Wed, 28 Jul 2010 18:36:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Primality Test v2.0</title>
		<link>http://ericsilva.org/2009/12/14/primality-test-v2-0/</link>
		<comments>http://ericsilva.org/2009/12/14/primality-test-v2-0/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 18:02:44 +0000</pubDate>
		<dc:creator>Eric Silva</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[LinkedIn]]></category>
		<category><![CDATA[prime numbers]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://ericsilva.org/?p=359</guid>
		<description><![CDATA[After feedback from some friends of mine, and doing a little bit of background research, I am writing this update to my original post last week.  As it turns out, by checking all the numbers in the form 6k ± 1  instead of checking each number up to the input value, I have increased the speed [...]]]></description>
			<content:encoded><![CDATA[<p>After feedback from some friends of mine, and doing a little bit of <a href="http://en.wikipedia.org/wiki/Primality_test" target="_blank">background research</a>, I am writing this update to my <a href="http://ericsilva.org/2009/12/10/determining-if-a-number-is-prime/" target="_self">original post last week</a>.  As it turns out, by checking all the numbers in the form 6<em>k</em> ± 1 <img style="vertical-align: middle; margin: 0px; border: initial none initial;" src="http://upload.wikimedia.org/math/2/8/e/28edd0f7feeaea1aac75a80e84bc048c.png" alt="\scriptstyle{}\leq\sqrt n" /> instead of checking each number up to the input value, I have increased the speed by 7 times! determineIsPrime3 (line 48 below) is the fastest algorithm so far.  determineIsPrime2, a simple comparision against the <img src="http://upload.wikimedia.org/math/f/8/d/f8d9ab3dcb0f9aee56999cd1fc1f7b21.png" alt="\scriptstyle\sqrt n" />, was twice as fast as the original algorithm.</p>
<p>For now, I am putting this one to bed.  It was a fun exercise, but I have got what I need from it.</p>
<pre class="brush: python; gutter: true; toolbar: true;">
'''
Checks the specified value to determine if it is a prime number.
If it is not prime the divisor will be returned instead.

@author: Eric Silva
'''

import math, time

#Change this value to whatever value you want to test for prime.
#testValue = 65027
#testValue = 155188329701
testValue = 99194853094755497
#testValue = 10888869450418352160768000001
print 'Testing %d...' % testValue

def determineIsPrime(testPrime):
    if testPrime % 2 == 0:
        return 'Divisible by 2'
    if testPrime % 3 == 0:
        return 'Divisible by 3'
    testNum = 7
    testLimit = testPrime
    while testLimit &gt;= testNum:
        if testPrime % testNum == 0:
           return 'Divisible by %d' % testNum
        testLimit = testPrime/testNum

        testNum = testNum + 2

    return '%d is prime!' % testPrime

def determineIsPrime2(testPrime):
    if testPrime % 2 == 0:
        return 'Divisible by 2'
    if testPrime % 3 == 0:
        return 'Divisible by 3'
    testNum = 5
    sqrt = math.sqrt(testPrime)
    while testNum &lt;= sqrt:
        if testPrime % testNum == 0:
           return 'Divisible by %d' % testNum

        testNum = testNum + 2

    return '%d is prime!' % testPrime

def determineIsPrime3(testPrime):
    if testPrime % 2 == 0:
        return 'Divisible by 2'
    if testPrime % 3 == 0:
        return 'Divisible by 3'
    testNum = 7
    sqrt = math.sqrt(testPrime)
    while ((6 * testNum) + 1 &lt;= sqrt) or ((6 * testNum) - 1 &lt;= sqrt):
        if testPrime % testNum == 0:
           return 'Divisible by %d' % testNum

        testNum = testNum + 2

    return '%d is prime!' % testPrime

startTime = time.time()
result = determineIsPrime(testValue)
endTime = time.time()

print result
print '1. Calculation took %f s\n' % (endTime - startTime)

startTime = time.time()
result = determineIsPrime2(testValue)
endTime = time.time()

print result
print '2. Calculation took %f s\n' % (endTime - startTime)

startTime = time.time()
result = determineIsPrime3(testValue)
endTime = time.time()

print result
print '3. Calculation took %f s\n' % (endTime - startTime)
</pre>
<p><strong>Results:</strong></p>
<pre class="brush: plain;">
Testing 99194853094755497...
99194853094755497 is prime!
1. Calculation took 202.609000 s

99194853094755497 is prime!
2. Calculation took 114.813000 s

99194853094755497 is prime!
3. Calculation took 28.781000 s
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ericsilva.org/2009/12/14/primality-test-v2-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Determining if a Number is Prime</title>
		<link>http://ericsilva.org/2009/12/10/determining-if-a-number-is-prime/</link>
		<comments>http://ericsilva.org/2009/12/10/determining-if-a-number-is-prime/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 12:09:55 +0000</pubDate>
		<dc:creator>Eric Silva</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[LinkedIn]]></category>
		<category><![CDATA[prime numbers]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://ericsilva.org/?p=353</guid>
		<description><![CDATA[While working on some caching settings, I had a need to know if a number is prime. I wrote this little Python script which will tell you if the number defined in the script is indeed a prime. ''' Checks the specified value to determine if it is a prime number. If it is not [...]]]></description>
			<content:encoded><![CDATA[<p>While working on some caching settings, I had a need to know if a number is prime.  I wrote this little Python script which will tell you if the number defined in the script is indeed a prime.</p>
<pre class="brush: python; gutter: true; toolbar: true;">
'''
Checks the specified value to determine if it is a prime number.
If it is not prime the divisor will be returned instead.

@author: Eric Silva
'''

#Change this value to whatever value you want to test for prime.
testValue = 3011

def determineIsPrime(testPrime):
    if testPrime % 2 == 0:
        return 'Divisible by 2'
    testNum = 3
    testLimit = testPrime
    while testLimit &gt;= testNum:
        if testPrime % testNum == 0:
           return 'Divisible by %d' % testNum
        testLimit = testPrime/testNum

        testNum = testNum + 2

    return '%d is prime!' % testPrime

result = determineIsPrime(testValue)

print result
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ericsilva.org/2009/12/10/determining-if-a-number-is-prime/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
