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

Fix: deduplicated "is_prime" functions #5488

wants to merge 1 commit into from

Conversation

murilo-goncalves
Copy link
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
Contributor Author

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

but I didn't even change that file :c

@srishtik2310
Copy link

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

poyea commented Oct 21, 2021

ERROR maths/primelib.py - ModuleNotFoundError: No module named 'prime_check'

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

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

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

poyea commented Oct 21, 2021

It's probably from . import prime_check

@poyea
Copy link
Member

poyea commented Oct 21, 2021

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

poyea commented Oct 21, 2021

Please see and wait for #5496.

@murilo-goncalves
Copy link
Contributor Author

I will close this PR for now, wait for #5496 and make separate PRs for the changes discussed. Thanks for the feedback!

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