Python-检查一个数字,看看它是否是使用除数的素数

时间:2016-07-27 01:15:14

标签: python

我必须编写一个程序,通过遍历范围(1,num)中的除数来检查数字是否为素数。 (例如,如果输入为5,程序将检查2是否为除数,然后是3,然后是4)。有人可以解释我如何将最后一个表达式放在我的程序中(“这是一个素数”打印函数),这样它只出现在“for”循环中的“else”函数之后而不是“if div = = 0:“陈述?谢谢!

while True:
    num = int(input("Enter a positive number to see if it's a prime number: "))
    if num > 1:
        break
    elif num == 1:
        print("1 is technically not a prime number.")
    else:
        print("Number cannot be negative- try again.")

for num1 in range(2,num):
    div = num%num1
    if div == 0:
        print(num1,"is a divisor of",num,"... stopping.")
        print(num,"is not a prime number.")
        break
    else:
        print(num1,"is NOT a divisor of",num,"... continuing")

print(num,"is a prime number!")

4 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点:

设置标志!

is_prime = True # a boolean flag
for num1 in range(2,num):
    div = num%num1
    if div == 0:
        print(num1,"is a divisor of",num,"... stopping.")
        print(num,"is not a prime number.")
        is_prime = False
        break
    else:
        print(num1,"is NOT a divisor of",num,"... continuing")

 if is_prime:
     print(num,"is a prime number!")

使用for-else

for num1 in range(2,num):
    div = num%num1
    if div == 0:
        print(num1,"is a divisor of",num,"... stopping.")
        print(num,"is not a prime number.")
        is_prime = False
        break
    else:
        print(num1,"is NOT a divisor of",num,"... continuing")
 else: 
      print(num,"is a prime number!") # will execute only when for-loop ends

答案 1 :(得分:1)

Python的for表达式有an optional else clause

  

循环语句可能有一个else子句;当循环通过列表耗尽(with for)或条件变为false(with while)时终止,但是当循环被break语句终止时,执行它。

所以你的代码应该是这样的:

while True:
    num = int(input("Enter a positive number to see if it's a prime number: "))
    if num > 1:
        break
    elif num == 1:
        print("1 is technically not a prime number.")
    else:
        print("Number cannot be negative- try again.")

for num1 in range(2,num):
    div = num%num1
    if div == 0:
        print(num1,"is a divisor of",num,"... stopping.")
        print(num,"is not a prime number.")
        break
    else:
        print(num1,"is NOT a divisor of",num,"... continuing")
else:
    print(num,"is a prime number!")

答案 2 :(得分:0)

鉴于你已经将除法除以1,所有你需要做的就是计算是否只有一个除数(数字本身)。因此,为了保持代码结构,我添加了一个计数器。

while True:
    num = int(input("Enter a positive number to see if it's a prime number: "))
    if num > 1:
        break
    elif num == 1:
        print("1 is technically not a prime number.")
    else:
        print("Number cannot be negative- try again.")

for num1 in range(2,num):
    div = num%num1
    count = 0
    if div == 0:
        print(num1,"is a divisor of",num,"... stopping.")
        print(num,"is not a prime number.")
        break
    else:
        count += 1
        print(num1,"is NOT a divisor of",num,"... continuing")

if count == 1:
    print(num,"is a prime number!")

答案 3 :(得分:0)

我添加了一行sys.exit()

library(dplyr)

df <- read.table(text = "RT   Max    blk24  blk23    mat23   sm23    sm24
    1   1.6  261    13     19       19      27      20
    2   1.6  284    NA     53       5       99      91
    3   1.7  304    NA     NA       NA      81      NA
    4   1.8  305    32     28       28      39      33
    5   1.9  322    8      NA       NA      36      39
    6   1.9  349    NA     NA       NA      24      17
    7   2    389    1       3       NA      50      NA
    8   2.1  393    80      70      7       12      71
    9   2.1  622    NA      NA      15      97      96
    10  2.2  705    NA      NA      NA      NA      32", header = T)

nams <- df %>% select(matches('blk|mat')) 
df <- df[apply(nams, 1, function(i) all(is.na(i)))==TRUE,]

df

    RT Max blk24 blk23 mat23 sm23 sm24
3  1.7 304    NA    NA    NA   81   NA
6  1.9 349    NA    NA    NA   24   17
10 2.2 705    NA    NA    NA   NA   32