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 [ ]:
for num in range(1,101): # for-loop through the numbers
    prime = True # boolean flag to check the number for being prime
    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
        print(num)

In [1]:
for num in range(1,101):
    prime = True
    for i in range(2, num):
        if (num%i==0):
            prime = False
    if prime:
        print(num)


1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

In [12]:
for num in range(1,101):
    prime = True
    for i in range(2, num):
        if (num%i==0):
            prime = False
    if prime:
        print(num)
return num[1::2]


1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
  File "<ipython-input-12-2f4904ca7125>", line 8
    return num[1::2]
                    ^
SyntaxError: 'return' outside function

In [14]:
for num in range(1,101):
    prime = True
    for i in range(2, num):
        if (num%i==0):
            prime = False
    if prime:
        print(num)
    def altElement(a):
        return a[::2]


1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

In [2]:
prime_num = 0

for num in range(1,101):
    prime = True
    for i in range(2,num): 
        if (num%i==0): 
            prime = False 
    if prime: 
        prime_num = prime_num+1
        if (prime_num%2==0):
            print(num)
        else:
            pass


2
5
11
17
23
31
41
47
59
67
73
83
97

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


In [ ]: