How to resolve the algorithm Heronian triangles step by step in the J programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Heronian triangles step by step in the J programming language
Table of Contents
Problem Statement
Hero's formula for the area of a triangle given the length of its three sides a, b, and c is given by: where s is half the perimeter of the triangle; that is,
Heronian triangles are triangles whose sides and area are all integers.
Note that any triangle whose sides are all an integer multiple of 3, 4, 5; such as 6, 8, 10, will also be a Heronian triangle. Define a Primitive Heronian triangle as a Heronian triangle where the greatest common divisor of all three sides is 1 (unity). This will exclude, for example, triangle 6, 8, 10.
Show all output here. Note: when generating triangles it may help to restrict
a <= b <= c
{\displaystyle a<=b<=c}
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Heronian triangles step by step in the J programming language
Source code in the j programming language
a=: 0&{"1
b=: 1&{"1
c=: 2&{"1
s=: (a+b+c) % 2:
area=: 2 %: s*(s-a)*(s-b)*(s-c) NB. Hero's formula
perim=: +/"1
isPrimHero=: (0&~: * (= <.@:+))@area * 1 = a +. b +. c
perim=: +/"1
s=: -:@:perim
area=: [: %: s * [: */"1 s - ] NB. Hero's formula
isNonZeroInt=: 0&~: *. (= <.@:+)
isPrimHero=: isNonZeroInt@area *. 1 = +./&.|:
Tri=:(1-i.3)+"1]3 comb 202 NB. distinct triangles with sides <= 200
HeroTri=: (#~ isPrimHero) Tri NB. all primitive Heronian triangles with sides <= 200
# HeroTri NB. count triangles found
517
HeroTri=: (/: area ,. perim ,. ]) HeroTri NB. sort by area, perimeter & sides
(,. _ ,. perim ,. area) 10 {. HeroTri NB. tabulate sides, perimeter & area for top 10 triangles
3 4 5 _ 12 6
5 5 6 _ 16 12
5 5 8 _ 18 12
4 13 15 _ 32 24
5 12 13 _ 30 30
9 10 17 _ 36 36
3 25 26 _ 54 36
7 15 20 _ 42 42
10 13 13 _ 36 60
8 15 17 _ 40 60
(,. _ ,. perim ,. area) (#~ 210 = area) HeroTri NB. tablulate sides, perimeter & area for triangles with area = 210
17 25 28 _ 70 210
20 21 29 _ 70 210
12 35 37 _ 84 210
17 28 39 _ 84 210
7 65 68 _ 140 210
3 148 149 _ 300 210
You may also check:How to resolve the algorithm Host introspection step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the Sidef programming language
You may also check:How to resolve the algorithm Array length step by step in the Objeck programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the PureBasic programming language