0
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?

SH96
  • 1
  • 1
  • 2
    Sorry, but where is the `break` missing from? – quamrana Nov 08 '20 at 17:47
  • It should be here @quamrana: `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` – SH96 Nov 08 '20 at 17:51
  • Perhaps you could update the question with: `# break` where the `break` used to be? – quamrana Nov 08 '20 at 17:52
  • I think your indentation is wrong. `for-else` makes more sense there (especially with the `break`) than `if-else`. – VPfB Nov 08 '20 at 17:58
  • @VPfB yes sorry just corrected! Can you explain why it's for-else here? – SH96 Nov 08 '20 at 18:02
  • @SH96 That has been asked before, please read: https://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops – VPfB Nov 08 '20 at 18:42

1 Answers1

0

The reason you need the break is that without it, the for loop keeps going and may find other factors, each time executing x += 2. The break stops the loop at the first factor found. Perhaps your code should be like this:

def count_primes(num):

    if num < 2:  # for the case of num = 0 or 1
        return 0

    primes = [2]
    x = 3

    while x <= num:
        for y in range(3, x, 2):  # test all odd factors up to x-1
            if x % y == 0:
                break
        else:
            primes.append(x)
        x += 2
    print(primes)
    return len(primes)
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Its because eventually `x` becomes `75` and the `for` loop finds `y=3` divides into `75` and does `x += 2` *and then continues the loop* to find `y=7` divides into `77` and does `x+=2` *and then continues the loop* only this time to find `79` has no factors and quits with `x = 81` – quamrana Nov 08 '20 at 18:13
  • Also one more question im a little confused about - why is the code running x=77 and x=79 etc. if it has specified while x<=num? – SH96 Nov 08 '20 at 18:49
  • The `while x <= num:` only executes when it executes and not at other times. When the program is in the `for` loop, the `while` has no effect. The `while` only has any effect when the `for` loop exits. That is what the `break` gives you. It exits the `for` loop to let `while` come into play. – quamrana Nov 08 '20 at 18:52
  • Thank you :) Also i see you've changed the position of the x+=2 in your neater code^. Why does this work because if you break out of the for loop [if x%y==0], then you return back to the while <=num statement, thus your code won't run x+=2? – SH96 Nov 11 '20 at 14:40
  • No, the `break` jumps out of the nearest enclosing loop, which is the `for` loop, so the very next thing is `x += 2`. – quamrana Nov 11 '20 at 14:43