project-euler / Ada / fractions.adb

with Ada.Containers.Vectors;
with Ada.Text_IO;

package body Fractions is
   procedure Print_Decimal(V     : Repeat_Vector.Vector;
                           Index : Repeat_Vector.Extended_Index) is
      First_Index  : Integer := V.First_Index;
      Repeat_Index : Integer := Repeat_Vector.Extended_Index'Pos(Index);
      Last_Index   : Integer := Repeat_Vector.Extended_Index'Pos(V.Last_Index);
   begin
      if Index = Repeat_Vector.No_Index then
         for I in Integer range First_Index .. Last_Index loop
            Ada.Text_IO.Put(Integer'Image(V.Element(I).Quotient));
         end loop;
      else
         for I in Integer range First_Index .. Repeat_Index - 1 loop
            Ada.Text_IO.Put(Integer'Image(V.Element(I).Quotient));
         end loop;
         Ada.Text_IO.Put("(");
         for I in Integer range Repeat_Index .. Last_Index loop
            Ada.Text_IO.Put(Integer'Image(V.Element(I).Quotient));
         end loop;
         Ada.Text_IO.Put(")");
      end if;
   end Print_Decimal;

   function Repeat_Length(Frac : Fraction) return Integer is
      Divisor      : Integer := Frac.Numerator;
      Quotient     : Integer := 0;
      Remainder    : Integer := 0;
      Repeat_Part  : Repeat_Vector.Vector;
      Repeat_Point : Repeat_Vector.Extended_Index;
   begin
      Ada.Text_IO.Put(Integer'Image(Frac.Numerator) & "/" &
                        Integer'Image(Frac.Denominator) & " => ");
  Long_Division:
      while Divisor /= 0 loop
         Divisor := Divisor * 10;
         while Divisor < Frac.Denominator loop
            Divisor := Divisor * 10;
            Repeat_Part.Append(Repeat_Info'(0, Divisor));
         end loop;
         Quotient := Divisor / Frac.Denominator;
         Remainder := Divisor rem Frac.Denominator;

         --  Ada.Text_IO.Put(Integer'Image(Divisor));
         --  Ada.Text_IO.Put(Integer'Image(Quotient));
         --  Ada.Text_IO.Put_Line(Integer'Image(Remainder));

         exit Long_Division
         when Repeat_Part.Contains(Repeat_Info'(Quotient, Remainder));

         Repeat_Part.Append(Repeat_Info'(Quotient, Remainder));
         Divisor := Divisor rem Frac.Denominator;
      end loop Long_Division;

      if Divisor = 0 then
         Repeat_Point := Repeat_Vector.No_Index;
      else
         Repeat_Point := Repeat_Part.Find_Index(Repeat_Info'(Quotient, Remainder));
      end if;

      Ada.Text_IO.Put("0.");
      Print_Decimal(Repeat_Part, Repeat_Point);
      Ada.Text_IO.New_Line;

      if Divisor = 0 then
         return 0;
      else
         return Repeat_Part.Last_Index - Repeat_Point + 1;
      end if;
   end;
end Fractions;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.