Lesson 5: Listing up all prime numbers using for loop

Post date: Nov 2, 2017 3:00:05 PM

------------------------------------------------------------------------------------------------

Developed version in class

------------------------------------------------------------------------------------------------

total = 0

for x in range(2,100):

prime = True

for y in range(2,x):

if x % y == 0:

prime = False

if prime == True:

print (x, "is a prime number")

total = total + 1

print("There are", total, "number of prime number")

------------------------------------------------------------------------------------------------

Optimized version post-class

------------------------------------------------------------------------------------------------

total = 0

iterations = 0

upper = 100000

primelist = []

for x in range(2,upper):

prime = True

for y in primelist:

iterations += 1

if x % y == 0:

prime = False

break

if prime == True:

total += 1

primelist.append(x)

print("There are", total, "prime numbers till", upper)

print("I've have found them in ", iterations, "interatitons")

print("They are ", primelist)