I'm writing a simple algorithm to check the primality of an integer and I'm having a problem translating this Java code into Python:
for (int i = 3; i < Math.sqrt(n); i += 2) {
if (n % i == 0)
return false;
}
So, I've been trying to use this, but I'm obviously skipping the division by 3:
i = 3
while (i < int(math.sqrt(n))):
i += 2 # where do I put this?
if (n % i == 0):
return False
for (a; b; c) { _ }
->a; while(b) { _; c; }
- barring scoping issues and having to deal withcontinue
. The last component of the for-each construct is evalutated after each evaluation of the loop body. – Mayer