Fix: deduplicated "is_prime" functions by murilo-goncalves · Pull Request #5488 · TheAlgorithms/Python · GitHub
Skip to content

Fix: deduplicated "is_prime" functions - #5488

Closed
murilo-goncalves wants to merge 1 commit into
TheAlgorithms:masterfrom
murilo-goncalves:fix/duplicated-is_prime-functions
Closed

murilo-goncalves wants to merge 1 commit into
TheAlgorithms:masterfrom
murilo-goncalves:fix/duplicated-is_prime-functions

Conversation

@murilo-goncalves

Copy link
Copy Markdown
Contributor

Unified all functions that check if number is prime to maths/check_prime.py check_prime function, that checks primality in O(sqrt(n)).

Functions left unchanged:

  • Functions that use Sieve of Eratosthenes like in project_euler/problem_037/sol1.py
  • Functions that have return type different from boolean like in project_euler/problem_058/sol1.py
  • Functions that checks primality using specific methods like in maths/miller_rabin.py

Fixes: #5434

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@ghost ghost added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass labels Oct 20, 2021
@murilo-goncalves

Copy link
Copy Markdown
Contributor Author

@srishtik2310

Copy link
Copy Markdown

FAILED project_euler/problem_015/sol1.py::project_euler.problem_015.sol1.solution

but I didn't even change that file :c
Yeah. Same thing happened with me too. I was looking through PRs and found yours.

Screenshot 2021-10-21 at 7 57 28 AM

@poyea

poyea commented Oct 21, 2021

Copy link
Copy Markdown
Member
ERROR maths/primelib.py - ModuleNotFoundError: No module named 'prime_check'

Comment thread maths/prime_check.py
Comment on lines 7 to 11
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.
"""

@poyea poyea Oct 21, 2021

Copy link
Copy Markdown
Member

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

Comment thread maths/primelib.py
isPrime(number)
sieveEr(N)
getPrimeNumbers(N)
primeFactorization(number)

Copy link
Copy Markdown
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

Comment on lines -17 to -36
"""
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.
"""

Copy link
Copy Markdown
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

Comment on lines -22 to -30
>>> is_prime(2)
True
>>> is_prime(15)
False
>>> is_prime(29)
True
>>> is_prime(0)
False
"""

Copy link
Copy Markdown
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

Comment on lines -20 to -26
>>> isprime(2)
True
>>> isprime(15)
False
>>> isprime(29)
True
"""

Copy link
Copy Markdown
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

Comment on lines -21 to -29
>>> is_prime(2)
True
>>> is_prime(3)
True
>>> is_prime(27)
False
>>> is_prime(2999)
True
"""

Copy link
Copy Markdown
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

Comment on lines -22 to -30
>>> is_prime(2)
True
>>> is_prime(3)
True
>>> is_prime(27)
False
>>> is_prime(2999)
True
"""

Copy link
Copy Markdown
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

Comment on lines -23 to -29
>>> is_prime(67483)
False
>>> is_prime(563)
True
>>> is_prime(87)
False
"""

Copy link
Copy Markdown
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

@poyea

poyea commented Oct 21, 2021

Copy link
Copy Markdown
Member

It's probably from . import prime_check

@poyea

poyea commented Oct 21, 2021

Copy link
Copy Markdown
Member

In Python 3.10

math.factorial(x)
    Return x factorial as an integer. Raises ValueError if x is not integral or is negative.

so it's causing errors: https://github.com/TheAlgorithms/Python/runs/3958162685?check_suite_focus=true#step:3:5

@poyea

poyea commented Oct 21, 2021

Copy link
Copy Markdown
Member

Please see and wait for #5496.

@murilo-goncalves

Copy link
Copy Markdown
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deduplicate repeated is_prime functions

3 participants