Heronian triangles: Difference between revisions
→{{header|Ruby}}: mostly cosmetic; made valid? a class method.
(→{{header|Ruby}}: Added Ruby) |
(→{{header|Ruby}}: mostly cosmetic; made valid? a class method.) |
||
Line 424:
<lang ruby>Triangle = Struct.new(:a, :b, :c) do
include Comparable
def valid?▼
def self.valid?(a,b,c) # class method
short, middle, long =
short + middle > long
end
@sides ||= [a, b, c].sort
end
Line 433 ⟶ 438:
end
def
Math.sqrt(s*(s - a)*(s - b)*(s - c))▼
end
def
▲ @area ||= Math.sqrt(s*(s - a)*(s - b)*(s - c))
end
def heronian?
end
def <=>(other)
end
Line 462 ⟶ 466:
b.upto(max) do |c|
next if a.gcd(b).gcd(c) > 1
prim_triangles << Triangle.new(a, b, c) if Triangle.valid?(a, b, c)
end
end
end
puts "Primitive heronian triangles with sides upto #{max}: #{sorted.size}"
puts "\
sorted.first(10).each{|tr| puts tr }
puts "\nTriangles with an area of: #{area}"
Line 500 ⟶ 503:
3x148x149 300 210.0
</pre>
=={{header|zkl}}==
{{trans|Python}}
|