Skip to content

Commit b0833eb

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent f610031 commit b0833eb

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

algorithms/cryptography/shor_algorithm.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import math
22
import random
33

4+
45
def gcd(a, b):
56
"""Computes the greatest common divisor using Euclidean algorithm."""
67
while b:
78
a, b = b, a % b
89
return a
910

11+
1012
def modular_exponentiation(base, exp, mod):
1113
"""Computes (base^exp) % mod using fast modular exponentiation."""
1214
result = 1
@@ -17,6 +19,7 @@ def modular_exponentiation(base, exp, mod):
1719
exp //= 2
1820
return result
1921

22+
2023
def find_order(a, N):
2124
"""Finds the smallest r such that a^r ≡ 1 (mod N)"""
2225
r = 1
@@ -26,6 +29,7 @@ def find_order(a, N):
2629
return None
2730
return r
2831

32+
2933
def shor_algorithm(N):
3034
"""Simulates Shor’s Algorithm classically to factorize N."""
3135
if N % 2 == 0:
@@ -49,6 +53,7 @@ def shor_algorithm(N):
4953
if 1 < factor2 < N:
5054
return factor2, N // factor2
5155

56+
5257
# Example usage
5358
if __name__ == "__main__":
5459
N = 15 # You can test with 21, 35, 55, etc.

0 commit comments

Comments
 (0)