Skip to content

Remove duplicate is_prime related functions #5892

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 9 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ciphers/rabin_miller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)))
10 changes: 5 additions & 5 deletions maths/miller_rabin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
# 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:
Expand Down Expand Up @@ -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)))
22 changes: 11 additions & 11 deletions project_euler/problem_003/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
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.
Expand Down Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions project_euler/problem_007/sol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"""


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
"""

Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions project_euler/problem_007/sol3.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
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
"""

Expand All @@ -39,7 +39,7 @@ def prime_generator():

num = 2
while True:
if prime_check(num):
if is_prime(num):
yield num
num += 1

Expand Down
17 changes: 8 additions & 9 deletions project_euler/problem_058/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
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:
Expand All @@ -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)
Expand All @@ -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


Expand Down