def count_primes(num):
primes = [2]
x = 3
if num < 2: # for the case of num = 0 or 1
return 0
while x <= num:
for y in range(3,x,2): # test all odd factors up to x-1
if x%y == 0:
x += 2
break
else:
primes.append(x)
x += 2
print(primes)
return len(primes)
#without break
count_primes(75)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79]
22
Hey guys, a little confused as if i miss out the break
statement in the for loop and I test count_primes(75), because I don't include the break, it also includes 79 in the list of primes even though it was specified while x<=num
. Why does this happen? What is the full purpose of the break statement in this code?