mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-23 01:28:26 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
f610031f9e
commit
b0833eb96b
|
@ -1,12 +1,14 @@
|
||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
def gcd(a, b):
|
def gcd(a, b):
|
||||||
"""Computes the greatest common divisor using Euclidean algorithm."""
|
"""Computes the greatest common divisor using Euclidean algorithm."""
|
||||||
while b:
|
while b:
|
||||||
a, b = b, a % b
|
a, b = b, a % b
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
def modular_exponentiation(base, exp, mod):
|
def modular_exponentiation(base, exp, mod):
|
||||||
"""Computes (base^exp) % mod using fast modular exponentiation."""
|
"""Computes (base^exp) % mod using fast modular exponentiation."""
|
||||||
result = 1
|
result = 1
|
||||||
|
@ -17,6 +19,7 @@ def modular_exponentiation(base, exp, mod):
|
||||||
exp //= 2
|
exp //= 2
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def find_order(a, N):
|
def find_order(a, N):
|
||||||
"""Finds the smallest r such that a^r ≡ 1 (mod N)"""
|
"""Finds the smallest r such that a^r ≡ 1 (mod N)"""
|
||||||
r = 1
|
r = 1
|
||||||
|
@ -26,6 +29,7 @@ def find_order(a, N):
|
||||||
return None
|
return None
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
def shor_algorithm(N):
|
def shor_algorithm(N):
|
||||||
"""Simulates Shor’s Algorithm classically to factorize N."""
|
"""Simulates Shor’s Algorithm classically to factorize N."""
|
||||||
if N % 2 == 0:
|
if N % 2 == 0:
|
||||||
|
@ -49,6 +53,7 @@ def shor_algorithm(N):
|
||||||
if 1 < factor2 < N:
|
if 1 < factor2 < N:
|
||||||
return factor2, N // factor2
|
return factor2, N // factor2
|
||||||
|
|
||||||
|
|
||||||
# Example usage
|
# Example usage
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
N = 15 # You can test with 21, 35, 55, etc.
|
N = 15 # You can test with 21, 35, 55, etc.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user