Heronian triangles: Difference between revisions

Content deleted Content added
→‎{{header|Ruby}}: corrects the definition of the Triangle ( Struct-> class ) and output.
Line 139:
7 65 68 _ 210 140
3 148 149 _ 210 300</lang>
 
=={{header|Java}}==
<lang Java>
import java.util.ArrayList;
public class Heron {
public static void main(String[] args) {
ArrayList<int[]> list = new ArrayList<int[]>();
boolean found;
for(int c = 1; c <= 200; c++){
for(int b = 1; b <= 200 && b <= c; b++){
for(int a = 1; a <= 200 && a <= b && a <= c; a++){
if(gcd(gcd(a, b), c) == 1){
found = false;
for(int i = 0; i < list.size() && !found; i++){
if(allreadyFound(new int[]{a, b, c}, list.get(i)))
found = true;
}
if(found == false && isHeron(heronArea(a, b, c)))
list.add(new int[]{a, b, c, a + b + c, (int)heronArea(a, b, c)});
}
}
}
}
sort(list);
System.out.printf("Number of primitive Heronian triangles with sides up to 200: %d\n\nFirst ten when ordered by increasing area, then perimeter:\nSides Perimeter Area", list.size());
for(int i = 0; i < 10; i++){
System.out.printf("\n%d x %d x %d %d %d",list.get(i)[0], list.get(i)[1], list.get(i)[2], list.get(i)[3], list.get(i)[4]);
}
System.out.printf("\n\nArea = 210\nSides Perimeter Area");
for(int i = 0; i < list.size(); i++){
if(list.get(i)[4] == 210)
System.out.printf("\n%d x %d x %d %d %d",list.get(i)[0], list.get(i)[1], list.get(i)[2], list.get(i)[3], list.get(i)[4]);
}
}
public static double heronArea(int a, int b, int c){
double s = (a + b + c)/ 2f;
return Math.sqrt(s *(s -a)*(s - b)*(s - c));
}
public static boolean isHeron(double h){
return h % 1 == 0 && h > 0;
}
public static int gcd(int a, int b){
int leftover = 1, dividend = a > b ? a : b, divisor = a > b ? b : a;
while(leftover != 0){
leftover = dividend % divisor;
if(leftover > 0){
dividend = divisor;
divisor = leftover;
}
}
return divisor;
}
public static void sort(ArrayList<int[]> list){
boolean swapped = true;
int[] temp;
while(swapped){
swapped = false;
for(int i = 1; i < list.size(); i++){
if(list.get(i)[4] < list.get(i - 1)[4]){
temp = list.get(i);
list.set(i, list.get(i - 1));
list.set(i - 1, temp);
swapped = true;
}
else if(list.get(i)[4] == list.get(i - 1)[4] && list.get(i)[3] < list.get(i - 1)[3]){
temp = list.get(i);
list.set(i, list.get(i - 1));
list.set(i - 1, temp);
swapped = true;
}
}
}
}
public static boolean allreadyFound(int[] a, int[] b){
return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] || a[0] == b[0] && a[1] == b[2] && a[2] == b[1] || a[0] == b[1] && a[1] == b[0] && a[2] == b[2] || a[0] == b[1] && a[1] == b[2] && a[2] == b[1] ||a[0] == b[2] && a[1] == b[0] && a[2] == b[1] ||a[0] == b[2] && a[1] == b[1] && a[2] == b[0];
}
}
</lang>
{{out}}
<lang>
Number of primitive Heronian triangles with sides up to 200: 517
 
First ten when ordered by increasing area, then perimeter:
Sides Perimeter Area
3 x 4 x 5 12 6
5 x 5 x 6 16 12
5 x 5 x 8 18 12
4 x 13 x 15 32 24
5 x 12 x 13 30 30
9 x 10 x 17 36 36
3 x 25 x 26 54 36
7 x 15 x 20 42 42
10 x 13 x 13 36 60
8 x 15 x 17 40 60
 
Area = 210
Sides Perimeter Area
17 x 25 x 28 70 210
20 x 21 x 29 70 210
12 x 35 x 37 84 210
17 x 28 x 39 84 210
7 x 65 x 68 140 210
3 x 148 x 149 300 210
</lang>
 
=={{header|jq}}==