简体   繁体   English

检查素数:任何数字是素数?

[英]Checking for prime numbers: Any number is prime?

Hello dearest community, 您好亲爱的社区,

I'm stuck on something very easy, I guess sometimes your just out of it. 我被困在一个非常简单的事情上,我想有时候您只是出于例外。 Anyhow, I wrote this to check for prime numbers: 无论如何,我写了这个来检查素数:

num = int(input(""))

if num > 1:
    for i in range(2, num-1):
        if (num % i)  == 0:
            print(num, "is no Primenumber")
            break
        else:
            print(i, "PRIME!!!")

(The original code doesn't print anything, but I had trouble with it, so I rewrote it for debugging purposes) (原始代码无法打印任何内容,但是我遇到了麻烦,因此我将其重写以用于调试)

Anyway, it correctly identifies non prime numbers, but prints out this for prime numbers, lets say 7: 无论如何,它可以正确识别非质数,但可以打印出质数,例如7:

7
2 PRIME!!!
3 PRIME!!!
4 PRIME!!!
5 PRIME!!!

If you are concerned with performance for larger numbers it is better to use square root over number flooring division 2 . 如果您担心较大数字的性能,最好在number flooring division 2上使用square root Any number that is a factor greater than the square root will also have 1 that is less. 任何大于平方根的因数也将具有小于1的因数。

Here's a common solution: 这是一个常见的解决方案:

def check_for_prime(p_in):
    is_prime = True
    for n in range(2, int(p_in ** 0.5) + 1):
        if p_in % n == 0:
            is_prime = False
            break
    return is_prime

num = int(input(""))

if check_for_prime(num):
    print(f'{num} is prime')
else:
    print(f'{num} is not prime')

Not the most performance conscious, but will get the job done and is simple. 不是最注重性能的人,但是可以完成工作并且很简单。

n = input("Enter a number to check if it is prime [returns True or False]: ")
n = int(n)

if n < 2:
    quit()

if all(n % i for i in range(2, n)):
    print(n, "PRIME!!!")

Use the below logic to find the number is prime or not. 使用以下逻辑查找数字是否为质数。 This is suited for python, as there is an else block for the for loop which gets exectued when the loop is not broken during execution which mean the number is prime. 这适用于python,因为for循环还有一个else块,当执行过程中循环未中断时,该块将执行,这意味着数字为质数。

if num > 1: 

   # Iterate from 2 to n / 2  
   for i in range(2, num//2): 

       # If num is divisible by any number between  
       # 2 and n / 2, it is not prime  
       if (num % i) == 0: 
           print(num, "is not a prime number") 
           break
   else: 
       print(num, "is a prime number") 

else: 
   print(num, "is not a prime number") 

Without third-party module, you can easily perform it this way: 如果没有第三方模块,则可以通过以下方式轻松执行:

def is_prime(number):
    return all(number % n for n in range(2, number))

This way, you check: 这样,您检查:

  • all division between the specified number, and any number between 2 and the one before the number 指定数字之间的所有除数,以及2和该数字之前的数字之间的任何数字
  • the trick is to consider the fact that if any number % n gives 0, which means it is a multiple, so it can't be a prime number, and you will get a result immediately 诀窍是考虑以下事实:如果任何number % n给出0,则意味着它是一个倍数,因此它不能是素数,您将立即得到结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM