Heronian triangles: Difference between revisions

→‎{{header|Python}}: Improved performance by restricting search space - dropped the import of 'product'
m (→‎{{header|Python}}: (Replaced deprecated fractions.gcd with math.gcd))
(→‎{{header|Python}}: Improved performance by restricting search space - dropped the import of 'product')
Line 3,635:
<lang python>from __future__ import division, print_function
from math import gcd, sqrt
from itertools import product
 
 
Line 3,654 ⟶ 3,653:
 
if __name__ == '__main__':
maxsideMAXSIDE = 200
 
h = [(a, b, c) for a, b, c in product(range(1, maxside + 1), repeat=3)
N = 1 + MAXSIDE
if a <= b <= c and a + b > c and gcd3(a, b, c) == 1 and is_heronian(a, b, c)]
h = [(x, y, z)
h.sort(key = lambda x: (hero(*x), sum(x), x[::-1])) # By increasing area, perimeter, then sides
for x in range(1, N)
print('Primitive Heronian triangles with sides up to %i:' % maxside, len(h))
for y in range(x, N)
print('\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:')
for z in range(y, N) if (x + y > z) and
1 == gcd3(x, y, z) and
is_heronian(x, y, z)]
 
# By increasing area, perimeter, then sides
h.sort(key = lambda x: (hero(*x), sum(x), x[::-1])) # By increasing area, perimeter, then sides
 
print(
print( 'Primitive Heronian triangles with sides up to %i:' % maxsideMAXSIDE, len(h))
)
print('\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:'),
'then maximum sides:')
print('\n'.join(' %14r perim: %3i area: %i'
% (sides, sum(sides), hero(*sides)) for sides in h[:10]))
Line 3,665 ⟶ 3,676:
print('\n'.join(' %14r perim: %3i area: %i'
% (sides, sum(sides), hero(*sides)) for sides in h
if hero(*sides) == 210))</lang>
</lang>
 
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
9,659

edits