HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Prime number calculator in Python 3.4.1

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberprimecalculatorpython

Problem

I have only been coding for a short time and I have written these two functions and am looking to optimise them. If anyone could point out things that could be done better it would be much appreciated.
Please Note:

This code has been reviewed/improved using the suggestions below, take a look:

# Python 3.4.1
# Function to check if a number is prime

# Takes integer, returns the factors
def findFactors(num):
    # Create variable to hold the factors and add 1 and itself (all numbers have these factors)
    factors = [1, num]

    # For each possible factor 
    for i in range(2, int(num/4) + 3):
        # Check that it is a factor and that the factor and its corresponding factor are not already in the list
        if num % i == 0 and i not in factors and int(num/i) not in factors:
            # Add it and its corresponding factor to the list
            factors.append(i)
            factors.append(int(num/i))

    return factors

# Takes an integer, returns true or false
def isPrime(number):
    number = int(number)

    # Check if the only factors are 1 and itself and it is greater than 1
    if len(findFactors(number)) == 2 and number > 1:
        return True
    
    return False

Solution

Style

Python has a style guide called PEP 8 which is definitly worth reading and and worth following if you do not have good reasons not to. In you case, your function name for instance is not compliant to PEP8. You'll find tools online to check your code compliancy to PEP8 in a automated way if you want to.

Docstrings

There are Python docstring conventions you can find in PEP 257. Instead of

# Takes an integer, returns true or false
def isPrime(number):


You could write :

def isPrime(number):
    """Return whether an integer is prime."""


Please note: the imperative tone, the triple-quote strings under the def line, the ending period.

Do less

If your function isPrime takes an integer as a parameter, you do not need number = int(number).

Also, you can rewrite :

# Check if the only factors are 1 and itself and it is greater than 1
if len(findFactors(number)) == 2 and number > 1:
    return True

return False


as

# Check if the only factors are 1 and itself and it is greater than 1
return len(findFactors(number)) == 2 and number > 1


Different algorithm

You do not really need to find the list of all factors to know if the number is prime. You can stop as soon as a factor is found.

Code Snippets

# Takes an integer, returns true or false
def isPrime(number):
def isPrime(number):
    """Return whether an integer is prime."""
# Check if the only factors are 1 and itself and it is greater than 1
if len(findFactors(number)) == 2 and number > 1:
    return True

return False
# Check if the only factors are 1 and itself and it is greater than 1
return len(findFactors(number)) == 2 and number > 1

Context

StackExchange Code Review Q#133077, answer score: 3

Revisions (0)

No revisions yet.