Thema: Delphi from C# to delphi

Einzelnen Beitrag anzeigen

sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#3

AW: from C# to delphi

  Alt 22. Dez 2022, 17:01
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.
so many thanks for such a great effort .
  Mit Zitat antworten Zitat