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 [2]:
j = 0 # add check counter outside the for-loop so it doesn't get reset
for num in range(1,101): 
    prime = True 
    for i in range(2,num): 
        if (num%i==0): 
            prime = False 
    if prime: 
        if j%2 == 0: # test the check counter for being even and if so, then print the number
            print num
        j += 1 # increment the check counter each time a prime is found


1
3
7
13
19
29
37
43
53
61
71
79
89

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


In [12]:
j = 0 
for num in range(1,101): 
    prime = True 
    for i in range(2,num): 
        if (num%i==0): 
            prime = False
            continue 
            # once the number has already been shown to be false, 
            # there's no reason to keep checking
    if prime: 
        if j%2 == 0: 
            print num
        j += 1


1
3
7
13
19
29
37
43
53
61
71
79
89

In [12]:


In [ ]: