[Tutor] returning None from function definition

boB Stepp robertvstepp at gmail.com
Sat Oct 24 18:57:12 EDT 2020


On Sat, Oct 24, 2020 at 06:52:33PM +0530, Manprit Singh wrote:
>
>Based on your comments, I have written it again . Hope this definition is
>perfectly fine .
>def prime(x):
>    if x > 1:
>        for i in range(2, int(x**0.5) + 1):
>            if x % i == 0:
>                return False
>        else:
>            return True
>    else:
>        return False

One suggestion about your algorithm:  Do an explicit check if the number is
divisible by two before you start looping.  Then loop over only the odd
numbers.

-- 
Wishing you only the best,

boB Stepp


More information about the Tutor mailing list