Heronian triangles: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Added 11l)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 35:
 
<small>'''Note''': when generating triangles it may help to restrict</small> <math>a <= b <= c</math>
 
 
=={{header|11l}}==
Line 493 ⟶ 492:
3 148 149 210 300
</pre>
 
 
=={{header|AppleScript}}==
Line 1,018 ⟶ 1,016:
149 x 148 x 3 300 210
</pre>
 
=={{header|C sharp|C#}}==
<lang Csharp>using System;
using System.Collections.Generic;
 
namespace heron
{
class Program{
static void Main(string[] args){
List<int[]> list = new List<int[]>();
for (int c = 1; c <= 200; c++)
for (int b = 1; b <= c; b++)
for (int a = 1; a <= b; a++)
if (gcd(a, gcd(b, c)) == 1 && isHeron(heronArea(a, b, c)))
list.Add(new int[] { a, b, c, a + b + c, (int)heronArea(a, b, c)});
sort(list);
Console.WriteLine("Number of primitive Heronian triangles with sides up to 200: " + list.Count + "\n\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:\nSides\t\t\tPerimeter\tArea");
for(int i = 0; i < 10; i++)
Console.WriteLine(list[i][0] + "\t" + list[i][1] + "\t" + list[i][2] + "\t" + list[i][3] + "\t\t" + list[i][4]);
Console.WriteLine("\nPerimeter = 210\nSides\t\t\tPerimeter\tArea");
foreach (int[] i in list)
if (i[4] == 210)
Console.WriteLine(i[0] + "\t" + i[1] + "\t" + i[2] + "\t" + i[3] + "\t\t" + i[4]);
}
static bool isHeron(double heronArea){
return heronArea % 1 == 0 && heronArea != 0;
}
static double heronArea(int a, int b, int c){
double s = (a + b + c) / 2d;
return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
}
static int gcd(int a, int b){
int remainder = 1, dividend, divisor;
dividend = a > b ? a : b;
divisor = a > b ? b : a;
while (remainder != 0){
remainder = dividend % divisor;
if (remainder != 0){
dividend = divisor;
divisor = remainder;
}
}
return divisor;
}
static void sort(List<int[]> list){
int[] temp = new int[5];
bool changed = true;
while(changed){
changed = false;
for (int i = 1; i < list.Count; i++)
if (list[i][4] < list[i - 1][4] || list[i][4] == list[i - 1][4] && list[i][3] < list[i - 1][3]){
temp = list[i];
list[i] = list[i - 1];
list[i - 1] = temp;
changed = true;
}
}
}
}
}</lang>
{{out}}
<pre>Number of primitive Heronian triangles with sides up to 200: 517
 
First ten when ordered by increasing area, then perimeter,then maximum sides:
Sides Perimeter Area
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
 
Perimeter = 210
Sides Perimeter Area
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</pre>
 
=={{header|C++}}==
Line 1,131 ⟶ 1,213:
210 140 68x65x7
210 300 149x148x3</pre>
 
=={{header|C sharp|C#}}==
<lang Csharp>using System;
using System.Collections.Generic;
 
namespace heron
{
class Program{
static void Main(string[] args){
List<int[]> list = new List<int[]>();
for (int c = 1; c <= 200; c++)
for (int b = 1; b <= c; b++)
for (int a = 1; a <= b; a++)
if (gcd(a, gcd(b, c)) == 1 && isHeron(heronArea(a, b, c)))
list.Add(new int[] { a, b, c, a + b + c, (int)heronArea(a, b, c)});
sort(list);
Console.WriteLine("Number of primitive Heronian triangles with sides up to 200: " + list.Count + "\n\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:\nSides\t\t\tPerimeter\tArea");
for(int i = 0; i < 10; i++)
Console.WriteLine(list[i][0] + "\t" + list[i][1] + "\t" + list[i][2] + "\t" + list[i][3] + "\t\t" + list[i][4]);
Console.WriteLine("\nPerimeter = 210\nSides\t\t\tPerimeter\tArea");
foreach (int[] i in list)
if (i[4] == 210)
Console.WriteLine(i[0] + "\t" + i[1] + "\t" + i[2] + "\t" + i[3] + "\t\t" + i[4]);
}
static bool isHeron(double heronArea){
return heronArea % 1 == 0 && heronArea != 0;
}
static double heronArea(int a, int b, int c){
double s = (a + b + c) / 2d;
return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
}
static int gcd(int a, int b){
int remainder = 1, dividend, divisor;
dividend = a > b ? a : b;
divisor = a > b ? b : a;
while (remainder != 0){
remainder = dividend % divisor;
if (remainder != 0){
dividend = divisor;
divisor = remainder;
}
}
return divisor;
}
static void sort(List<int[]> list){
int[] temp = new int[5];
bool changed = true;
while(changed){
changed = false;
for (int i = 1; i < list.Count; i++)
if (list[i][4] < list[i - 1][4] || list[i][4] == list[i - 1][4] && list[i][3] < list[i - 1][3]){
temp = list[i];
list[i] = list[i - 1];
list[i - 1] = temp;
changed = true;
}
}
}
}
}</lang>
{{out}}
<pre>Number of primitive Heronian triangles with sides up to 200: 517
 
First ten when ordered by increasing area, then perimeter,then maximum sides:
Sides Perimeter Area
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
 
Perimeter = 210
Sides Perimeter Area
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</pre>
 
=={{header|CoffeeScript}}==
Line 3,478 ⟶ 3,476:
210 140 7×65×68
210 300 3×148×149</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2018.09}}
<lang perl6>sub hero($a, $b, $c) {
my $s = ($a + $b + $c) / 2;
($s * ($s - $a) * ($s - $b) * ($s - $c)).sqrt;
}
sub heronian-area($a, $b, $c) {
$_ when Int given hero($a, $b, $c).narrow;
}
 
sub primitive-heronian-area($a, $b, $c) {
heronian-area $a, $b, $c
if 1 == [gcd] $a, $b, $c;
}
 
sub show(@measures) {
say " Area Perimeter Sides";
for @measures -> [$area, $perim, $c, $b, $a] {
printf "%6d %6d %12s\n", $area, $perim, "$a×$b×$c";
}
}
sub MAIN ($maxside = 200, $first = 10, $witharea = 210) {
my @hh[1000];
my atomicint $i;
(1 .. $maxside).race(:12batch).map: -> $c {
for 1 .. $c -> $b {
for $c - $b + 1 .. $b -> $a {
if primitive-heronian-area($a,$b,$c) -> $area {
@hh[$i⚛++] = [$area, $a+$b+$c, $c, $b, $a];
}
}
}
}
 
my @h = (@hh.grep: so *).sort;
say "Primitive Heronian triangles with sides up to $maxside: ", +@h;
 
say "\nFirst $first:";
show @h[^$first];
 
say "\nArea $witharea:";
show @h.grep: *[0] == $witharea;
}</lang>
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
 
First 10:
Area Perimeter Sides
6 12 3×4×5
12 16 5×5×6
12 18 5×5×8
24 32 4×13×15
30 30 5×12×13
36 36 9×10×17
36 54 3×25×26
42 42 7×15×20
60 36 10×13×13
60 40 8×15×17
 
Area 210:
Area Perimeter Sides
210 70 17×25×28
210 70 20×21×29
210 84 12×35×37
210 84 17×28×39
210 140 7×65×68
210 300 3×148×149</pre>
 
=={{header|Phix}}==
Line 3,896 ⟶ 3,824:
 
<br><br>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.09}}
<lang perl6>sub hero($a, $b, $c) {
my $s = ($a + $b + $c) / 2;
($s * ($s - $a) * ($s - $b) * ($s - $c)).sqrt;
}
sub heronian-area($a, $b, $c) {
$_ when Int given hero($a, $b, $c).narrow;
}
 
