Skip to content
Snippets Groups Projects
Commit 8a9a4702 authored by adrian.spycher's avatar adrian.spycher
Browse files

complete algo.py

parent 3fe94d7c
No related branches found
No related tags found
No related merge requests found
import math
def bachet_bezout(a, b):
"""Does the Bachet-Bezout algorithm on a of b
Args:
a (uint): the number
b (uint): the modulo
Returns:
uint: An array with the pgdc and the coefficients of bachet_bezout, [pgdc, u, v]
"""
if (a == 0):
return 0
if (b == 0):
return -1
u = [1, 0]
v = [0, 1]
q = 1
r = 1
i = 2
while (r > 0):
q = a // b
r = a % b
u.append(u[i-2] - q * u[i-1])
v.append(v[i-2] - q * v[i-1])
a = b
b = r
i += 1
return a, u[i-2], v[i-2]
def exponentiation_rapide(a, exp, n):
"""Does the quick explanation of a pow x modulo n
Args:
a (uint): the number
exp (uint): the exponent of the number
n (uint): the modulo
Returns:
uint: An array with the pgdc and the coefficients of bachet_bezout, [pgdc, u, v]
"""
if (a == 0):
return 0
if (exp == 0):
return 1
r = 1
b = a % n
while (exp > 0):
y = exp % 2
r = (r * b**y) % n
b = (b**2) % n
exp = exp // 2
return r
def is_square(a):
"""Check if a number is a perfect square, using the Newton methode
from https://stackoverflow.com/questions/2489435/check-if-a-number-is-a-perfect-square
Args:
a (uint): number checked
Returns:
bool: true if the number is a perfect square, otherwise flase
"""
x = a // 2
seen = set([x])
while x * x != a:
x = (x + (a // x)) // 2
if x in seen:
return False
seen.add(x)
return True
def fermat_factorization(n):
"""Does the Fermat's factorization on n,
n = a² - b² = (a + b) * (a - b) = p * q <=> b² = a² - n
Args:
n (uint): number used
Returns:
tuple of uint: the two coeficient a and b
"""
a = math.ceil(math.sqrt(n))
b = 0
while(True):
b2 = a**2 - n
if (is_square(b2)):
b = math.sqrt(b2)
break
a += 1
return (a, b)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment