Heronian triangles: Difference between revisions

Added 11l
(→‎{{header|Python}}: Improved performance by restricting search space - dropped the import of 'product')
(Added 11l)
Line 36:
<small>'''Note''': when generating triangles it may help to restrict</small> <math>a <= b <= c</math>
 
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>F gcd(=u, =v)
L v != 0
(u, v) = (v, u % v)
R abs(u)
 
F hero(a, b, c)
V s = (a + b + c) / 2
V a2 = s * (s - a) * (s - b) * (s - c)
R I a2 > 0 {sqrt(a2)} E 0
 
F is_heronian(a, b, c)
V x = hero(a, b, c)
R x > 0 & fract(x) == 0
 
F gcd3(x, y, z)
R gcd(gcd(x, y), z)
 
V MAXSIDE = 200
[(Int, Int, Int)] h
L(x) 1..MAXSIDE
L(y) x..MAXSIDE
L(z) y..MAXSIDE
I (x + y > z) & gcd3(x, y, z) == 1 & is_heronian(x, y, z)
h [+]= (x, y, z)
 
h = sorted(h, key' x -> (hero(x[0], x[1], x[2]), sum(x), (x[2], x[1], x[0])))
 
print(‘Primitive Heronian triangles with sides up to #.: #.’.format(MAXSIDE, h.len))
print("\nFirst ten when ordered by increasing area, then perimeter, then maximum sides:")
print(h[0.<10].map3((x, y, z) -> ‘ #14 perim: #3 area: #.’.format(String((x, y, z)), x + y + z, hero(x, y, z))).join("\n"))
print("\nAll with area 210 subject to the previous ordering:")
print(h.filter3((x, y, z) -> hero(x, y, z) == 210).map3((x, y, z) -> ‘ #14 perim: #3 area: #.’.format(String((x, y, z)), x + y + z, hero(x, y, z))).join("\n"))</lang>
 
{{out}}
<pre>
Primitive Heronian triangles with sides up to 200: 517
 
First ten when ordered by increasing area, then perimeter, then maximum sides:
(3, 4, 5) perim: 12 area: 6
(5, 5, 6) perim: 16 area: 12
(5, 5, 8) perim: 18 area: 12
(4, 13, 15) perim: 32 area: 24
(5, 12, 13) perim: 30 area: 30
(9, 10, 17) perim: 36 area: 36
(3, 25, 26) perim: 54 area: 36
(7, 15, 20) perim: 42 area: 42
(10, 13, 13) perim: 36 area: 60
(8, 15, 17) perim: 40 area: 60
 
All with area 210 subject to the previous ordering:
(17, 25, 28) perim: 70 area: 210
(20, 21, 29) perim: 70 area: 210
(12, 35, 37) perim: 84 area: 210
(17, 28, 39) perim: 84 area: 210
(7, 65, 68) perim: 140 area: 210
(3, 148, 149) perim: 300 area: 210
</pre>
 
=={{header|Ada}}==
1,481

edits