MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solovay_strassen

Function solovay_strassen

maths/solovay_strassen_primality_test.py:64–100  ·  view source on GitHub ↗

Check whether the input number is prime or not using the Solovay-Strassen Primality test Parameters: number: The number that is tested for primality iterations: The number of times that the test is run which effects the accuracy Returns: result: Tru

(number: int, iterations: int)

Source from the content-addressed store, hash-verified

62
63
64def solovay_strassen(number: int, iterations: int) -> bool:
65 """
66 Check whether the input number is prime or not using
67 the Solovay-Strassen Primality test
68
69 Parameters:
70 number: The number that is tested for primality
71 iterations: The number of times that the test is run
72 which effects the accuracy
73
74 Returns:
75 result: True if number is probably prime and false
76 if not
77
78 >>> random.seed(10)
79 >>> solovay_strassen(13, 5)
80 True
81 >>> solovay_strassen(9, 10)
82 False
83 >>> solovay_strassen(17, 15)
84 True
85 """
86
87 if number <= 1:
88 return False
89 if number <= 3:
90 return True
91
92 for _ in range(iterations):
93 a = random.randint(2, number - 2)
94 x = jacobi_symbol(a, number)
95 y = pow(a, (number - 1) // 2, number)
96
97 if x == 0 or y != x % number:
98 return False
99
100 return True
101
102
103if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

jacobi_symbolFunction · 0.85

Tested by

no test coverage detected