检查一个数字是否为质数的Python程序

385 阅读5分钟

检查一个数字是否为质数的Python程序

一个数字的因数。

当两个整数相乘时,其结果是一个积。乘积的因子就是我们所乘的数字。

在数学中,因数是一个数字或代数表达式,它平均除以另一个数字或表达式,不留余地。

素数。

一个质数是一个大于1的正整数,除了1和数字本身之外没有其他变量。因为它们没有其他变量,所以2、3、5、7等数字都是质数。

给定一个数字,任务是检查该数字是否是质数。

举例说明

例1:

输入

number =5

输出

The given number 5 is prime number

例2:

输入

number =8

输出

The given number 8 is not prime number

例子3:

输入

number =2

输出。

The given number 2 is not prime number

用Python编程检查质数的程序

以下是检查给定数字是否为质数的方法。

Python编程实例指南中探索更多与Python概念相关的实例,使Python编程语言从初学者晋升为专业程序员水平。

1)使用for循环,用标志或临时变量从2到N-1循环。

为了查看除了1和数字本身之外是否有任何正除数,我们用输入的数字除以2到(N-1)范围内的所有数字。

如果发现被除数,则显示 "数字不是质数 "的信息,否则显示 "数字是质数 "的信息。

我们使用for循环从2到N-1进行遍历。

下面是实现的过程。

# given number
number = 5
# intializing a temporary variable with False
temp = False
# We will check if the number is greater than 1 or not
# since prime numbers starts from 2
if number > 1:
    # checking the divisors of the number
    for i in range(2, number):
        if (number % i) == 0:
            # if any divisor is found then set temp to true since it is not prime number
            temp = True
            # Break the loop if it is not prime
            break
if(temp):
    print("The given number", number, "is not prime number")
else:
    print("The given number", number, "is prime number")

输出

The given number 5 is prime number

解释

在这个程序中我们检查了一个数字是否是质数。质数不是那些小于或等于1的数字。因此,我们只在数字大于1的情况下才前进。

我们检查一个数字是否能被2和数字-1之间的任何数字整除。我们将temp设为 "真",如果我们在这个范围内找到一个因子,则表明这个数字不是质数,我们就中断循环。

我们在循环外检查temp是True还是False。

如果是True,数字就不是质数。
如果是假的,给定的数字就是一个质数。

2)使用For Else语句

为了检查数字是否是质数,我们使用了for...else参数。

它是基于这样的逻辑:当且仅当for循环没有被破坏时,for循环的else语句才会运行。只有当没有找到变量时,才满足条件,表明给定的数字是质数。

因此,我们在else子句中打印该数字是质数。

下面是实现过程。

# given number
number = 5

# We will check if the number is greater than 1 or not
# since prime numbers starts from 2
if number > 1:
    # checking the divisors of the number
    for i in range(2, number):
        if (number % i) == 0:
            # if any divisor is found then print it is not prime
            print("The given number", number, "is not prime number")
            # Break the loop if it is not prime
            break
    else:
        print("The given number", number, "is prime number")
# if the number is less than 1 then print it is not prime number
else:
    print("The given number", number, "is not prime number")

输出

The given number 5 is prime number

3)上述方法在时间复杂度方面的限制

在这两种方法中,循环从2运行到N-1。

因此,我们可以说上述方法的时间复杂性为O(n)。

如果数字非常大呢?

比如10^18,上述方法需要将近31年的时间来执行。

那么如何避免这种情况呢?

我们可以看到,除了数字本身,数字的因子从1到N/2都存在。

但这也需要15年的时间来执行。

因此,为了解决这个问题,我们在下一个方法中一直循环到N的平方根,这样做的时间复杂度为O(Sqrt(n))。

4)高效方法的解决方法

我们将通过减少搜索因子的数量范围来改进我们的程序。

在上面的程序中,我们的搜索范围是2到数字-1。

应该使用一组,range(2,num/2)或range(2,math.floor(math.sqrt(number))。后者的范围是基于复合数的因子必须小于其平方根的要求。否则,它就是一个素数。

5)高效方法的实现

在这个函数中,我们使用Python的数学库来计算一个整数,max_divisor,它是该数的平方根,并得到它的底限值。我们在上一个例子中从2到n-1进行迭代。然而,在这种情况下,我们把除数减少一半,如图所示。为了得到floor和sqrt函数,你需要导入数学模块。
办法。

  • 如果整数小于1,则返回False。
  • 现在需要验证的数字被还原为给定数字的平方根。
  • 如果给定的数字能被2到数字的平方根中的任何一个数字整除,该函数将返回False。
  • 否则,将返回True。

下面是实现的过程。

# importing math module
import math
# function which returns True if the number is prime else not


def checkPrimeNumber(number):
    if number < 1:
        return False
    # checking the divisors of the number
    max_divisor = math.floor(math.sqrt(number))
    for i in range(2, max_divisor+1):
        if number % i == 0:
            # if any divisor is found then return False
            return False
        # if no factors are found then return True
        return True


# given number
number = 5
# passing number to checkPrimeNumber function
if(checkPrimeNumber(number)):
    print("The given number", number, "is prime number")
else:
    print("The given number", number, "is not prime number")

输出

The given number 5 is prime number