以下是Python代码:

def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

使用方法:

调用is_prime(n)函数,返回值为True表示n是素数,返回值为False表示n不是素数。

我要python代码 判断一个正整数nn=2是否为素数。素数又称质数。一个大于1的自然数除了1和它自身外不能被其它整数整除的数叫做素数;否则称为合数。

原文地址: https://www.cveoy.top/t/topic/0yh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录