Project Euler Solutions 6 - 10 2

Posted by Aaron Feng Thu, 31 Jan 2008 11:57:00 GMT

6. What is the difference between the sum of the squares and the square of the sums?

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 "Took: #{Time.now - start} seconds"
puts (square_of_sums ** 2) - sum_of_squares

Took: 0.000165 seconds

7. Find the 10001st prime.


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 << n; end
  if prime.length >= 10001 then break; end
  index = n + n 
  while index <= max
    table.delete(index.to_s)
    index = index + n
  end
end

puts "Took: #{Time.now - start} seconds"
puts "#{prime.length} prime is #{prime.last}"

Took: 2.576914

8. Discover the largest product of five consecutive digits in the 1000-digit number.

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 > greatest
      greatest = result
      greatest_5.replace(next_5)
    end

    line_chars.slice!(0,1)
  end

end

puts "Took: #{Time.now - start} seconds"
puts "#{greatest}"
__END__
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Took: 0.0071 seconds

9. Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.

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 "Took: #{Time.now - start} seconds"
        p "#{a} + #{b} + #{c} = #{a + b + c}"
        p "Product is #{a * b * c}"
        exit
      end     
    end
  end
end

Took: 0.522663 seconds

10. Calculate the sum of all the primes below two million.

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 < max_prime
    table.delete(index.to_s)
    index = index + n
  end
end

result = 0

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

puts "Took: #{Time.now - start} seconds"
puts "Result is: #{result}"

Took: 48.149662 seconds

Comments

Leave a response

  1. billy gates Sat, 29 Mar 2008 23:53:39 GMT

    This seems rather, rather slow to compute all primes below 1,000,000 (it should actually be 2,000,000).

    The removing from the table could possibly be the key factor in slowing you down?

    My solution runs in less than 1 sec (0.020 sec when calculating the sum for all primes < 2,000,000)

  2. Aaron Feng Sun, 30 Mar 2008 00:29:48 GMT

    Billy,

    You are totally right! I submitted the correct answer, but some how I copied the wrong code. the 20 seconds is based on one million not two million. It actually took around 48 seconds with two million.

    The delete does slow things down a lot, unfortunately this particular implementation does depend on the delete to happen.

    My goal isn't to write the fastest algorithm, but to solve the problem under the given time constraint specified by project euler. Which is one minute. Also ruby tends to be fairly slow compared to many other languages. Depending on your language of choice, it can have a big impact.

    Maybe when I have time I'll updated the implementation to be more speedy.

    Thanks for pointing it out.