woche12 > master: code py - pollards rho mit log-wachstum für y
This commit is contained in:
parent
2bd07544f3
commit
7d07f4317e
@ -30,24 +30,37 @@ def pollard_rho_algorithm(
|
|||||||
x_init: int = 2,
|
x_init: int = 2,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
):
|
):
|
||||||
d = 1;
|
|
||||||
x = y = x_init;
|
|
||||||
steps = [];
|
steps = [];
|
||||||
steps.append(Step(x=x, y=y));
|
|
||||||
success = False;
|
success = False;
|
||||||
f = lambda _: fct(_, n=n);
|
f = lambda _: fct(_, n=n);
|
||||||
|
|
||||||
|
d = 1;
|
||||||
|
x = y = x_init;
|
||||||
|
steps.append(Step(x=x));
|
||||||
|
k = 0;
|
||||||
|
k_next = 1;
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
# aktualisiere x: x = f(x_prev):
|
||||||
x = f(x);
|
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);
|
d = math.gcd(abs(x-y), n);
|
||||||
steps.append(Step(x=x, y=y, d=d));
|
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;
|
continue;
|
||||||
elif d < n:
|
elif d == n: # versagt
|
||||||
success = True;
|
success = False;
|
||||||
break;
|
break;
|
||||||
else:
|
else:
|
||||||
success = False;
|
success = True;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
|
@ -27,14 +27,14 @@ def display_table(steps: List[Step]) -> str:
|
|||||||
table = pd.DataFrame({
|
table = pd.DataFrame({
|
||||||
'i': [i for i in range(len(steps))],
|
'i': [i for i in range(len(steps))],
|
||||||
'x': [step.x for step in 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],
|
'd': [step.d or '-' for step in steps],
|
||||||
}) \
|
}) \
|
||||||
.reset_index(drop=True);
|
.reset_index(drop=True);
|
||||||
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
||||||
repr = tabulate(
|
repr = tabulate(
|
||||||
table,
|
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,
|
showindex=False,
|
||||||
colalign=('right', 'right', 'right', 'center'),
|
colalign=('right', 'right', 'right', 'center'),
|
||||||
tablefmt='simple',
|
tablefmt='simple',
|
||||||
|
@ -22,5 +22,5 @@ __all__ = [
|
|||||||
@dataclass
|
@dataclass
|
||||||
class Step():
|
class Step():
|
||||||
x: int = field();
|
x: int = field();
|
||||||
y: int = field();
|
y: Optional[int] = field(default=None);
|
||||||
d: Optional[int] = field(default=None);
|
d: Optional[int] = field(default=None);
|
||||||
|
Loading…
Reference in New Issue
Block a user