<?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: Category project euler</title>
    <link>http://www.aaronfeng.com/articles/category/project-euler</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Adventures in software development</description>
    <item>
      <title>Goodbye ruby and path to a new language</title>
      <description>&lt;p&gt;Well, not in a forever way.  Ruby is the language I use mostly on my own time.  Since my free time is very limited, I need to shelf it for now.  Making room for me to hack some lisp.&lt;/p&gt;

&lt;p&gt;Like many people, my free time comes in small chunks.  I have been using &lt;a href="http://projecteuler.net/"&gt;project euler&lt;/a&gt; as a way to learn new languages (also math).  The code required to solve each problem is usually very short (30 lines or less).  I can usually fit each problem into my small chunks of time very easily.  It's a great way to get your hands dirty on the language you want to learn.&lt;/p&gt;

&lt;p&gt;For those who follow my blog, you probably noticed that I have been posting project euler solutions in ruby.  Since I'm trying to learn lisp now, I'll start to solve the problems using lisp.  I'll post the rest of my solutions written in ruby in the following post.&lt;/p&gt;

&lt;p&gt;Overall, I still enjoy using ruby.  The lack of free time has forced me to make this hard decision.  &lt;/p&gt;</description>
      <pubDate>Tue, 26 Feb 2008 22:45:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:d7c6b7bb-cfde-45e5-8181-5909ba8741fe</guid>
      <author>Aaron Feng</author>
      <link>http://www.aaronfeng.com/articles/2008/02/26/goodbye-ruby-and-path-to-a-new-language</link>
      <category>thoughts</category>
      <category>ruby</category>
      <category>programming</category>
      <category>project euler</category>
    </item>
    <item>
      <title>Project Euler Solutions 6 - 10</title>
      <description>&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=6"&gt;&lt;strong&gt;6.  What is the difference between the sum of the squares and the square of the sums?&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
sum_of_squares = 0
square_of_sums = 0

(1..100).each do |n|
  sum_of_squares = sum_of_squares + n ** 2  
  square_of_sums = square_of_sums + n  
end

puts &amp;quot;Took: #{Time.now - start} seconds&amp;quot;
puts (square_of_sums ** 2) - sum_of_squares&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

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

&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=7"&gt;&lt;strong&gt;7.  Find the 10001st prime.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;
max = 150000
table = {}
prime = [] 

start = Time.now

(2..max).each do |n|
  table[n.to_s] = n
end

(2..(max + 1 / 2)).each do |n|
  unless table[n.to_s].nil? then prime &amp;lt;&amp;lt; n; end
  if prime.length &amp;gt;= 10001 then break; end
  index = n + n 
  while index &amp;lt;= max
    table.delete(index.to_s)
    index = index + n
  end
end

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

&lt;p&gt;Took: 2.576914&lt;/p&gt;

&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=8"&gt;&lt;strong&gt;8.  Discover the largest product of five consecutive digits in the 1000-digit number.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;lines = DATA.readlines

greatest = 0
greatest_5 = []

start = Time.now

lines.each do |line|
  line_chars = line.split(//)

  while not line_chars.empty?
    result = 1
    next_5 = line_chars.slice(0,5)

    next_5.each do |n|
      result = n.to_i * result
    end

    if result &amp;gt; greatest
      greatest = result
      greatest_5.replace(next_5)
    end

    line_chars.slice!(0,1)
  end

end

puts &amp;quot;Took: #{Time.now - start} seconds&amp;quot;
puts &amp;quot;#{greatest}&amp;quot;
__END__
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

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

&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=9"&gt;&lt;strong&gt;9.  Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.&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
(1..1000).each do |a|
  ((a + 1)..1000).each do |b|
    a_2 = a ** 2
    b_2 = b ** 2
    c = Math.sqrt(a_2 + b_2)
    if c == c.to_i
      c = c.to_i
      c_2 = c ** 2

      sum =  Math.sqrt(a_2) + Math.sqrt(b_2) + Math.sqrt(c_2) 

      if sum == 1000 
        puts &amp;quot;Took: #{Time.now - start} seconds&amp;quot;
        p &amp;quot;#{a} + #{b} + #{c} = #{a + b + c}&amp;quot;
        p &amp;quot;Product is #{a * b * c}&amp;quot;
        exit
      end     
    end
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

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

&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=10"&gt;&lt;strong&gt;10.  Calculate the sum of all the primes below two 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

max_prime = 2000000 
table = {}
(2...max_prime).each do |n|
  table[n.to_s] = n
end

(2...(max_prime / 2)).each do |n|
  index = n + n 

  while index &amp;lt; max_prime
    table.delete(index.to_s)
    index = index + n
  end
end

result = 0

table.each do |k,v|
  result = result + v
end

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

&lt;p&gt;Took: 48.149662 seconds&lt;/p&gt;</description>
      <pubDate>Thu, 31 Jan 2008 06:57:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:223c2f6d-b996-42c5-b8ef-807564abd20e</guid>
      <author>Aaron Feng</author>
      <link>http://www.aaronfeng.com/articles/2008/01/31/project-euler-solutions-6-10</link>
      <category>ruby</category>
      <category>project euler</category>
    </item>
    <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>
  </channel>
</rss>
