Python: Deduplicate repeated `is_prime` functions

We have a lot of is_prime (or similar) functions: https://github.com/TheAlgorithms/Python/search?p=4&q=is_prime, https://github.com/TheAlgorithms/Python/search?q=isPrime, data_structures/hashing/number_theory/prime_numbers, etc. Shall we use one common function for that exactly identical is_prime calculation (which takes O(sqrt(n)))?

Candidates include:

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 46 (22 by maintainers)

Commits related to this issue

Most upvoted comments

Can you assign this to me.

I will do it!

@elpaxoudis That sounds good to me 😃 - unless there’s some weird specifications in the problem / custom implementation (say the author wrote a Sieve-like approach) which I’m sure it’s rare. Doctests / test cases are always welcome!

Hey @elpaxoudis! For 1 & 2, yes, and these can be done altogether in this issue. It would be of help if you could check also other algorithms and files in this repository which define a is_prime function or alike.

For 3, I agree that one of them is a duplicate. Let’s handle it in a different issue (or without an issue because it’s straightforward in terms of scale of change). We can merge all the test cases / comments, while preserving one clearer version.

is_prime function to check whether a given number is prime using O(sqrt N) algorithm

def is_prime(n):

    try:
        n = int(n)
    except:
        print("Not an integer input")
        print("Sorry ! Prime Number checking can be done only on integers")
        return
    
    if n <= 1:
        return False

    for i in range(2,int(n**0.5)+1):
        if n%i == 0:
            return False
    return True

I wanted to learn Python, but it seems to be a difficult language.

@poyea, I have other question: some problems in projecteuler folder seens to be resolved already. Some require a function like is_prime(), to determine if it’s prime or not and this function is ready to work. Shouldn’t they be empty? See project_euler/problem_010/sol1.py

I would say yes, but this may be our second priority because project_euler is a folder of solutions, and we may want them to be self-contained, in some sense. The goal is to replace those repetitively appeared is_prime in other main algorithm files. And make it clear enough for others to use it first. Maybe we can make a list of these is_prime instances first and decide whether we should change them (at all).

I’ll do it.

@poyea,

This is a list from Project Euler with isprime() like functions on his solutions:

project_euler.problem_007.sol1.is_prime()
project_euler.problem_010.sol1.is_prime()
project_euler.problem_010.sol2.is_prime()
project_euler.problem_027.sol1.is_prime()
project_euler.problem_035.sol1.is_prime()
project_euler.problem_041.sol1.is_prime()
project_euler.problem_046.sol1.is_prime()
project_euler.problem_049.sol1.is_prime()
project_euler.problem_003.sol1.isPrime()
project_euler.problem_007.sol2.isprime()
project_euler.problem_058.sol1.isPrime()
project_euler.problem_007.sol3.prime_check()

It’s to register for later decision.

Can I try solve this prob?

Hi! I’m interested in working on this