Anonymous user
Heronian triangles: Difference between revisions
→{{header|Ruby}}: corrects the definition of the Triangle ( Struct-> class ) and output.
m (→{{header|REXX}}: localized a subroutine variable, changed whitespace in the program and output.) |
(→{{header|Ruby}}: corrects the definition of the Triangle ( Struct-> class ) and output.) |
||
Line 767:
=={{header|Ruby}}==
<lang ruby>class Triangle
def self.valid?(a,b,c) # class method
short, middle, long = [a, b, c].sort
short + middle > long
end
@sides ||= [a, b, c].sort▼
def initialize(a,b,c)
end
▲ @perimeter ||= a + b + c
▲ @semiperimeter ||= perimeter/2.0
▲ @area ||= Math.sqrt(s*(s - a)*(s - b)*(s - c))
def heronian?
area == area.to_i
end
def <=>(other)
[area, perimeter, sides] <=> [other.area, other.perimeter, other.sides]
end
def to_s
"
end
end
Line 819 ⟶ 809:
puts "Primitive heronian triangles with sides upto #{max}: #{sorted.size}"
puts "\nsides
puts sorted.first(10).
puts "\nTriangles with an area of: #{area}"
sorted.
{{out}}
<pre>
Primitive heronian triangles with sides upto 200: 517
sides
3x4x5
5x5x6
5x5x8
4x13x15
5x12x13
9x10x17
3x25x26
7x15x20
10x13x13
8x15x17
Triangles with an area of: 210
17x25x28
20x21x29
12x35x37
17x28x39
7x65x68
3x148x149
</pre>
|