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