数据分析交流吧 关注:21贴子:97
  • 0回复贴,共1

为什么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


IP属地:北京1楼2019-01-31 11:24回复