Remove duplicate is_prime related functions (#5892)

* Fixes (#5434)

* Update ciphers.rabin_miller.py
         maths.miller_rabin.py

* Fixing ERROR maths/miller_rabin.py - ModuleNotFoundError and changing project_euler's isPrime to is_prime function names

* Update sol1.py

* fix: try to change to list

* fix pre-commit

* fix capital letters

* Update miller_rabin.py

* Update rabin_miller.py

Co-authored-by: John Law <johnlaw.po@gmail.com>
This commit is contained in:
Paulo S. G. Ferraz 2022-04-08 14:40:45 -03:00 committed by GitHub
parent 1d3d18bcd2
commit 1400cb86ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 38 deletions

View File

@ -25,7 +25,7 @@ def rabinMiller(num: int) -> bool:
return True return True
def isPrime(num: int) -> bool: def is_prime_low_num(num: int) -> bool:
if num < 2: if num < 2:
return False return False
@ -213,11 +213,11 @@ def isPrime(num: int) -> bool:
def generateLargePrime(keysize: int = 1024) -> int: def generateLargePrime(keysize: int = 1024) -> int:
while True: while True:
num = random.randrange(2 ** (keysize - 1), 2 ** (keysize)) num = random.randrange(2 ** (keysize - 1), 2 ** (keysize))
if isPrime(num): if is_prime_low_num(num):
return num return num
if __name__ == "__main__": if __name__ == "__main__":
num = generateLargePrime() num = generateLargePrime()
print(("Prime number:", num)) print(("Prime number:", num))
print(("isPrime:", isPrime(num))) print(("is_prime_low_num:", is_prime_low_num(num)))

View File

@ -6,11 +6,11 @@ from .binary_exp_mod import bin_exp_mod
# This is a probabilistic check to test primality, useful for big numbers! # This is a probabilistic check to test primality, useful for big numbers!
# if it's a prime, it will return true # 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 # 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 is_prime_big(n, prec=1000):
""" """
>>> from .prime_check import prime_check >>> from maths.prime_check import prime_check
>>> # all(is_prime(i) == prime_check(i) for i in range(1000)) # 3.45s >>> # all(is_prime_big(i) == prime_check(i) for i in range(1000)) # 3.45s
>>> all(is_prime(i) == prime_check(i) for i in range(256)) >>> all(is_prime_big(i) == prime_check(i) for i in range(256))
True True
""" """
if n < 2: if n < 2:
@ -48,4 +48,4 @@ def is_prime(n, prec=1000):
if __name__ == "__main__": if __name__ == "__main__":
n = abs(int(input("Enter bound : ").strip())) n = abs(int(input("Enter bound : ").strip()))
print("Here's the list of primes:") 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 is_prime_big(i)))

View File

@ -13,23 +13,23 @@ References:
import math import math
def isprime(num: int) -> bool: def is_prime(num: int) -> bool:
""" """
Returns boolean representing primality of given number num. Returns boolean representing primality of given number num.
>>> isprime(2) >>> is_prime(2)
True True
>>> isprime(3) >>> is_prime(3)
True True
>>> isprime(27) >>> is_prime(27)
False False
>>> isprime(2999) >>> is_prime(2999)
True True
>>> isprime(0) >>> is_prime(0)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: Parameter num must be greater than or equal to two. ValueError: Parameter num must be greater than or equal to two.
>>> isprime(1) >>> is_prime(1)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: Parameter num must be greater than or equal to two. ValueError: Parameter num must be greater than or equal to two.
@ -84,18 +84,18 @@ def solution(n: int = 600851475143) -> int:
if n <= 0: if n <= 0:
raise ValueError("Parameter n must be greater than or equal to one.") raise ValueError("Parameter n must be greater than or equal to one.")
max_number = 0 max_number = 0
if isprime(n): if is_prime(n):
return n return n
while n % 2 == 0: while n % 2 == 0:
n //= 2 n //= 2
if isprime(n): if is_prime(n):
return n return n
for i in range(3, int(math.sqrt(n)) + 1, 2): for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0: if n % i == 0:
if isprime(n // i): if is_prime(n // i):
max_number = n // i max_number = n // i
break break
elif isprime(i): elif is_prime(i):
max_number = i max_number = i
return max_number return max_number

View File

@ -13,15 +13,15 @@ References:
""" """
def isprime(number: int) -> bool: def is_prime(number: int) -> bool:
""" """
Determines whether the given number is prime or not Determines whether the given number is prime or not
>>> isprime(2) >>> is_prime(2)
True True
>>> isprime(15) >>> is_prime(15)
False False
>>> isprime(29) >>> is_prime(29)
True True
""" """
@ -76,7 +76,7 @@ def solution(nth: int = 10001) -> int:
primes: list[int] = [] primes: list[int] = []
num = 2 num = 2
while len(primes) < nth: while len(primes) < nth:
if isprime(num): if is_prime(num):
primes.append(num) primes.append(num)
num += 1 num += 1
else: else:

View File

@ -15,15 +15,15 @@ import itertools
import math import math
def prime_check(number: int) -> bool: def is_prime(number: int) -> bool:
""" """
Determines whether a given number is prime or not Determines whether a given number is prime or not
>>> prime_check(2) >>> is_prime(2)
True True
>>> prime_check(15) >>> is_prime(15)
False False
>>> prime_check(29) >>> is_prime(29)
True True
""" """
@ -39,7 +39,7 @@ def prime_generator():
num = 2 num = 2
while True: while True:
if prime_check(num): if is_prime(num):
yield num yield num
num += 1 num += 1

View File

@ -36,14 +36,14 @@ count of current primes.
from math import isqrt from math import isqrt
def isprime(number: int) -> int: def is_prime(number: int) -> int:
""" """
returns whether the given number is prime or not Returns whether the given number is prime or not
>>> isprime(1) >>> is_prime(1)
0 0
>>> isprime(17) >>> is_prime(17)
1 1
>>> isprime(10000) >>> is_prime(10000)
0 0
""" """
if number == 1: if number == 1:
@ -60,7 +60,7 @@ def isprime(number: int) -> int:
def solution(ratio: float = 0.1) -> int: def solution(ratio: float = 0.1) -> int:
""" """
returns the side length of the square spiral of odd length greater Returns the side length of the square spiral of odd length greater
than 1 for which the ratio of primes along both diagonals than 1 for which the ratio of primes along both diagonals
first falls below the given ratio. first falls below the given ratio.
>>> solution(.5) >>> solution(.5)
@ -76,9 +76,8 @@ def solution(ratio: float = 0.1) -> int:
while primes / (2 * j - 1) >= ratio: while primes / (2 * j - 1) >= ratio:
for i in range(j * j + j + 1, (j + 2) * (j + 2), j + 1): for i in range(j * j + j + 1, (j + 2) * (j + 2), j + 1):
primes = primes + isprime(i) primes += is_prime(i)
j += 2
j = j + 2
return j return j