Delphi快加大整数?

时间:2014-01-05 21:59:14

标签: delphi delphi-7

function AddNumStrings (Str1, Str2 : string): string;
var
  i : integer;
  carryStr : string;
  worker : integer;
  workerStr,s : string;

  begin
  Result := inttostr (length(Str1));
  Result := '';
  carryStr := '0';

  // make numbers the same length
 s:=StringofChar('0',Length(Str1)-1);
 Str2:=s+Str2;

  i := 0;
  while i < length(Str1) do
  begin
    worker := strtoint(copy(Str1, length(str1)-i, 1)) +
              strtoint(copy(Str2, length(str2)-i, 1)) +
              strtoint (carryStr);
    if worker > 9 then
    begin
      workerStr := inttostr(worker);
      carryStr := copy(workerStr, 1, 1);
      result := copy(workerStr, 2, 1) + result;
    end
    else
    begin
      result := inttostr(worker) + result;
      carryStr := '0';
    end;


    inc(i);
  end; { while }
  if carryStr <> '0' then
    result := carryStr + result;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s,z:String;
begin
s:='1000';
repeat
s:=AddNumStrings(s,'1');

until
Length(s)=1000;


ShowMessage(s);
end;

end.

但这段代码需要时间。对我的代码有什么选择以最快的方式吗? 我工作量很大,所以我必须手动编写“Inc()”程序以获得数十亿的数字。我知道你对它的看法我必须这样做。谢谢..

1 个答案:

答案 0 :(得分:5)

  1. INT128 lib for FPC
  2. GNURZ lib(适用于FPC,但应与Delphi兼容)
  3. GMP(FPC supports it,Delphi also
  4. BigIntBigFloat
  5. BigInt Delphi Library
  6. 另一个BigInt
  7. TPMath
  8. DeHL代表Delphi
  9. BigNumbers BigInteger,BigDecimal和BigRational for Delphi
  10. 希望其中一个更快......