算法与程序

问题描述 投票:0回答:1

我需要为 python 编写一个算法和一个程序..分别打印 2 到 500 之间数字的所有除数。

我只是初学者,请帮助我,我需要准确的算法才能完成 以及这些除数的准确 Python 程序

algorithm math numbers
1个回答
0
投票
# loop for number in range of [2,501) note: range is inclusive on the start and exclusive at the end
for x in range(2, 501):
    # adding the end="" tag makes print not add a newline at the end
    print("number:", x, "divisors: ", end="")
    for i in range(1, x + 1):
        # % takes the mod of x with i
        if (x % i == 0):
            print(i, end=" ")
    # because we didn't print newlines we have to add it sepereatly so the next number starts in newline
    print()
© www.soinside.com 2019 - 2024. All rights reserved.