Project Euler Problem 9 Solution

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2

paroxetine 40mgs price

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

My solution in Ruby:

def get_hypotenuse(a, b)
  Math.sqrt(a**2 + b**2)
end

a = 1
b = 1
while (a < 1000) do
  while ((a**2 + b**2) < 1000**2) do
    c = get_hypotenuse(a, b)
    if (a + b + c) == 1000
      puts a*b*c
      exit(0)
    end
    b += 1
  end
  b = 1
  a += 1
end

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.