我目前在教导自己Ada,尽管我可以从解决一些比较传统的问题开始。Ada中的任意长度整数
更具体地说,我尝试计算阶乘n !,而n> 100。我的实现至今是:
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;
的问题是很明显,即使是Long_Long_Integer可以为此太小,并引发对n> 20 CONTRAINT_ERROR例外。
是否有包实现任意大小的整数?
谢谢! PS:我选择了反对递归,因为我想在本练习中探索循环。但否则请评论代码的所有方面(风格,最佳做法,错误..)
非常感谢!这是比我希望得到的更多的答案..我会检查出来。 – Arne 2009-08-22 21:59:36
好的答案,基督徒。我继续并验证了您的额外网页,并将它们转换为适合您的链接。希望从我的upvote中得到额外的10分会帮助你更快地从不受信任的noob地区驱逐出去。 – 2009-08-24 17:39:03