In [1]:
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 [9]:
# Modified Code
prime_numbers = list()
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
prime_numbers.append(num)
len_prime = len(prime_numbers) - 1
for i in range(0,len_prime):
if i % 2 == 0:
print(prime_numbers[i])
In [12]:
# Improvement: I am not doing all the calculations for numbers that can be devided by two.
for num in range(1,101):
prime = True
if num == 2:
pass
elif num % 2 == 0:
prime = False
else:
for i in range(3,num):
if (num%i==0):
prime = False
if prime:
print(num)
In [ ]: