# public Kerrick /project-euler-ruby

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Solve problem 009

 ... ... `@@ -0,0 +1,35 @@` 1 `+# http://projecteuler.net/problem=9` 2 `+#` 3 `+# A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,` 4 `+# a^2 + b^2 = c^2` 5 `+#` 6 `+# For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.` 7 `+#` 8 `+# There exists exactly one Pythagorean triplet for which a + b + c = 1000.` 9 `+# Find the product a * b * c.` 10 `+` 11 `+def find_pythagorean_triplet_by_sum(sum) #Brute force because I don't understand` 12 `+ done = false #the math behind it well enough to come` 13 `+ a, b, c = 1, 1, 1 #up with a more elegant solution.` 14 `+ until done == :error || done == true` 15 `+ c += 1 if c < sum # Increment C first, because a < b < c` 16 `+ if c >= sum && b < sum` 17 `+ c = 0` 18 `+ b += 1` 19 `+ end` 20 `+ if b >= sum && a < sum` 21 `+ b = 0` 22 `+ a += 1` 23 `+ end` 24 `+ done = true if a * a + b * b == c * c && a + b + c == sum` 25 `+ done = :error if a >= sum && b >= sum && c >= sum` 26 `+ end` 27 `+ if done == :error` 28 `+ puts "Error, could not find answer. Terminated at a = #{a}, b = #{b}, c = #{c}"` 29 `+ return :error` 30 `+ end` 31 `+ [a, b, c]` 32 `+end` 33 `+` 34 `+answer = find_pythagorean_triplet_by_sum(1000)` 35 `+puts "Answer: #{answer.reduce(:*)} (a = #{answer[0]}, b = #{answer[1]}, c = #{answer[2]})" unless answer == :error`