我正在编写一个简单的算法来检查整数的素性,但我有一个问题需要将这段Java代码翻译成Python:
for (int i = 3; i < Math.sqrt(n); i += 2) {
if (n % i == 0)
return false;
}
所以,我一直在尝试使用它,但显然我跳过了除以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; }
- 除了作用域问题和必须处理continue
之外。for-each 结构的最后一个组件在每次循环体的评估之后进行评估。 - user2246674