with Ada.Numerics.Big_Numbers.Big_Integers; use Ada.Numerics.Big_Numbers.Big_Integers;
with Ada.Text_IO; use Ada.Text_IO;

--  Big_Integer values of arbitrary precision (D185): far past the 4096 bits they once had,
--  copied, held in records and arrays, and freed as a loop makes and drops them
procedure Main is
   type Pair is record
      Low, High : Big_Integer;
   end record;
   type Row is array (1 .. 4) of Big_Integer;

   function Factorial (N : Natural) return Big_Integer
     with Post => Factorial'Result >= To_Big_Integer (1)
   is
      R : Big_Integer := To_Big_Integer (1);
   begin
      for I in 1 .. N loop
         pragma Loop_Invariant (R >= To_Big_Integer (1));
         R := R * To_Big_Integer (I);
      end loop;
      return R;
   end Factorial;

   Two : constant Big_Integer := To_Big_Integer (2);
   Prime : constant Big_Integer := To_Big_Integer (1_000_000_007);
   F : constant Big_Integer := Factorial (1000);
   P : constant Big_Integer := Two ** 10_000;
   M : constant Big_Integer := Two ** 3000 - To_Big_Integer (1);
   X : Big_Integer := To_Big_Integer (3) ** 2000;
   Q : Pair := (Low => F, High => P);
   R : Row := (others => M);
   Unset : Big_Integer;
begin
   Put_Line (To_String (F mod Prime));
   Put_Line (Natural'Image (To_String (F)'Length));
   Put_Line (Natural'Image (To_String (P)'Length));
   Put_Line (To_String ((P / F) mod Prime));
   Put_Line (To_String (-(P mod (F + To_Big_Integer (1))) mod Prime));
   Put_Line (To_String ((P - (P / Prime) * Prime) - P rem Prime));
   --  each pass makes and drops values of 3000 bits
   for I in 1 .. 20_000 loop
      X := (X * X + To_Big_Integer (I)) mod M;
      Q := (Low => X, High => Q.Low);
      R (I mod 4 + 1) := Q.High;
   end loop;
   Put_Line (To_String (X mod Prime));
   Put_Line (To_String (R (1) mod Prime));
   Put_Line (Boolean'Image (F > P) & Boolean'Image (-F < -P));
   --  (an object declared without a value is finalized as it is first assigned)
   Unset := To_Big_Integer (Integer'Last) * To_Big_Integer (Integer'Last);
   Put_Line (To_String (Unset));
   Put_Line (Integer'Image (To_Integer (Unset / To_Big_Integer (Integer'Last))));
end Main;
