From 7d07f4317ef7f5ee0e854bf9a6615b8b33042b7c Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Thu, 30 Jun 2022 06:24:10 +0200 Subject: [PATCH] =?UTF-8?q?woche12=20>=20master:=20code=20py=20-=20pollard?= =?UTF-8?q?s=20rho=20mit=20log-wachstum=20f=C3=BCr=20y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/algorithms/pollard_rho/algorithms.py | 29 ++++++++++++++----- .../src/algorithms/pollard_rho/display.py | 4 +-- code/python/src/models/pollard_rho/logging.py | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) 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);