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

Remove duplicate is_prime related functions #5892

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -25,7 +25,7 @@ def rabinMiller(num: int) -> bool:
return True


def isPrime(num: int) -> bool:
def low_num_is_prime(num: int) -> bool:
if num < 2:
return False

@@ -213,11 +213,11 @@ def isPrime(num: int) -> bool:
def generateLargePrime(keysize: int = 1024) -> int:
while True:
num = random.randrange(2 ** (keysize - 1), 2 ** (keysize))
if isPrime(num):
if low_num_is_prime(num):
return num


if __name__ == "__main__":
num = generateLargePrime()
print(("Prime number:", num))
print(("isPrime:", isPrime(num)))
print(("low_num_is_prime:", low_num_is_prime(num)))
@@ -1,16 +1,16 @@
import random

from .binary_exp_mod import bin_exp_mod
from binary_exp_mod import bin_exp_mod


# This is a probabilistic check to test primality, useful for big numbers!
# if it's a prime, it will return true
# if it's not a prime, the chance of it returning true is at most 1/4**prec
def is_prime(n, prec=1000):
def big_num_is_prime(n, prec=1000):
"""
>>> from .prime_check import prime_check
>>> # all(is_prime(i) == prime_check(i) for i in range(1000)) # 3.45s
>>> all(is_prime(i) == prime_check(i) for i in range(256))
>>> from maths.prime_check import prime_check
>>> # all(big_num_is_prime(i) == prime_check(i) for i in range(1000)) # 3.45s
>>> all(big_num_is_prime(i) == prime_check(i) for i in range(256))
True
"""
if n < 2:
@@ -48,4 +48,4 @@ def is_prime(n, prec=1000):
if __name__ == "__main__":
n = abs(int(input("Enter bound : ").strip()))
print("Here's the list of primes:")
print(", ".join(str(i) for i in range(n + 1) if is_prime(i)))
print(", ".join(str(i) for i in range(n + 1) if big_num_is_prime(i)))
Remove duplicate is_prime related functions by paulosgf · Pull Request #5892 · TheAlgorithms/Python · GitHub
24 201