<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Aaron Feng: Project Euler Solutions 1 - 5</title>
    <link>http://www.aaronfeng.com/articles/2007/12/31/project-euler-solutions-1-5</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Adventures in software development</description>
    <item>
      <title>Project Euler Solutions 1 - 5</title>
      <description>&lt;p&gt;&lt;em&gt;Click on each problem for a more detailed solution.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=1"&gt;&lt;strong&gt;1. Add all the natural numbers below 1000 that are multiples of 3 or 5.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;start = Time.now
total = 0
(1...1000).each do |n|
  total += n if (n % 3).zero? or (n % 5).zero?
end

puts &amp;quot;Took: #{Time.now - start} seconds&amp;quot;
puts total&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Took: 0.000977 seconds&lt;/p&gt;

&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=2"&gt;&lt;strong&gt;2. Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed one million.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;start = Time.now
def fib(n1, n2, total)
  return total if n2 &amp;gt; 1000000 
  total += n2 if (n2 % 2).zero? 
  fib(n2, n1 + n2, total)
end

puts &amp;quot;Took: #{Time.now - start} seconds&amp;quot;
puts fib(1, 2, 0)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Took: 1.0e-05 seconds&lt;/p&gt;

&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=3"&gt;&lt;strong&gt;3. Find the largest prime factor of 317584931803.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;def next_prime(start_num, max_num)
  is_prime = true
  prime = 0
  (start_num + 1..max_num).each do |n|
    prime = n
    (2..n - 1).each do |nn| 
      if (n % nn).zero? then is_prime = false; break; end
    end
    if is_prime then break; end
    is_prime = true
  end
  prime
end

start = Time.now
n = 1
biggest_prime = 0
num = 317584931803

while(num != 1 or num &amp;gt; n)
  n = next_prime(n,  num)
  if n &amp;gt; 0
    result = num % n 

    if (result).zero?
      biggest_prime = n
      num = num / n 
    end
  end
  n += 1  
end

puts &amp;quot;Took: #{Time.now - start} seconds&amp;quot;
puts biggest_prime&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Took: 0.477182 seconds&lt;/p&gt;

&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=4"&gt;&lt;strong&gt;4.  Find the largest palindrome made from the product of two 3-digit numbers.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;start = Time.now
result = 0
left = 0
right = 0

(100...1000).to_a.reverse.each do |l|
  (100...1000).to_a.reverse.each do |r|
    temp = (l * r).to_s 

    if temp == temp.reverse and temp.to_i &amp;gt; result
      result = temp.to_i
      left = l
      right = r
    end     
  end
end

puts &amp;quot;Took: #{Time.now - start} seconds&amp;quot;
puts &amp;quot;#{left} * #{right} = #{result}&amp;quot;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Took: 1.501993 seconds&lt;/p&gt;

&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=5"&gt;&lt;strong&gt;5. What is the smallest number divisible by each of the numbers 1 to 20?&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;def is_prime(num)
  if num &amp;lt; 2  then return false; end
  (2..num - 1).each do |n|
    if (num % n).zero?
      return false
    end
  end
  return true
end

def smallest_factor(num)
  (2..num - 1).each do |n|
    if (num % n).zero? then return n; end
  end
  return num
end

start = Time.now
result = 1 
(1..20).each do |n|
  if is_prime(n) 
    result = result * n
  elsif not (result % n).zero? 
    result = result * smallest_factor(n) 
  end
end

puts &amp;quot;Took: #{Time.now - start} seconds&amp;quot;
puts result&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Took: 0.00018 seconds&lt;/p&gt;</description>
      <pubDate>Mon, 31 Dec 2007 18:49:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:99f7d211-d0cc-40dc-badb-049d4a23c360</guid>
      <author>Aaron Feng</author>
      <link>http://www.aaronfeng.com/articles/2007/12/31/project-euler-solutions-1-5</link>
      <category>programming</category>
      <category>project euler</category>
    </item>
    <item>
      <title>"Project Euler Solutions 1 - 5" by Aaron Feng</title>
      <description>&lt;p&gt;Ah yes.  Thanks for the correction.&lt;/p&gt;</description>
      <pubDate>Mon, 07 Jan 2008 22:34:16 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:969b585d-8423-4e8d-91a6-84e60562f796</guid>
      <link>http://www.aaronfeng.com/articles/2007/12/31/project-euler-solutions-1-5#comment-4329</link>
    </item>
    <item>
      <title>"Project Euler Solutions 1 - 5" by ashton</title>
      <description>&lt;p&gt;Aaron,
No, I think you are a bit confused.
(n2 % 2) evaluates to being true if it's nonzero, meaning:
((n2 % 2) != 0), which assumes that if it's odd, then perform the "then" statement to the if component.&lt;/p&gt;

&lt;p&gt;i.e.,
if ((n2 %2) == 1)
   sum += 0;
else
   sum += n2;&lt;/p&gt;</description>
      <pubDate>Mon, 07 Jan 2008 21:34:45 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:8880e77b-27bf-4d2f-b7a4-79c7e7f57257</guid>
      <link>http://www.aaronfeng.com/articles/2007/12/31/project-euler-solutions-1-5#comment-4328</link>
    </item>
    <item>
      <title>"Project Euler Solutions 1 - 5" by Aaron Feng</title>
      <description>&lt;p&gt;I agree.  It's matter of taste.  There are many ways a problem can be solved.  It's just matter of how you picture it in your head.&lt;/p&gt;

&lt;p&gt;On the side note, I think you meant: sum += (n2 % 2) ? n2 : 0;&lt;/p&gt;</description>
      <pubDate>Sun, 06 Jan 2008 17:37:16 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ed94a898-c2eb-4d67-a966-75004c105d33</guid>
      <link>http://www.aaronfeng.com/articles/2007/12/31/project-euler-solutions-1-5#comment-4326</link>
    </item>
    <item>
      <title>"Project Euler Solutions 1 - 5" by ashton</title>
      <description>&lt;p&gt;Why do you use tail recursion, when it's not necessary?
It bottoms out to doing a loop...
int n1 = 1, n2 = 2, sum = 0;
while (n2 &amp;lt;= 1000000) {
   sum += (n2 % 2) ? 0 : n2;
   n2 += n1;
   n1 = n2 - n1;
}&lt;/p&gt;</description>
      <pubDate>Fri, 04 Jan 2008 18:43:24 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b88841c2-ebfd-42cf-bafb-1e54dd22af52</guid>
      <link>http://www.aaronfeng.com/articles/2007/12/31/project-euler-solutions-1-5#comment-4320</link>
    </item>
    <item>
      <title>"Project Euler Solutions 1 - 5" by john</title>
      <description>&lt;p&gt;Divisible by all 1, ..., 20.
= LCM(1,...,20)
lcm(a,b,c) = lcm(a,lcm(b,c)) ... etc&lt;/p&gt;

&lt;p&gt;lcm(x,y) = x * y / gcd(x,y)
gcd(x,y) = return x == 0 ? y : gcd(y, x%y)&lt;/p&gt;</description>
      <pubDate>Wed, 02 Jan 2008 22:09:41 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1d6ffee4-fdb3-4afc-9a51-9f320d244ffe</guid>
      <link>http://www.aaronfeng.com/articles/2007/12/31/project-euler-solutions-1-5#comment-4319</link>
    </item>
  </channel>
</rss>
