From 1400cb86ff7c656087963db41844e0ca503ae6d5 Mon Sep 17 00:00:00 2001 From: "Paulo S. G. Ferraz" Date: Fri, 8 Apr 2022 14:40:45 -0300 Subject: [PATCH] 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 --- ciphers/rabin_miller.py | 6 +++--- maths/miller_rabin.py | 10 +++++----- project_euler/problem_003/sol1.py | 22 +++++++++++----------- project_euler/problem_007/sol2.py | 10 +++++----- project_euler/problem_007/sol3.py | 10 +++++----- project_euler/problem_058/sol1.py | 17 ++++++++--------- 6 files changed, 37 insertions(+), 38 deletions(-) diff --git a/ciphers/rabin_miller.py b/ciphers/rabin_miller.py index c42ad2f59..a9b834bfb 100644 --- a/ciphers/rabin_miller.py +++ b/ciphers/rabin_miller.py @@ -25,7 +25,7 @@ def rabinMiller(num: int) -> bool: return True -def isPrime(num: int) -> bool: +def is_prime_low_num(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 is_prime_low_num(num): return num if __name__ == "__main__": num = generateLargePrime() print(("Prime number:", num)) - print(("isPrime:", isPrime(num))) + print(("is_prime_low_num:", is_prime_low_num(num))) diff --git a/maths/miller_rabin.py b/maths/miller_rabin.py index 2b0944508..d35e54858 100644 --- a/maths/miller_rabin.py +++ b/maths/miller_rabin.py @@ -6,11 +6,11 @@ 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 is_prime_big(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(is_prime_big(i) == prime_check(i) for i in range(1000)) # 3.45s + >>> all(is_prime_big(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 is_prime_big(i))) diff --git a/project_euler/problem_003/sol1.py b/project_euler/problem_003/sol1.py index 1f3299842..606a6945e 100644 --- a/project_euler/problem_003/sol1.py +++ b/project_euler/problem_003/sol1.py @@ -13,23 +13,23 @@ References: import math -def isprime(num: int) -> bool: +def is_prime(num: int) -> bool: """ Returns boolean representing primality of given number num. - >>> isprime(2) + >>> is_prime(2) True - >>> isprime(3) + >>> is_prime(3) True - >>> isprime(27) + >>> is_prime(27) False - >>> isprime(2999) + >>> is_prime(2999) True - >>> isprime(0) + >>> is_prime(0) Traceback (most recent call last): ... ValueError: Parameter num must be greater than or equal to two. - >>> isprime(1) + >>> is_prime(1) Traceback (most recent call last): ... ValueError: Parameter num must be greater than or equal to two. @@ -84,18 +84,18 @@ def solution(n: int = 600851475143) -> int: if n <= 0: raise ValueError("Parameter n must be greater than or equal to one.") max_number = 0 - if isprime(n): + if is_prime(n): return n while n % 2 == 0: n //= 2 - if isprime(n): + if is_prime(n): return n for i in range(3, int(math.sqrt(n)) + 1, 2): if n % i == 0: - if isprime(n // i): + if is_prime(n // i): max_number = n // i break - elif isprime(i): + elif is_prime(i): max_number = i return max_number diff --git a/project_euler/problem_007/sol2.py b/project_euler/problem_007/sol2.py index dfcad8c14..44d72e949 100644 --- a/project_euler/problem_007/sol2.py +++ b/project_euler/problem_007/sol2.py @@ -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 - >>> isprime(2) + >>> is_prime(2) True - >>> isprime(15) + >>> is_prime(15) False - >>> isprime(29) + >>> is_prime(29) True """ @@ -76,7 +76,7 @@ def solution(nth: int = 10001) -> int: primes: list[int] = [] num = 2 while len(primes) < nth: - if isprime(num): + if is_prime(num): primes.append(num) num += 1 else: diff --git a/project_euler/problem_007/sol3.py b/project_euler/problem_007/sol3.py index 7911fa3e9..daa719cef 100644 --- a/project_euler/problem_007/sol3.py +++ b/project_euler/problem_007/sol3.py @@ -15,15 +15,15 @@ import itertools import math -def prime_check(number: int) -> bool: +def is_prime(number: int) -> bool: """ Determines whether a given number is prime or not - >>> prime_check(2) + >>> is_prime(2) True - >>> prime_check(15) + >>> is_prime(15) False - >>> prime_check(29) + >>> is_prime(29) True """ @@ -39,7 +39,7 @@ def prime_generator(): num = 2 while True: - if prime_check(num): + if is_prime(num): yield num num += 1 diff --git a/project_euler/problem_058/sol1.py b/project_euler/problem_058/sol1.py index ed407edf7..c59b0dd71 100644 --- a/project_euler/problem_058/sol1.py +++ b/project_euler/problem_058/sol1.py @@ -36,14 +36,14 @@ count of current primes. from math import isqrt -def isprime(number: int) -> int: +def is_prime(number: int) -> int: """ - returns whether the given number is prime or not - >>> isprime(1) + Returns whether the given number is prime or not + >>> is_prime(1) 0 - >>> isprime(17) + >>> is_prime(17) 1 - >>> isprime(10000) + >>> is_prime(10000) 0 """ if number == 1: @@ -60,7 +60,7 @@ def isprime(number: int) -> 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 first falls below the given ratio. >>> solution(.5) @@ -76,9 +76,8 @@ def solution(ratio: float = 0.1) -> int: while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1, (j + 2) * (j + 2), j + 1): - primes = primes + isprime(i) - - j = j + 2 + primes += is_prime(i) + j += 2 return j