以下是一个示例代码,用于遍历所有小于L的无平方因子的合数:
import math
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
def has_square_factor(n):
for i in range(2, int(math.sqrt(n)) + 1):
if n % (i*i) == 0:
return True
return False
def find_composites(L):
composites = []
for n in range(4, L):
if not is_prime(n) and not has_square_factor(n):
composites.append(n)
return composites
L = 100
composites = find_composites(L)
print("小于", L, "的无平方因子的合数有:", composites)
这段代码定义了三个函数:is_prime(n)
用于判断一个数是否是质数,has_square_factor(n)
用于判断一个数是否有平方因子,find_composites(L)
用于找出所有小于L的无平方因子的合数。
在主程序中,我们设定了L的值为100,并调用find_composites(L)
函数来获取所有小于100的无平方因子的合数。最后将结果打印出来。
运行以上代码,将输出:
小于 100 的无平方因子的合数有: [6, 10, 14, 15, 21, 22, 26, 33, 34, 35, 38, 39, 46, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95]
这些数都是小于100的合数,且没有平方因子。