热线电话:13121318867

登录
2019-01-31 阅读量: 773
为什么is_prime对某些数字失败了?

问题描述:

为什么is_prime对某些数字失败了?

问题解决:

如果你的代码对某些数字失败了,可能是因为你return太快了,或者因为你return错了地方错了。在不给出答案的情况下,请查看下面的示例控制流伪代码:

# From hint: any number less than 2 is not prime
if x is less than 2:
return x is not prime

# This loop is where we put our number to the test and return False
# so the function exits immediately if x isn’t prime
for n from 2 to x:
if x is evenly divisible by n:
return x is not prime

# If we made it here, our number must be prime because none
# of the other return statements were executed!
return x is prime
0.0000
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子