Task: Law of cosines - triples




Go: package main

import "fmt"

type triple struct{ a, b, c int }

var squares13 = make(map[int]int, 13)
var squares10000 = make(map[int]int, 10000)

func init() {
    for i := 1; i <= 13; i++ {
        squares13[i*i] = i
    }
    for i := 1; i <= 10000; i++ {
        squares10000[i*i] = i
    }
}

func solve(angle, maxLen int, allowSame bool) []triple {
    var solutions []triple
    for a := 1; a <= maxLen; a++ {
        for b := a; b <= maxLen; b++ {
            lhs := a*a + b*b
            if angle != 90 {
                switch angle {
                case 60:
                    lhs -= a * b
                case 120:
                    lhs += a * b
                default:
                    panic("Angle must be 60, 90 or 120 degrees")
                }
            }
            switch maxLen {
            case 13:
                if c, ok := squares13[lhs]; ok {
                    if !allowSame && a == b && b == c {
                        continue
                    }
                    solutions = append(solutions, triple{a, b, c})
                }
            case 10000:
                if c, ok := squares10000[lhs]; ok {
                    if !allowSame && a == b && b == c {
                        continue
                    }
                    solutions = append(solutions, triple{a, b, c})
                }
            default:
                panic("Maximum length must be either 13 or 10000")
            }
        }
    }
    return solutions
}

func main() {
    fmt.Print("For sides in the range [1, 13] ")
    fmt.Println("where they can all be of the same length:-\n")
    angles := []int{90, 60, 120}
    var solutions []triple
    for _, angle := range angles {
        solutions = solve(angle, 13, true)
        fmt.Printf("  For an angle of %d degrees", angle)
        fmt.Println(" there are", len(solutions), "solutions, namely:")
        fmt.Printf("  %v\n", solutions)
        fmt.Println()
    }
    fmt.Print("For sides in the range [1, 10000] ")
    fmt.Println("where they cannot ALL be of the same length:-\n")
    solutions = solve(60, 10000, false)
    fmt.Print("  For an angle of 60 degrees")
    fmt.Println(" there are", len(solutions), "solutions.")
}








Ada: with Ada.Text_IO;
with Ada.Containers.Ordered_Maps;

procedure Law_Of_Cosines is

   type Angle_Kind is (Angle_60, Angle_90, Angle_120);

   function Is_Triangle (A, B, C : in Positive;
                         Angle   : in Angle_Kind) return Boolean
   is
      A2 : constant Positive := A**2;
      B2 : constant Positive := B**2;
      C2 : constant Positive := C**2;
      AB : constant Positive := A * B;
   begin
      case Angle is
         when Angle_60  =>  return A2 + B2 - AB = C2;
         when Angle_90  =>  return A2 + B2 = C2;
         when Angle_120 =>  return A2 + B2 + AB = C2;
      end case;
   end Is_Triangle;

   procedure Count_Triangles is
      use Ada.Text_IO;
      Count : Natural;
   begin
      for Angle in Angle_Kind loop
         Count := 0;
         Put_Line (Angle'Image & " triangles");
         for A in 1 ..13 loop
            for B in 1 .. A loop
               for C in 1 .. 13 loop
                  if Is_Triangle (A, B, C, Angle) then
                     Put_Line (A'Image & B'Image & C'Image);
                     Count := Count + 1;
                  end if;
               end loop;
            end loop;
         end loop;
         Put_Line ("There are " & Count'Image & " " & Angle'Image &" triangles");
      end loop;
   end Count_Triangles;

   procedure Extra_Credit (Limit : in Natural) is
      use Ada.Text_IO;

      package Square_Maps is new Ada.Containers.Ordered_Maps (Natural, Boolean);
      Squares : Square_Maps.Map;

      Count : Natural :=  0;
   begin
      for C in 1 .. Limit loop
         Squares.Insert (C**2, True);
      end loop;

      for A in 1 .. Limit loop
         for B in 1 .. A loop
            if Squares.Contains (A**2 + B**2 - A * B) then
               Count := Count + 1;
            end if;
         end loop;
      end loop;
      Put_Line ("There are " & Natural'(Count - Limit)'Image &
                  " " & Angle_60'Image &" triangles between 1 and " & Limit'Image & ".");
   end Extra_Credit;

begin
   Count_Triangles;
   Extra_Credit (Limit => 10_000);
end Law_Of_Cosines;