Handled type hinds

This commit is contained in:
Joelkurien 2024-10-29 21:36:23 +11:00
commit ebf66fb78c
2 changed files with 18 additions and 19 deletions

View File

@ -1192,6 +1192,7 @@
## Quantum
* [Q Fourier Transform](quantum/q_fourier_transform.py)
* [Shor Algorithm](quantum/shor_algorithm.py)
## Scheduling
* [First Come First Served](scheduling/first_come_first_served.py)

View File

@ -13,6 +13,8 @@ idea actually works.
Website referred for shor algorithm:
https://www.geeksforgeeks.org/shors-factorization-algorithm/
"""
class Shor:
def period_find(self, num: int, number: int) -> int:
"""
@ -24,7 +26,7 @@ class Shor:
>>> shor.period_find(3, 7)
6
"""
start:int = 1
start: int = 1
while pow(num, start, number) != 1:
start += 1
return start
@ -40,28 +42,24 @@ class Shor:
>>> factors
(3, 5)
"""
if number%2 == 0:
return 2, number//2
if number % 2 == 0:
return 2, number // 2
while True:
random.seed(0)
num:int = random.randint(2, number-1)
gcd_number_num:int = math.gcd(number, num)
num: int = random.randint(2, number - 1)
gcd_number_num: int = math.gcd(number, num)
if gcd_number_num > 1:
return gcd_number_num, number//gcd_number_num
return gcd_number_num, number // gcd_number_num
result:int = self.period_find(num, number)
if not result%2:
start:int = pow(num, result//2, number)
if start != number-1:
p_value:int = math.gcd(start-1, number)
q_value:int = math.gcd(start+1, number)
result: int = self.period_find(num, number)
if not result % 2:
start: int = pow(num, result // 2, number)
if start != number - 1:
p_value: int = math.gcd(start - 1, number)
q_value: int = math.gcd(start + 1, number)
if p_value > 1 and q_value > 1:
return p_value, q_value
shor = Shor()
print(shor.shor_algorithm(15))