Commits

Taylor Venable committed c4f0fea

Add solution for #26 in Ada

Comments (0)

Files changed (3)

+with Ada.Text_IO;
+with Fractions;
+
+procedure Euler026 is
+   Current        : Fractions.Fraction;
+   Longest        : Fractions.Fraction := Fractions.Fraction'(0, 1);
+   Repeat_Len     : Integer;
+   Max_Repeat_Len : Integer := 0;
+
+   package Integer_IO is new Ada.Text_IO.Integer_IO (Integer);
+begin
+   for I in Integer range 2 .. 999 loop
+      Current := Fractions.Fraction'(Numerator => 1, Denominator => I);
+      Repeat_Len := Current.Repeat_Length;
+      if Repeat_Len > Max_Repeat_Len then
+         Max_Repeat_Len := Repeat_Len;
+         Longest := Current;
+      end if;
+   end loop;
+   Ada.Text_IO.Put_Line("Denominator   = " & Integer'Image(Longest.Denominator));
+   Ada.Text_IO.Put_Line("Repeat Length = " & Integer'Image(Max_Repeat_Len));
+end Euler026;

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;

Ada/fractions.ads

+with Ada.Containers.Vectors;
+
+package Fractions is
+   type Fraction is tagged record
+      Numerator : Integer;
+      Denominator : Integer;
+   end record;
+
+   type Repeat_Info is record
+      Quotient : Integer;
+      Remainder : Integer;
+   end record;
+
+   subtype Vector_Index is Integer range 0 .. Integer'Last;
+   package Repeat_Vector is new
+     Ada.Containers.Vectors (Index_Type => Vector_Index,
+                             Element_Type => Repeat_Info);
+
+   function Repeat_Length (Frac : Fraction) return Integer;
+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.