-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshor_algorithm.py
More file actions
30 lines (24 loc) · 858 Bytes
/
Copy pathshor_algorithm.py
File metadata and controls
30 lines (24 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import math
import argparse
def shor_algorithm(n: int):
for a in range(1, n):
r = 0
while r < n:
if math.pow(a, r) % n == 1:
result = [a, r]
num1 = int(math.pow(result[0], result[1] // 2) - 1)
num2 = int(math.pow(result[0], result[1] // 2) + 1)
p = math.gcd(num1, n)
q = math.gcd(num2, n)
if p != n and q != n:
return [p, q]
r = r + 2
return []
def main():
parser = argparse.ArgumentParser(description='Simulate Shor\'s algorithm for N.')
parser.add_argument('n', type=int, help='The integer to factor')
args = parser.parse_args()
p, q = shor_algorithm(args.n)
print("Factor P is: " + str(p) + " and " + "Factor Q is: " + str(q))
if __name__ == "__main__":
main()