sub primitive-heronian-area($a, $b, $c) {
heronian-area $a, $b, $c
if 1 == [gcd] $a, $b, $c;
}
 
sub show(@measures) {
say " Area Perimeter Sides";
for @measures -> [$area, $perim, $c, $b, $a] {
printf "%6d %6d %12s\n", $area, $perim, "$a×$b×$c";
}
}
sub MAIN ($maxside = 200, $first = 10, $witharea = 210) {
my @hh[1000];
my atomicint $i;
(1 .. $maxside).race(:12batch).map: -> $c {
for 1 .. $c -> $b {
for $c - $b + 1 .. $b -> $a {
if primitive-heronian-area($a,$b,$c) -> $area {
@hh[$i⚛++] = [$area, $a+$b+$c, $c, $b, $a];
}
}
}
}
 
my @h = (@hh.grep: so *).sort;
say "Primitive Heronian triangles with sides up to $maxside: ", +@h;
 
say "\nFirst $first:";
show @h[^$first];
 
say "\nArea $witharea:";
show @h.grep: *[0] == $witharea;
}</lang>
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
 
First 10:
Area Perimeter Sides
6 12 3×4×5
12 16 5×5×6
12 18 5×5×8
24 32 4×13×15
30 30 5×12×13
36 36 9×10×17
36 54 3×25×26
42 42 7×15×20
60 36 10×13×13
60 40 8×15×17
 
Area 210:
Area Perimeter Sides
210 70 17×25×28
210 70 20×21×29
210 84 12×35×37
210 84 17×28×39
210 140 7×65×68
210 300 3×148×149</pre>
 
=={{header|REXX}}==
=== using iSQRT ===
Line 4,568:
(3, 148, 149) perimiter = 300; area = 210
</pre>
 
=={{header|VBA}}==
{{trans|Phix}}<lang vb>Function heroArea(a As Integer, b As Integer, c As Integer) As Double
Line 4,644 ⟶ 4,645:
210 140 68x65x7
210 300 149x148x3</pre>
 
=={{header|zkl}}==
{{trans|Python}}
10,333

edits