-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Fix: deduplicated "is_prime" functions #5488
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
|
||
Overview: | ||
|
||
isPrime(number) | ||
sieveEr(N) | ||
getPrimeNumbers(N) | ||
primeFactorization(number) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change these signatures too, e.g. |
||
|
@@ -38,41 +37,7 @@ | |
|
||
""" | ||
|
||
from math import sqrt | ||
|
||
|
||
def isPrime(number): | ||
""" | ||
input: positive integer 'number' | ||
returns true if 'number' is prime otherwise false. | ||
""" | ||
|
||
# precondition | ||
assert isinstance(number, int) and ( | ||
number >= 0 | ||
), "'number' must been an int and positive" | ||
|
||
status = True | ||
|
||
# 0 and 1 are none primes. | ||
if number <= 1: | ||
status = False | ||
|
||
for divisor in range(2, int(round(sqrt(number))) + 1): | ||
|
||
# if 'number' divisible by 'divisor' then sets 'status' | ||
# of false and break up the loop. | ||
if number % divisor == 0: | ||
status = False | ||
break | ||
|
||
# precondition | ||
assert isinstance(status, bool), "'status' must been from type bool" | ||
|
||
return status | ||
|
||
|
||
# ------------------------------------------ | ||
from prime_check import prime_check | ||
|
||
|
||
def sieveEr(N): | ||
|
@@ -129,7 +94,7 @@ def getPrimeNumbers(N): | |
# if a number is prime then appends to list 'ans' | ||
for number in range(2, N + 1): | ||
|
||
if isPrime(number): | ||
if prime_check(number): | ||
|
||
ans.append(number) | ||
|
||
|
@@ -164,11 +129,11 @@ def primeFactorization(number): | |
ans.append(number) | ||
|
||
# if 'number' not prime then builds the prime factorization of 'number' | ||
elif not isPrime(number): | ||
elif not prime_check(number): | ||
|
||
while quotient != 1: | ||
|
||
if isPrime(factor) and (quotient % factor == 0): | ||
if prime_check(factor) and (quotient % factor == 0): | ||
ans.append(factor) | ||
quotient /= factor | ||
else: | ||
|
@@ -317,8 +282,8 @@ def goldbach(number): | |
isinstance(ans, list) | ||
and (len(ans) == 2) | ||
and (ans[0] + ans[1] == number) | ||
and isPrime(ans[0]) | ||
and isPrime(ans[1]) | ||
and prime_check(ans[0]) | ||
and prime_check(ans[1]) | ||
), "'ans' must contains two primes. And sum of elements must been eq 'number'" | ||
|
||
return ans | ||
|
@@ -462,11 +427,11 @@ def getPrime(n): | |
|
||
# if ans not prime then | ||
# runs to the next prime number. | ||
while not isPrime(ans): | ||
while not prime_check(ans): | ||
ans += 1 | ||
|
||
# precondition | ||
assert isinstance(ans, int) and isPrime( | ||
assert isinstance(ans, int) and prime_check( | ||
ans | ||
), "'ans' must been a prime number and from type int" | ||
|
||
|
@@ -486,7 +451,7 @@ def getPrimesBetween(pNumber1, pNumber2): | |
|
||
# precondition | ||
assert ( | ||
isPrime(pNumber1) and isPrime(pNumber2) and (pNumber1 < pNumber2) | ||
prime_check(pNumber1) and prime_check(pNumber2) and (pNumber1 < pNumber2) | ||
), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" | ||
|
||
number = pNumber1 + 1 # jump to the next number | ||
|
@@ -495,7 +460,7 @@ def getPrimesBetween(pNumber1, pNumber2): | |
|
||
# if number is not prime then | ||
# fetch the next prime number. | ||
while not isPrime(number): | ||
while not prime_check(number): | ||
number += 1 | ||
|
||
while number < pNumber2: | ||
|
@@ -505,7 +470,7 @@ def getPrimesBetween(pNumber1, pNumber2): | |
number += 1 | ||
|
||
# fetch the next prime number. | ||
while not isPrime(number): | ||
while not prime_check(number): | ||
number += 1 | ||
|
||
# precondition | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,39 +12,7 @@ | |
""" | ||
import math | ||
|
||
|
||
def isprime(num: int) -> bool: | ||
""" | ||
Returns boolean representing primality of given number num. | ||
|
||
>>> isprime(2) | ||
True | ||
>>> isprime(3) | ||
True | ||
>>> isprime(27) | ||
False | ||
>>> isprime(2999) | ||
True | ||
>>> isprime(0) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Parameter num must be greater than or equal to two. | ||
>>> isprime(1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Parameter num must be greater than or equal to two. | ||
""" | ||
Comment on lines
-17
to
-36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just copy-paste these to the prime check |
||
|
||
if num <= 1: | ||
raise ValueError("Parameter num must be greater than or equal to two.") | ||
if num == 2: | ||
return True | ||
elif num % 2 == 0: | ||
return False | ||
for i in range(3, int(math.sqrt(num)) + 1, 2): | ||
if num % i == 0: | ||
return False | ||
return True | ||
from maths.prime_check import prime_check | ||
|
||
|
||
def solution(n: int = 600851475143) -> int: | ||
|
@@ -84,18 +52,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 prime_check(n): | ||
return n | ||
while n % 2 == 0: | ||
n //= 2 | ||
if isprime(n): | ||
if prime_check(n): | ||
return n | ||
for i in range(3, int(math.sqrt(n)) + 1, 2): | ||
if n % i == 0: | ||
if isprime(n // i): | ||
if prime_check(n // i): | ||
max_number = n // i | ||
break | ||
elif isprime(i): | ||
elif prime_check(i): | ||
max_number = i | ||
return max_number | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,33 +12,7 @@ | |
- https://en.wikipedia.org/wiki/Prime_number | ||
""" | ||
|
||
from math import sqrt | ||
|
||
|
||
def is_prime(num: int) -> bool: | ||
""" | ||
Determines whether the given number is prime or not | ||
|
||
>>> is_prime(2) | ||
True | ||
>>> is_prime(15) | ||
False | ||
>>> is_prime(29) | ||
True | ||
>>> is_prime(0) | ||
False | ||
""" | ||
Comment on lines
-22
to
-30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be copy-pasted |
||
|
||
if num == 2: | ||
return True | ||
elif num % 2 == 0: | ||
return False | ||
else: | ||
sq = int(sqrt(num)) + 1 | ||
for i in range(3, sq, 2): | ||
if num % i == 0: | ||
return False | ||
return True | ||
from maths.prime_check import prime_check | ||
|
||
|
||
def solution(nth: int = 10001) -> int: | ||
|
@@ -63,11 +37,11 @@ def solution(nth: int = 10001) -> int: | |
number = 1 | ||
while count != nth and number < 3: | ||
number += 1 | ||
if is_prime(number): | ||
if prime_check(number): | ||
count += 1 | ||
while count != nth: | ||
number += 2 | ||
if is_prime(number): | ||
if prime_check(number): | ||
count += 1 | ||
return number | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,23 +12,7 @@ | |
- https://en.wikipedia.org/wiki/Prime_number | ||
""" | ||
|
||
|
||
def isprime(number: int) -> bool: | ||
""" | ||
Determines whether the given number is prime or not | ||
|
||
>>> isprime(2) | ||
True | ||
>>> isprime(15) | ||
False | ||
>>> isprime(29) | ||
True | ||
""" | ||
Comment on lines
-20
to
-26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be copy-pasted |
||
|
||
for i in range(2, int(number ** 0.5) + 1): | ||
if number % i == 0: | ||
return False | ||
return True | ||
from maths.prime_check import prime_check | ||
|
||
|
||
def solution(nth: int = 10001) -> int: | ||
|
@@ -76,7 +60,7 @@ def solution(nth: int = 10001) -> int: | |
primes: list[int] = [] | ||
num = 2 | ||
while len(primes) < nth: | ||
if isprime(num): | ||
if prime_check(num): | ||
primes.append(num) | ||
num += 1 | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,28 +11,7 @@ | |
- https://en.wikipedia.org/wiki/Prime_number | ||
""" | ||
|
||
from math import sqrt | ||
|
||
|
||
def is_prime(n: int) -> bool: | ||
""" | ||
Returns boolean representing primality of given number num. | ||
|
||
>>> is_prime(2) | ||
True | ||
>>> is_prime(3) | ||
True | ||
>>> is_prime(27) | ||
False | ||
>>> is_prime(2999) | ||
True | ||
""" | ||
Comment on lines
-21
to
-29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be copy-pasted |
||
|
||
for i in range(2, int(sqrt(n)) + 1): | ||
if n % i == 0: | ||
return False | ||
|
||
return True | ||
from maths.prime_check import prime_check | ||
|
||
|
||
def solution(n: int = 2000000) -> int: | ||
|
@@ -55,7 +34,7 @@ def solution(n: int = 2000000) -> int: | |
return 0 | ||
|
||
for i in range(3, n, 2): | ||
if is_prime(i): | ||
if prime_check(i): | ||
sum_of_primes += i | ||
|
||
return sum_of_primes | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,28 +10,10 @@ | |
References: | ||
- https://en.wikipedia.org/wiki/Prime_number | ||
""" | ||
import math | ||
from itertools import takewhile | ||
from typing import Iterator | ||
|
||
|
||
def is_prime(number: int) -> bool: | ||
""" | ||
Returns boolean representing primality of given number num. | ||
|
||
>>> is_prime(2) | ||
True | ||
>>> is_prime(3) | ||
True | ||
>>> is_prime(27) | ||
False | ||
>>> is_prime(2999) | ||
True | ||
""" | ||
Comment on lines
-22
to
-30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be copy-pasted |
||
|
||
if number % 2 == 0 and number > 2: | ||
return False | ||
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) | ||
from maths.prime_check import prime_check | ||
|
||
|
||
def prime_generator() -> Iterator[int]: | ||
|
@@ -41,7 +23,7 @@ def prime_generator() -> Iterator[int]: | |
|
||
num = 2 | ||
while True: | ||
if is_prime(num): | ||
if prime_check(num): | ||
yield num | ||
num += 1 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to add a few tests to this function (although we have tests down there, we can still add a few doctests).