-2

I'm writing a prime number program and I'd like a hint, it currently prints primes except for numbers divisible by 3 and 5 in starting at 9 and 15, but somehow accurately computes 20, also a multiple of 5, as not prime. Any help appreciated. Using python 2 because I'm on an old Vaio with no net connection.

def main():
    nums = [2]
    dividend = 3

    while i < 30:
        if dividend % nuns[i] > 0:
                print divided, 'is prime'
                nums.append(dividend)
                dividend = dividend + 1
       if dividend % nums[i] == 0:
                print divided, 'is not prime'
                nums.append(dividend)
                dividend = dividend + 1
       i = i + 1
main()
CT Hildreth
  • 205
  • 1
  • 2
  • 8
  • 4
    Please show your actual code and describe what parts of it are not working as you expect. – BrenBarn Dec 30 '13 at 20:07
  • 1
    From your edit: I know it sucks, but really, there's no way we're going to be able to find the bug in your code if we can't see it. – abarnert Dec 30 '13 at 20:08
  • 1
    It sounds to me like you're updating a list while looping over that list in a for loop. Don't do that. – Iguananaut Dec 30 '13 at 20:08
  • This code has a number of bugs that stop it from running and it's difficult to figure out what you are trying to do. If you have code that runs then please post that. Or explain how this is supposed to work. – Stuart Dec 31 '13 at 02:40
  • I did make a typo, 'nun' instead of 'num', but the way I want it to run is to divide a number n by every preceding number n - 1, then test for remainders using the modulo operator to divine a list of primes. As the code is, it spits out primes except some numbers divisible by 3 and 5. Thanks. – CT Hildreth Dec 31 '13 at 03:17
  • The only primes divisible by 3 and 5 are 3 and 5. Primes are only divisible by one and themselves. So if a number that isn't 3 or 5 is divisible by 3 or 5 it isn't prime – kylieCatt Dec 31 '13 at 03:40
  • And therein lies my logic problem – CT Hildreth Dec 31 '13 at 03:44

2 Answers2

2

Your code doesn't work because you're only checking whether dividend is divisible by the i-th number in nums. You need to check whether it is divisible by any of the previous primes. (There are other problems in the logic of your code - it is checking for a prime twice in each loop, and this happens to make it seem like it might be half-working, when in fact it's not working at all...)

To do this, you need to use a nested loop - the Python tutorial shows how to solve exactly this problem with a nested for loop.

Alternatively, use the any function with a list comprehension to replace the inner loop.

def get_primes(max):
    """ Prints all prime numbers between 3 and max """
    primes = [2]
    for n in range(3, max + 1):
        if any(n % p == 0 for p in primes):
            print n, "is not prime"
        else:
            print n, "is prime"  
            primes.append(n)

get_primes(30)
Stuart
  • 9,597
  • 1
  • 21
  • 30
1
def primes(num):
    if num == 2:
        print("prime")
        return True
        more code here...

I'm assuming you want the first even to get 2 as a prime. Don't bother with modulo just check if the input is 2 then write the code to check the rest.

EDIT: The following code will find all the primes in it's given range and append them to a list named primes. It's in Pyhton 3 but the only thing you have to change is the print statements:

primes = []

def is_prime(num):
    if num == 1:
        return False
    if num == 2:
        return True
    for i in range(2, num):
        if num % i == 0:
            return False
    return True

for i in range(1, 31):
    if is_prime(i):
        print(str(i) + " is prime.")
        primes.append(i)
    else:
        print(str(i) + " is not prime.")

print("primes = " str(primes))

Output:

1 is not prime.
2 is prime.
3 is prime.
4 is not prime.
5 is prime.
6 is not prime.
7 is prime.
8 is not prime.
9 is not prime.
10 is not prime.
11 is prime.
12 is not prime.
13 is prime.
14 is not prime.
15 is not prime.
16 is not prime.
17 is prime.
18 is not prime.
19 is prime.
20 is not prime.
21 is not prime.
22 is not prime.
23 is prime.
24 is not prime.
25 is not prime.
26 is not prime.
27 is not prime.
28 is not prime.
29 is prime.
30 is not prime.
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
kylieCatt
  • 10,672
  • 5
  • 43
  • 51
  • 1
    You can reduce your main loop to `primes = filter(is_prime, range(1, 31))` – Burhan Khalid Dec 31 '13 at 04:36
  • @BurhanKhalid Thanks for the tip! I'm just learning myself and haven't had much experience with `filter()` yet but thanks for showing me that. Looks to be pretty useful. – kylieCatt Dec 31 '13 at 04:42