Thema: Delphi from C# to delphi

Einzelnen Beitrag anzeigen

Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.617 Beiträge
 
Delphi 12 Athens
 
#2

AW: from C# to delphi

  Alt 22. Dez 2022, 16:04
Completely untested (written in Notepad++):
Delphi-Quellcode:
function PQFactorize(pq: LongWord): LongWord;

  function gcd(left, right: LongWord): LongWord;
  var
    num: LongWord;
  begin
    while right <> 0 do
      begin
        num := left mod right;
        left := right;
        right := num;
      end;
    Result := left;
  end;

var
  g, q, x, y, z, res, a, other: LongWord;
  i, j, iter, lim: integer;
begin
  if pq < 2 then
    Exit(1);
  g := 0;
  i := 0;
  iter := 0;
  repeat
    q := LongWord(Random(16) + 17) mod (pq - 1);
    x := (LongWord(Random(MAXINT)) + LongWord(Random(MAXINT)) shl 31) mod (pq - 1) + 1;
    y := x;
    lim := 1 shl (math.Min(5, i) + 18);
    for j := 1 to lim - 1 do
      begin
        inc(iter);
        res := q;
        a := x;
        while x <> 0 do
          begin
            if (x and 1) <> 0 then
              res := (res + a) mod pq;
            a := (a + a) mod pq;
            x := x shr 1;
          end;
        x := res;
        if x < y then
          z := pq + x - y
        else
          z := x - y;
        g := gcd(z, pq);
        if g <> 1 then
          break;
        if (j and (j - 1)) = 0 then
          y := x;
      end;
    if (g > 1) and (g < pq) then
      break;
    inc(i);
  until (i >= 3) or (iter >= 1000);
  if (g <> 0) then
    begin
      other := pq div g;
      if (other < g) then
        g := other;
    end;
  Result := g;
end;
The keyword "static" implies that this is a static class function, so you have to declare it as such.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat