Hello!
I am currently teaching myself Ada and though I could begin by tackling some of the more conventional problems to get started.
More specifically I try to calculate the factorial n!, whereas n>100. My implementation so far is:
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
procedure Factorial is
-- define a type covering the range beginning at 1 up to which faculty is to
-- be computed.
subtype Argument is Long_Long_Integer range 1..100;
-- define a type that is large enough to hold the result
subtype Result is Long_Long_Integer range 1..Long_Long_Integer'Last;
package Result_IO is new Ada.Text_IO.Integer_IO(Result); use Result_IO;
-- variable holding the faculty calculated.
fac : Result := 1;
begin
-- loop over whole range of ARGUMENT and calculate n!
for n in ARGUMENT loop
fac := (fac * n);
end loop;
end;
The problem is obviously that even Long_Long_Integer is may too small for this and throws a CONTRAINT_ERROR exception for n>20.
Is there a package that implements arbitrary-sized integers?
Thanks!
PS: I did opt against recursion because I wanted to explore loops in this exercise. But otherwise please comment on all aspects of the code (style, best-practices, error..)