0

I can't seem to figure out a way to include some multiples into my non prime list. for examples "15" "49" "9" is considered a prime number. Any ideas or corrections ?

number = int(input("Enter a number. "))
non_prime = []
prime = []


def isPrime(number):
    for i in range(2,number):
        if number % i == 0:  
            return False
        else:
           return True

def append(number):
    for i in range(1,number+1):
        if isPrime(i) == True:
            prime.append(i)
        else:
            non_prime.append(i)


append(number)
print(non_prime)
print(prime)
print (len(prime))
Anthony
  • 23
  • 2
  • 1
    Your `isPrime()` function needs to be revised. It's only separating odd numbers from even numbers – Mangohero1 Oct 20 '17 at 21:12
  • 1
    It's pretty easy to find algorithms to find primes if you search. This might help https://stackoverflow.com/questions/18833759/python-prime-number-checker#18833870 – klutt Oct 20 '17 at 21:14
  • Your `isPrime()` function is returning `True` too soon. You need to investigate more numbers, and once no divisor has been found, you exit the loop and `return True`. – Jochem Schulenklopper Oct 20 '17 at 21:23
  • Also, you don't need to investigate all possible divisors between `2` and `number`. Looping until the square root of `number` is sufficient... if a divisor hasn't been found then, then it will not be found anymore. – Jochem Schulenklopper Oct 20 '17 at 21:24

1 Answers1

3
if number % i == 0:  
   return False
else:
   return True

Your function returns true for all odd numbers, since it checks if they can be divided by 2 in the 'if' clause, and since they cannot be divided by 2, isPrime returns True. You can do the following:

def isPrime(number):
    for i in range(2,number):
        if number % i == 0:  
            return False
    return True

If a number can exit the for loop, it cannot be divided by any number up to it, so it is a prime number.

gokhansim
  • 123
  • 6