簡體   English   中英

我在python中的素數函數怎么了?

[英]What is wrong with my prime number function in python?

我還看到了其他示例,其中有人使用“ for x range in any”,但是我們的老師還沒有教我們。

def is_prime(y):
    x=2
    while x<=y:
        if x=y:
            return True
        elif y%x==0:
            return False
        else:
            x=x+1

這是使用range函數的等效項:

def is_prime(y):
    for x in range(2, y):
        if y % x == 0:
            return False
    return True

我想到了。 對於y值小於2,我必須使其返回false,並且必須將ax = y更改為x == y。 這是我的新程序。

def is_prime(y):
    if y<2:
        return False
    else:
        x=2
        while x<=y:
            if x==y:
                return True
            elif y%x==0:
                return False
            else:
                x=x+1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM