woche12 > master: code py - pollards rho mit log-wachstum für y

This commit is contained in:
RD 2022-06-30 06:24:10 +02:00
parent 2bd07544f3
commit 7d07f4317e
3 changed files with 24 additions and 11 deletions

View File

@ -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:

View File

@ -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',

View File

@ -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);