diff --git a/code/python/src/algorithms/euklid/algorithms.py b/code/python/src/algorithms/euklid/algorithms.py index d853809..5befa63 100644 --- a/code/python/src/algorithms/euklid/algorithms.py +++ b/code/python/src/algorithms/euklid/algorithms.py @@ -94,5 +94,4 @@ def euklidean_algorithm( print(f'a=\x1b[1m{step.a}\x1b[0m; b=\x1b[1m{step.b}\x1b[0m; d = \x1b[1m{step.gcd}\x1b[0m = {expr}.'); print(''); - return d, coeff_a, coeff_b; diff --git a/code/python/src/algorithms/pollard_rho/algorithms.py b/code/python/src/algorithms/pollard_rho/algorithms.py index 4b1f56a..0067ca7 100644 --- a/code/python/src/algorithms/pollard_rho/algorithms.py +++ b/code/python/src/algorithms/pollard_rho/algorithms.py @@ -10,8 +10,8 @@ from src.thirdparty.maths import *; from models.generated.config import *; from src.core.utils import *; -# from src.models.pollard_rho import *; -from src.algorithms.euklid.display import *; +from src.models.pollard_rho import *; +from src.algorithms.pollard_rho.display import *; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # EXPORTS @@ -27,6 +27,48 @@ __all__ = [ def pollard_rho_algorithm( n: int, + x_init: int = 2, verbose: bool = False, ): - return; + d = 1; + x = y = x_init; + steps = []; + steps.append(Step(x=x, y=y)); + success = False; + f = lambda _: fct(_, n=n); + while True: + x = f(x); + y = f(f(y)); + d = math.gcd(abs(x-y), n); + steps.append(Step(x=x, y=y, d=d)); + if d == 1: + continue; + elif d < n: + success = True; + break; + else: + success = False; + break; + + if verbose: + repr = display_table(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; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# AUXILIARY METHOD function for Pollard's rho +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +def fct(x: int, n: int) -> int: + return (x**2 - 1) % n; diff --git a/code/python/src/algorithms/pollard_rho/display.py b/code/python/src/algorithms/pollard_rho/display.py index e69de29..7528211 100644 --- a/code/python/src/algorithms/pollard_rho/display.py +++ b/code/python/src/algorithms/pollard_rho/display.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +from src.thirdparty.code import *; +from src.thirdparty.maths import *; +from src.thirdparty.types import *; + +from src.models.pollard_rho import *; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 'display_table', +]; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# METHOD display table +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +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], + '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; diff --git a/code/python/src/endpoints/ep_algorithm_pollard_rho.py b/code/python/src/endpoints/ep_algorithm_pollard_rho.py index eba2cc4..2d23363 100644 --- a/code/python/src/endpoints/ep_algorithm_pollard_rho.py +++ b/code/python/src/endpoints/ep_algorithm_pollard_rho.py @@ -28,6 +28,7 @@ __all__ = [ def endpoint_pollard_rho(command: CommandPollard) -> Result[CallResult, CallError]: result = pollard_rho_algorithm( n = command.number, + x_init = command.x_init, verbose = config.OPTIONS.tsp.verbose, ); return Ok(CallResult(action_taken=True, message=result)); diff --git a/code/python/src/models/pollard_rho/__init__.py b/code/python/src/models/pollard_rho/__init__.py new file mode 100644 index 0000000..c914672 --- /dev/null +++ b/code/python/src/models/pollard_rho/__init__.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +from src.models.pollard_rho.logging import *; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 'Step', +]; diff --git a/code/python/src/models/pollard_rho/logging.py b/code/python/src/models/pollard_rho/logging.py new file mode 100644 index 0000000..8819c56 --- /dev/null +++ b/code/python/src/models/pollard_rho/logging.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +from src.thirdparty.types import *; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 'Step', +]; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# CLASS Step +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +@dataclass +class Step(): + x: int = field(); + y: int = field(); + d: Optional[int] = field(default=None);