diff --git a/code/python/src/algorithms/pollard_rho/algorithms.py b/code/python/src/algorithms/pollard_rho/algorithms.py index 0067ca7..493d9ca 100644 --- a/code/python/src/algorithms/pollard_rho/algorithms.py +++ b/code/python/src/algorithms/pollard_rho/algorithms.py @@ -30,24 +30,37 @@ def pollard_rho_algorithm( x_init: int = 2, verbose: bool = False, ): - d = 1; - x = y = x_init; steps = []; - steps.append(Step(x=x, y=y)); success = False; f = lambda _: fct(_, n=n); + + d = 1; + x = y = x_init; + steps.append(Step(x=x)); + k = 0; + k_next = 1; + while True: + # aktualisiere x: x = f(x_prev): x = f(x); - y = f(f(y)); + # aktualisiere y, wenn k = 2^j: y = x[j] = f(y_prev): + if k == k_next: + k_next = 2*k_next; + y = f(y); + + # ggT berechnen: d = math.gcd(abs(x-y), n); steps.append(Step(x=x, y=y, d=d)); - if d == 1: + + # Abbruchkriterien prüfen: + if d == 1: # weitermachen, solange d == 1 + k += 1; continue; - elif d < n: - success = True; + elif d == n: # versagt + success = False; break; else: - success = False; + success = True; break; if verbose: diff --git a/code/python/src/algorithms/pollard_rho/display.py b/code/python/src/algorithms/pollard_rho/display.py index 7528211..6ff792e 100644 --- a/code/python/src/algorithms/pollard_rho/display.py +++ b/code/python/src/algorithms/pollard_rho/display.py @@ -27,14 +27,14 @@ def display_table(steps: List[Step]) -> str: table = pd.DataFrame({ 'i': [i for i in range(len(steps))], 'x': [step.x for step in steps], - 'y': [step.y for step in steps], + 'y': [step.y or '-' for step in steps], 'd': [step.d or '-' for step in steps], }) \ .reset_index(drop=True); # benutze pandas-Dataframe + tabulate, um schöner darzustellen: repr = tabulate( table, - headers=['i', 'x[i]', 'y[i] = x[2i]', 'gcd(|x - y|,n)'], + headers=['i', 'x(i)', 'y(i) = x([log₂(i)])', 'gcd(|x - y|,n)'], showindex=False, colalign=('right', 'right', 'right', 'center'), tablefmt='simple', diff --git a/code/python/src/models/pollard_rho/logging.py b/code/python/src/models/pollard_rho/logging.py index 8819c56..0bc4463 100644 --- a/code/python/src/models/pollard_rho/logging.py +++ b/code/python/src/models/pollard_rho/logging.py @@ -22,5 +22,5 @@ __all__ = [ @dataclass class Step(): x: int = field(); - y: int = field(); + y: Optional[int] = field(default=None); d: Optional[int] = field(default=None);