Skip to content

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion maths/prime_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def prime_check(number: int) -> bool:
"""Checks to see if a number is a prime.
"""Checks to see if a number is a prime in O(sqrt(n)).

A number is prime if it has exactly two factors: 1 and itself.
"""
Comment on lines 7 to 11
Copy link
Member

@poyea poyea Oct 21, 2021

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).

Expand Down
57 changes: 11 additions & 46 deletions maths/primelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

Overview:

isPrime(number)
sieveEr(N)
getPrimeNumbers(N)
primeFactorization(number)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change these signatures too, e.g. sieve_of_eratosthenes

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
42 changes: 5 additions & 37 deletions project_euler/problem_003/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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:
Expand Down Expand Up @@ -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

Expand Down
32 changes: 3 additions & 29 deletions project_euler/problem_007/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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:
Expand All @@ -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

Expand Down
20 changes: 2 additions & 18 deletions project_euler/problem_007/sol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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:
Expand Down Expand Up @@ -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:
Expand Down
25 changes: 2 additions & 23 deletions project_euler/problem_010/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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:
Expand All @@ -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
Expand Down
22 changes: 2 additions & 20 deletions project_euler/problem_010/sol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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]:
Expand All @@ -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

Expand Down
Loading