小编Tho*_*mas的帖子

用于查找素数的Python while循环

作为Python的第一个练习,我正在尝试使用循环编写程序来查找素数.一切都与for循环一起工作所以我试图使用while循环.这有效,但程序返回一些不正确的数字.

import math
# looking for all primes below this number
max_num = int(input("max number?: "))

primes = [2]  # start with 2
test_num = 3  # which means testing starts with 3

while test_num < max_num:
    i = 0
    # It's only necessary to check with the primes smaller than the square
    # root of the test_num
    while primes[i] < math.sqrt(test_num):
        # using modulo to figure out if test_num is prime or not
        if (test_num % primes[i]) == 0: …
Run Code Online (Sandbox Code Playgroud)

python primes while-loop python-3.x

3
推荐指数
1
解决办法
7908
查看次数

标签 统计

primes ×1

python ×1

python-3.x ×1

while-loop ×1