Skip to content

Commit 1400cb8

Browse files
paulosgfpoyea
andauthored
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 <[email protected]>
1 parent 1d3d18b commit 1400cb8

File tree

6 files changed

+37
-38
lines changed

6 files changed

+37
-38
lines changed

Diff for: ciphers/rabin_miller.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def rabinMiller(num: int) -> bool:
2525
return True
2626

2727

28-
def isPrime(num: int) -> bool:
28+
def is_prime_low_num(num: int) -> bool:
2929
if num < 2:
3030
return False
3131

@@ -213,11 +213,11 @@ def isPrime(num: int) -> bool:
213213
def generateLargePrime(keysize: int = 1024) -> int:
214214
while True:
215215
num = random.randrange(2 ** (keysize - 1), 2 ** (keysize))
216-
if isPrime(num):
216+
if is_prime_low_num(num):
217217
return num
218218

219219

220220
if __name__ == "__main__":
221221
num = generateLargePrime()
222222
print(("Prime number:", num))
223-
print(("isPrime:", isPrime(num)))
223+
print(("is_prime_low_num:", is_prime_low_num(num)))

Diff for: maths/miller_rabin.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
# This is a probabilistic check to test primality, useful for big numbers!
77
# if it's a prime, it will return true
88
# if it's not a prime, the chance of it returning true is at most 1/4**prec
9-
def is_prime(n, prec=1000):
9+
def is_prime_big(n, prec=1000):
1010
"""
11-
>>> from .prime_check import prime_check
12-
>>> # all(is_prime(i) == prime_check(i) for i in range(1000)) # 3.45s
13-
>>> all(is_prime(i) == prime_check(i) for i in range(256))
11+
>>> from maths.prime_check import prime_check
12+
>>> # all(is_prime_big(i) == prime_check(i) for i in range(1000)) # 3.45s
13+
>>> all(is_prime_big(i) == prime_check(i) for i in range(256))
1414
True
1515
"""
1616
if n < 2:
@@ -48,4 +48,4 @@ def is_prime(n, prec=1000):
4848
if __name__ == "__main__":
4949
n = abs(int(input("Enter bound : ").strip()))
5050
print("Here's the list of primes:")
51-
print(", ".join(str(i) for i in range(n + 1) if is_prime(i)))
51+
print(", ".join(str(i) for i in range(n + 1) if is_prime_big(i)))

Diff for: project_euler/problem_003/sol1.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
import math
1414

1515

16-
def isprime(num: int) -> bool:
16+
def is_prime(num: int) -> bool:
1717
"""
1818
Returns boolean representing primality of given number num.
1919
20-
>>> isprime(2)
20+
>>> is_prime(2)
2121
True
22-
>>> isprime(3)
22+
>>> is_prime(3)
2323
True
24-
>>> isprime(27)
24+
>>> is_prime(27)
2525
False
26-
>>> isprime(2999)
26+
>>> is_prime(2999)
2727
True
28-
>>> isprime(0)
28+
>>> is_prime(0)
2929
Traceback (most recent call last):
3030
...
3131
ValueError: Parameter num must be greater than or equal to two.
32-
>>> isprime(1)
32+
>>> is_prime(1)
3333
Traceback (most recent call last):
3434
...
3535
ValueError: Parameter num must be greater than or equal to two.
@@ -84,18 +84,18 @@ def solution(n: int = 600851475143) -> int:
8484
if n <= 0:
8585
raise ValueError("Parameter n must be greater than or equal to one.")
8686
max_number = 0
87-
if isprime(n):
87+
if is_prime(n):
8888
return n
8989
while n % 2 == 0:
9090
n //= 2
91-
if isprime(n):
91+
if is_prime(n):
9292
return n
9393
for i in range(3, int(math.sqrt(n)) + 1, 2):
9494
if n % i == 0:
95-
if isprime(n // i):
95+
if is_prime(n // i):
9696
max_number = n // i
9797
break
98-
elif isprime(i):
98+
elif is_prime(i):
9999
max_number = i
100100
return max_number
101101

Diff for: project_euler/problem_007/sol2.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
"""
1414

1515

16-
def isprime(number: int) -> bool:
16+
def is_prime(number: int) -> bool:
1717
"""
1818
Determines whether the given number is prime or not
1919
20-
>>> isprime(2)
20+
>>> is_prime(2)
2121
True
22-
>>> isprime(15)
22+
>>> is_prime(15)
2323
False
24-
>>> isprime(29)
24+
>>> is_prime(29)
2525
True
2626
"""
2727

@@ -76,7 +76,7 @@ def solution(nth: int = 10001) -> int:
7676
primes: list[int] = []
7777
num = 2
7878
while len(primes) < nth:
79-
if isprime(num):
79+
if is_prime(num):
8080
primes.append(num)
8181
num += 1
8282
else:

Diff for: project_euler/problem_007/sol3.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
import math
1616

1717

18-
def prime_check(number: int) -> bool:
18+
def is_prime(number: int) -> bool:
1919
"""
2020
Determines whether a given number is prime or not
2121
22-
>>> prime_check(2)
22+
>>> is_prime(2)
2323
True
24-
>>> prime_check(15)
24+
>>> is_prime(15)
2525
False
26-
>>> prime_check(29)
26+
>>> is_prime(29)
2727
True
2828
"""
2929

@@ -39,7 +39,7 @@ def prime_generator():
3939

4040
num = 2
4141
while True:
42-
if prime_check(num):
42+
if is_prime(num):
4343
yield num
4444
num += 1
4545

Diff for: project_euler/problem_058/sol1.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
from math import isqrt
3737

3838

39-
def isprime(number: int) -> int:
39+
def is_prime(number: int) -> int:
4040
"""
41-
returns whether the given number is prime or not
42-
>>> isprime(1)
41+
Returns whether the given number is prime or not
42+
>>> is_prime(1)
4343
0
44-
>>> isprime(17)
44+
>>> is_prime(17)
4545
1
46-
>>> isprime(10000)
46+
>>> is_prime(10000)
4747
0
4848
"""
4949
if number == 1:
@@ -60,7 +60,7 @@ def isprime(number: int) -> int:
6060

6161
def solution(ratio: float = 0.1) -> int:
6262
"""
63-
returns the side length of the square spiral of odd length greater
63+
Returns the side length of the square spiral of odd length greater
6464
than 1 for which the ratio of primes along both diagonals
6565
first falls below the given ratio.
6666
>>> solution(.5)
@@ -76,9 +76,8 @@ def solution(ratio: float = 0.1) -> int:
7676

7777
while primes / (2 * j - 1) >= ratio:
7878
for i in range(j * j + j + 1, (j + 2) * (j + 2), j + 1):
79-
primes = primes + isprime(i)
80-
81-
j = j + 2
79+
primes += is_prime(i)
80+
j += 2
8281
return j
8382

8483

0 commit comments

Comments
 (0)