From: Gavin Kistner Date: 2011-09-04T00:36:58+09:00 Subject: Re: Beginner needing help - Writing right-angle triangle program On Sep 3, 2011, at 7:49 AM, Patrick Lynch wrote: > I've been going through a Haskell tutorial (Just to see what it's like) > and saw an example program which would tell me how long each side a > right-angle triangle is, when given the perimeter. It seemed pretty > simple, and me, being the kind of guy that likes to write the same > program in multiple languages, tried to write it in ruby. Here's a simple, rather naïve approach: def right_triangle_sides( circumference ) [].tap do |solutions| 1.upto(circumference-2) do |a| 1.upto(a-1) do |b| c = circumference-a-b if a*a+b*b == c*c solutions.concat( [a,b,c].permutation.to_a ) end end end end end right_triangle_sides(24).each do |sides| p sides end #=> [8, 6, 10] #=> [8, 10, 6] #=> [6, 8, 10] #=> [6, 10, 8] #=> [10, 8, 6] #=> [10, 6, 8]