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)
| 62 | |
| 63 | |
| 64 | def 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 | |
| 103 | if __name__ == "__main__": |
nothing calls this directly
no test coverage detected