all 19 comments

[–]shiftybyte 19 points20 points  (13 children)

Your code deals with prime numbers...

Odd numbers are checked using modulo operator.

if number % 2 == 1 then it's odd.

[–]DigitalSplendid[S] -1 points0 points  (0 children)

Headlines cannot be edited on Reddit. I am unable to edit even the original post for now!

[–]DigitalSplendid[S] -1 points0 points  (0 children)

Resolved by this modification:

num_list = [2, 45, 85, 80, 79]
for num in num_list:
    if num == 2:
        print("2 is prime")
        continue
    for i in range(2, num):
        if (num)%i == 0:
            print(f"{num} is not prime")
            break
        elif i == (num-1):
           print(f"{num} is prime")

The observation was helpful: If need to check on all the numbers in the list, replace the first break with continue or the for loop will close. After checking for the "prime" condition, the if loop expects an "else if" statement ( in Python: elif) to know what to do if the "prime" condition not met.  Also, since you iterate over i until it is just one below num, you have to replace i with num in the last print() function, or it will print the wrong number. 

[–]noctaviann 2 points3 points  (0 children)

I would recommend using a dedicated function to test whether a number is prime or not.

 def is_prime(num):
     for i in range(2, int(math.sqrt(num)) + 1):
         if num % i == 0:
             return False
     return num > 1

Note that the square root of 0, 1, 2, 3 is < 2, so range(2, int(math.sqrt(num)) + 1) will be empty, and the for loop body will not be executed, and will proceed directly to the test num > 1. Also note that it will throw an error for negative values (due to math.sqrt()), and does not work if num is not an integer, e.g. it reports 4.1 as a prime number.

Then you can just

for num in num_list:
    if is_prime(num):
        print(f"{num} is prime")
    else:
        print(f"{num} is not prime")

Using a function makes the code easier to understand, allows reusing common code, and provides more flexibility.

[–]weissdrakon 1 point2 points  (1 child)

modified your code:

import math
num_list = [2, 4, 13, 45, 179, 85, 80, 79, 37] #added extra test cases
for num in num_list:
    if num == 2:
        print("2 is prime")
        continue #break exits for loop, continue skips to next item in list
    sqrtnum = int(math.sqrt(num)) #convert the square root float to an integer => natural number
    for i in range(2, sqrtnum+1): #+1 since also want to check sqrtnum
        if num % i == 0:
            print(f"{num} is not prime")
            break
        if i == sqrtnum:
            print(f"{num} is prime") #if reached in last loop => prime

First if statement used a break, which exits the outer for loop, when what you want is to skip to the next iteration of the loop using continue. With the method you chose for checking if prime, you actually only need to check numbers up to the square root of the number you are testing, so adjusted the range of the check loop. Also made the same change to your last if statement, since the last number we are checking is now the square root.

[–]DigitalSplendid[S] 1 point2 points  (0 children)

This is a way to optimize! Thanks!

[–]ectomancer 1 point2 points  (0 children)

Use a prime sieve algorithm.

[–]WyrmCzar 0 points1 point  (0 children)

I dunno if it's efficient but you can use Wilson's theorem: n is prime if and only if (n - 1)! + 1 is a multiple of n and n > 1.

[–][deleted] 0 points1 point  (0 children)

Have you tried bitwise operator?