<?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>AaronStaves.com &#187; ProjectEuler</title>
	<atom:link href="http://aaronstaves.com/category/projecteuler/feed/" rel="self" type="application/rss+xml" />
	<link>http://aaronstaves.com</link>
	<description>Not quite extinct!</description>
	<lastBuildDate>Wed, 13 May 2009 18:49:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Problem 10 &#8211; More prime numbers</title>
		<link>http://aaronstaves.com/2008/01/02/problem-10-more-prime-numbers/</link>
		<comments>http://aaronstaves.com/2008/01/02/problem-10-more-prime-numbers/#comments</comments>
		<pubDate>Thu, 03 Jan 2008 04:54:56 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[ProjectEuler]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://aaronstaves.com/2008/01/02/problem-10-more-prime-numbers/</guid>
		<description><![CDATA[So a big part of all this Project Euler nonsense includes prime numbers.  Problem 10, begs the ever-so-common question Find the sum of all the primes below one million.  Obviously, the first attempt would be to just straight out calculate it using my previous program in Problem 3.  Adapting that program to [...]]]></description>
			<content:encoded><![CDATA[<p>So a big part of all this <a href="http://aaronstaves.com/2008/01/01/code-test/" title="Project Euler">Project Euler</a> nonsense includes prime numbers.  Problem 10, begs the ever-so-common question <em>Find the sum of all the primes below one million.</em>  Obviously, the first attempt would be to just straight out calculate it using my previous program in Problem 3.  Adapting that program to one million integers resulted in about 768 seconds (read: over 10 minutes) of time.  Obviously this breaks the 1 minute rule the program needed to be tweaked.</p>
<p>Trying other various methods I still came up short with the result being about 176 seconds at best.  To optimize my program I was mainly looking for some information on how prime numbers might be related to bits/bytes to see if I could perhaps try some bitwise operations to speed things up.  In doing so, I came across an article suggesting that having an array of bits (essentially a long char) with each char representing a number in an array from 0 to x; x being the limit.  Once this array was created, simply just iterate from 0 to the square root of your limit, and mark all multiples of any primes found on your way.  Makes sense - and this <em>greatly</em> cuts down on all the harsh prime calculating going on in the background.<br />
<span id="more-9"></span><br />
As I'm still learning python, I figured I'd try the next best thing and just make an array of boolean variables.  This way I can just check to see if they're true/false.  After adapting my program to do this -  having a representative array as opposed to an array of actual values, the finished product was able to find the answer in 2 to 3 seconds.  Awesome.</p>
<p>Code is posted below.  Sooner or later I'm hoping these problems will force me into creating more of an OO type program.  But for the time being I'm being pretty lame and writing everything procedurally.  As always, comments/criticism is welcome!</p>
<h1>Problem 10</h1>
<p><em>Calculate the sum of all the primes below one million.</em></p>
<pre class="code">
#!/usr/bin/python#function to find if a number is prime

def is_prime(n):

#check found prime numbers first

  global primes

  for prime in primes:

    if(n%prime == 0):

      return False

  return True

#function to mark prime numbers

def mark_primes(n):

  global numbers

  x = 2

  while(x*n &lt; 1000000):

    if(numbers[x*n]):

      numbers[x*n] = False

    x += 1

#initialize vars

total   = 0

numbers = []

primes  = []

#initially set everything to true

for x in xrange(0, 1000000):

  numbers.append(True)

#we know 0 and 1 are false

numbers[0] = False

numbers[1] = False

#start with multiples of primes from 2 to 1000 (sqrt 1,000,000)

for x in xrange(2, 1000):

#if prime, add to prime pool and mark it and it's multiples

  if(is_prime(x)):

    primes.append(x)

    mark_primes(x)

#calculate the final sum

for x in xrange(0, len(numbers)):

  if(numbers[x]):

    total += x

print "Total: ", total</pre>
]]></content:encoded>
			<wfw:commentRss>http://aaronstaves.com/2008/01/02/problem-10-more-prime-numbers/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Project Euler</title>
		<link>http://aaronstaves.com/2008/01/01/code-test/</link>
		<comments>http://aaronstaves.com/2008/01/01/code-test/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 00:33:04 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[ProjectEuler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[python project euler]]></category>

		<guid isPermaLink="false">http://aaronstaves.com/2008/01/01/code-test/</guid>
		<description><![CDATA[So as many already probably know Euler was a famous European mathematician and physicist.  Recently at work a lot of talk has been going around about Project Euler; basically a website which poses mathematical problems that need to be solved by writing program. So a few of us at work have decided to take [...]]]></description>
			<content:encoded><![CDATA[<p>So as many already probably know Euler was a famous European mathematician and physicist.  Recently at work a lot of talk has been going around about <a href="http://projecteuler.net" title="Project Euler" target="_blank">Project Euler</a>; basically a website which poses mathematical problems that need to be solved by writing program. So a few of us at work have decided to take the plunge and learn a completely new language while attempting to answer some of these questions.</p>
<p>So I guess I should start with saying I'll be using/learning <a href="http://python.org" title="Python" target="_blank">Python</a> for all of my problems.  Not a terribly hard choice as I'm already pretty familiar with Perl and PHP which seem to be somewhat similar to Python.  Anyways, if anyone else wants to get in on the fun.  Feel free to do so and post links to your own blogs/solutions or simply just put them here!  I plan on using these solutions as an excuse to actually get posting more on here.  So, we'll see how that goes.  I may also have to make some sort of hiding script so solutions aren't completely revealed to those that want to accomplish everything on their own.  Without further adieu - the first 3 problems and my solutions.<span id="more-8"></span></p>
<h1>Problem 1</h1>
<p><em>Add all the natural numbers below 1000 that are multiples of 3 or 5.</em></p>
<p>A simple brute force script - nothing really too elegant.</p>
<pre class="code">#!/usr/bin/python
total = 0;
for n in range(1, 1000):
  if n%3 == 0 or n%5 == 0:
    total += n

print total</pre>
<h1>Problem 2</h1>
<p><em>Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed one million.</em></p>
<p>Another brute force script - nothing fancy.</p>
<pre class="code">#!/usr/bin/python
f1    = 0
f2    = 1
total = 0;

while (f1+f2) &lt; 1000000:
  tmp = f1 + f2
  if tmp%2 == 0:
    total += tmp
  f1 = f2
  f2 = tmp

print total</pre>
<h1>Problem 3</h1>
<p><em>Find the largest prime factor of 317584931803.</em></p>
<p>This problem was the first one that actually posed some thought.  Given the example "<em>The prime factors of 13195 are 5, 7, 13 and 29.</em>", as well as doing some research; prime factors are just prime numbers that all multiply into the initial number given.  Rather than explaining, I'll just let the code speak for itself.</p>
<pre class="code">#!/usr/bin/python
limit = 317584931803

#function to find if a number is prime
def is_prime(n) :
  test  = 2
  while (test &lt;= n/2):
    if(n%test == 0):
      return False
    else:
      test += 1
  return True

primes  = []  #holds prime factors
num = 2       #starting point

#loop through all possibilities until we hit the limit
while (num &lt;= limit):

  #assume it's not a multiple of any prime numbers
  is_multiple = False
  add = 1

  #check already known primes
  for prime in primes:
    #check to see if it's a multiple of a prime number
    if(num%prime == 0):
      is_multiple = True

  #if it's not a multiple and it devides into our current limit
  if(not is_multiple and limit%num == 0):

    #actually check to see if it's prime
    if(is_prime(num)):

      #it is! don't increment our current number yet
      #until it's not a factor of our limit
      add = 0

      #add to our array, and re-calculate the new limit
      primes.append(num)
      limit = limit/num

  #incriment to the next number
  num += add

#print our results
print primes</pre>
<p>So, all in all these seem like some pretty alright challenges.  I've already got my roomate doing them (Java) and I wouldn't be surprised if a few other friends (on or off the blogroll) would get interested as well.  I'd also be very interested to hear from some of the veteran pyCoders on things they would've done different, or some key aspects of the language that I'm missing.  I quickly found out already that the following code results in an overflow - hence why i'm using the while loop as opposed to a for x in y loop.  As always, comments, criticism and randomness is welcome!</p>
<pre class="code">for x in range(2, 317584931803):
  #creates array with 317584931803 variables! yikes!</pre>
<p>Any better ways to loop through that without having to do a while loop?</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronstaves.com/2008/01/01/code-test/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
