python - How can I continue the for loop of the outer side? -
def countprimes(self, n): if n <= 1: return 0 if n == 2: return 1 count = 0 counted = [2, ] num in xrange(3, n+1, 2): c in counted: if num % c == 0: continue count += 1 counted.append(num) return count
i writing code solution of primes counting problems. used counted array storing primes have been examined , use them examination next prime. tried using continue drop out inner loop count += 1 , counted.append(num)
not executed once num found not valid prime. however, met implementing problem here, continue
statement take me c
instead of num
.
if understand question correctly, want know how break inner c
loop, avoid other code, , continue num
. other answers, want make use of break
smart booleans. here's loop might like:
for num in xrange(3, n+1, 2): next_num = false c in counted: if num % c == 0: next_num = true break if next_num: continue count += 1 counted.append(num)
this way, if encounter num divisible c, break out of inner c
loop, avoid adding num counted list.
Comments
Post a Comment