The following code will print the prime numbers between 1 and 100. Modify the code so it prints every other prime number from 1 to 100


In [20]:
count = 0
prime = True
for num in range(1,101): # for-loop through the numbers
    prime = True
    for i in range(2,num): # for-loop to check for "primeness" by checking for divisors other than 1
        if (num%i==0): # logical test for the number having a divisor other than 1 and itself
            prime = False # if there's a divisor, the boolean value gets flipped to False
    if prime: # if prime is still True after going through all numbers from 1 - 100, then it gets printed
        count += 1
        if count%2 == 0:
            print("Skipped prim number is:", num)


Skipped prim number is: 2
Skipped prim number is: 5
Skipped prim number is: 11
Skipped prim number is: 17
Skipped prim number is: 23
Skipped prim number is: 31
Skipped prim number is: 41
Skipped prim number is: 47
Skipped prim number is: 59
Skipped prim number is: 67
Skipped prim number is: 73
Skipped prim number is: 83
Skipped prim number is: 97

Extra Credit: Can you write a procedure that runs faster than the one above?


In [19]:
#CLEARLY NOT

count = 0
prime = True
for num in range(1,101): # for-loop through the numbers
    prime = True
    for i in range(2,num) if (num%i==0)
       prime = False 
    if prime:
        count += 1
        if count%2 == 0:
            print("Skipped prim number is:", num)


  File "<ipython-input-19-94afbc3b712a>", line 7
    for i in range(2,num) if (num%i==0)
                                       ^
SyntaxError: invalid syntax

In [ ]: