Cenk Aybeyaz
Cenk Aybeyaz

Reputation: 21

Delphi fast plus big integer?

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.

But this codes takes too time. Is there any options to fastest way for my codes? I m working huge number so I have to write "Inc()" procedure manually for huge number billion digits. I know what you think about it bu I have to do it. Thank you..

Upvotes: 0

Views: 2977

Answers (1)

avra
avra

Reputation: 3730

  1. INT128 lib for FPC
  2. GNURZ lib (for FPC but should be compatible with Delphi)
  3. GMP (FPC supports it, Delphi also)
  4. BigInt and BigFloat
  5. BigInt Delphi Library
  6. Another BigInt
  7. TPMath
  8. DeHL for Delphi
  9. BigNumbers BigInteger, BigDecimal and BigRational for Delphi

Hopefully one of those will be faster...

Upvotes: 6

Related Questions