master > master: code py - pollard rho mit 2 modi
This commit is contained in:
@@ -12,5 +12,6 @@ from src.algorithms.pollard_rho.algorithms import *;
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'pollard_rho_algorithm',
|
||||
'pollard_rho_algorithm_linear',
|
||||
'pollard_rho_algorithm_exponential',
|
||||
];
|
||||
|
||||
@@ -18,14 +18,71 @@ from src.algorithms.pollard_rho.display import *;
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'pollard_rho_algorithm',
|
||||
'pollard_rho_algorithm_linear',
|
||||
'pollard_rho_algorithm_exponential',
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHOD pollard's rho algorithm
|
||||
# METHOD pollard's rho algorithm - with linear grwoth
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def pollard_rho_algorithm(
|
||||
def pollard_rho_algorithm_linear(
|
||||
n: int,
|
||||
x_init: int = 2,
|
||||
verbose: bool = False,
|
||||
):
|
||||
steps = [];
|
||||
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);
|
||||
# aktualisiere y: y = f(f(y_prev)):
|
||||
y = f(f(y));
|
||||
|
||||
# ggT berechnen:
|
||||
d = math.gcd(abs(x-y), n);
|
||||
steps.append(Step(x=x, y=y, d=d));
|
||||
|
||||
# Abbruchkriterien prüfen:
|
||||
if d == 1: # weitermachen, solange d == 1
|
||||
k += 1;
|
||||
continue;
|
||||
elif d == n: # versagt
|
||||
success = False;
|
||||
break;
|
||||
else:
|
||||
success = True;
|
||||
break;
|
||||
|
||||
if verbose:
|
||||
repr = display_table_linear(steps=steps);
|
||||
print('');
|
||||
print('\x1b[1mEuklidescher Algorithmus\x1b[0m');
|
||||
print('');
|
||||
print(repr);
|
||||
print('');
|
||||
if success:
|
||||
print('\x1b[1mBerechneter Faktor:\x1b[0m');
|
||||
print('');
|
||||
print(f'd = \x1b[1m{d}\x1b[0m.');
|
||||
else:
|
||||
print('\x1b[91mKein (Prim)faktor erkannt!\x1b[0m');
|
||||
print('');
|
||||
return d;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHOD pollard's rho algorithm - with exponential grwoth
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def pollard_rho_algorithm_exponential(
|
||||
n: int,
|
||||
x_init: int = 2,
|
||||
verbose: bool = False,
|
||||
@@ -64,7 +121,7 @@ def pollard_rho_algorithm(
|
||||
break;
|
||||
|
||||
if verbose:
|
||||
repr = display_table(steps=steps);
|
||||
repr = display_table_exponential(steps=steps);
|
||||
print('');
|
||||
print('\x1b[1mEuklidescher Algorithmus\x1b[0m');
|
||||
print('');
|
||||
|
||||
@@ -16,14 +16,37 @@ from src.models.pollard_rho import *;
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'display_table',
|
||||
'display_table_linear',
|
||||
'display_table_exponential',
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHOD display table
|
||||
# METHOD display table - linear
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def display_table(steps: List[Step]) -> str:
|
||||
def display_table_linear(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 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)'],
|
||||
showindex=False,
|
||||
colalign=('right', 'right', 'right', 'center'),
|
||||
tablefmt='simple',
|
||||
);
|
||||
return repr;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHOD display table - exponential
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def display_table_exponential(steps: List[Step]) -> str:
|
||||
table = pd.DataFrame({
|
||||
'i': [i for i in range(len(steps))],
|
||||
'x': [step.x for step in steps],
|
||||
|
||||
Reference in New Issue
Block a user