-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add Solovay-Strassen Primality test #10335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
""" | ||
This script implements the Solovay-Strassen Primality test. | ||
|
||
This probabilistic primality test is based on Euler's criterion. It is similar | ||
to the Fermat test but uses quadratic residues. It can quickly identify | ||
composite numbers but may occasionally classify composite numbers as prime. | ||
|
||
More details and concepts about this can be found on: | ||
https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test | ||
""" | ||
|
||
|
||
import random | ||
|
||
|
||
def jacobi_symbol(random_a: int, number: int) -> int: | ||
""" | ||
Calculate the Jacobi symbol. The Jacobi symbol is a mathematical function | ||
used to determine whether an integer is a quadratic residue modulo | ||
another integer (usually prime) or not. | ||
|
||
Parameters: | ||
random_a (int): A randomly chosen integer from 2 to n-2 (inclusive) | ||
number (int): The number that is tested for primality | ||
|
||
Returns: | ||
jocobi_symbol (int): The jacobi symbol | ||
|
||
>>> jacobi_symbol(2, 13) | ||
-1 | ||
>>> jacobi_symbol(5, 19) | ||
1 | ||
>>> jacobi_symbol(7, 14) | ||
0 | ||
""" | ||
|
||
if random_a == 0: | ||
return 0 | ||
if random_a == 1: | ||
return 1 | ||
saahil-mahato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
random_a = random_a % number | ||
saahil-mahato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t = 1 | ||
|
||
while random_a != 0: | ||
while random_a % 2 == 0: | ||
random_a = random_a // 2 | ||
saahil-mahato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r = number % 8 | ||
if r in (3, 5): | ||
t = -t | ||
|
||
random_a, number = number, random_a | ||
|
||
if random_a % 4 == number % 4 == 3: | ||
t = -t | ||
|
||
random_a = random_a % number | ||
saahil-mahato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if number == 1: | ||
return t | ||
else: | ||
return 0 | ||
saahil-mahato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def solovay_strassen(number: int, accuracy: int) -> bool: | ||
saahil-mahato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Check whether the input number is prime or not using | ||
the Solovay-Strassen Primality test | ||
|
||
Parameters: | ||
number (int): The number that is tested for primality | ||
accuracy (int): The accuracy for the test | ||
|
||
Returns: | ||
result (bool): True if number is probably prime and false | ||
if composite | ||
|
||
>>> solovay_strassen(13, 5) | ||
True | ||
>>> solovay_strassen(9, 10) | ||
False | ||
>>> solovay_strassen(17, 15) | ||
True | ||
saahil-mahato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
if number <= 1: | ||
return False | ||
if number <= 3: | ||
return True | ||
|
||
for _ in range(accuracy): | ||
a = random.randint(2, number - 2) | ||
x = jacobi_symbol(a, number) | ||
y = pow(a, (number - 1) // 2, number) | ||
|
||
if x == 0 or y != x % number: | ||
return False | ||
|
||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.