Einzelnen Beitrag anzeigen

Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#3

AW: Dezimal zu Dual, alternative zum Horner-Schema

  Alt 10. Feb 2013, 21:22
Bevor du dir viel Arbeit machst, schau mal hier:

Delphi-Quellcode:
function PowerAndMod(A, E, M: int64): int64;
begin
  Result := 0;
  if E < 0 then
    Exit;
  if (M = 1) or (M = -1) then
    Exit;
  if (A = 0) and (E > 0) then
    Exit;
  Result := 1;
  try
    while E > 0 do
      if E mod 2 <> 0 then // Odd
      begin
        Result := (Result * A) mod M;
        E := E - 1;
      end
      else
      begin
        A := (A * A) mod M;
        E := E div 2;
      end;
  except
    raise Exception.Create('Int64 Overflow');
  end;
end;
  Mit Zitat antworten Zitat