Python PrimePy 模塊

我們在學校的時候都學過質數,如果有誰忘記了也不用擔心。質數基本上是那些只能被 1 或自身整除的自然數,質數的另一個定義是大於 1 的自然數,除了數本身或 1 之外,沒有任何完整的整除因子。所有質數都是奇數,除了『2』,它是唯一的偶數質數。

除了 2 和 3 之外,所有的質數都可以用下面給出的兩種形式中的任何一種來表示:

(i) 6a + 1

(二) 6a – 1

在上面給出的表達式中,「a」是一個自然數。

我們可能都聽說過,通過編程語言處理數字非常容易,這是因為大多數編程語言都為我們提供了包,我們可以使用這些包來分類和處理所有類型的數字,例如整數、自然和其他類型。在程序中使用這些編程語言包使得處理數字、對數字進行分類以及處理數字變得非常容易。像所有其他編程語言一樣,Python 也為我們提供了多個包,我們可以在 Python 程序中使用它們來處理和處理數字。其中一些包內置於源代碼 Python 中,有些包需要在我們必須使用它們時安裝。Python 的模塊 PrimePy 就是我們上面談到的這樣的 Python 包之一。利用 Python 程序中的質數模塊,我們可以對質數進行運算,比如求一個數的質數因子等。因此,我們將在本教程中學習質數模塊,並了解如何在 Python 程序中使用它及其函數來處理質數和其他數字。

Python 質數模塊介紹

PrimePy 是一個 Python 包或模塊,它提供了多個函數,我們可以使用這些函數來計算與質數直接或間接相關的多個運算。這個模塊最大的優點是使用這個模塊,所有關於質數的功能都可以在更短的時間內完成。因為質數沒有任何因素,這就是為什麼有時使用它們會變得非常困難。在這種情況下,質數模變得非常重要,因為它提供了一些函數,我們可以用這些函數輕鬆地對質數進行所有這些運算。

安裝原始模塊

正如我們在介紹部分已經說過的,一些 Python 模塊沒有內置在 Python 源代碼包中。primepy 就是這樣一個沒有內置的包,因此如果我們想使用 Python 的這個模塊,我們必須首先在我們的設備中安裝這個模塊。有多種安裝方法可以用來在我們的機器上安裝 primepy 模塊,但是在我們選擇這些方法之前,我們應該試着找出哪一種對我們來說最容易和最簡單。因此,在本教程的這一部分,我們已經使用 pip 安裝程序安裝方法在我們的系統中安裝了 primepy 模塊。使用 pip installer 方法安裝 primepy 模塊是最簡單和最容易的方法,我們只需在終端 Shell 中編寫以下 pip 命令來安裝該模塊:


pip install primePy

一旦我們在系統的命令提示終端中編寫了上面給出的 pip 命令,我們只需要按下「回車」鍵,primepy 模塊的安裝就開始了。現在,我們只需要等待一段時間,直到成功安裝該模塊的所有依賴項。

我們可以看到,質數模塊已經成功安裝在我們的設備中,現在我們可以在 Python 程序中導入這個模塊,使用這個模塊的功能來處理質數。

Python 中質數模塊的實現

我們可以使用 Python 程序中的質數模塊來執行許多功能,包括質數的直接或間接參與。將質數模塊導入程序後,我們可以使用該模塊的功能對質數執行所需的任務。

我們可以使用質數模塊的功能來執行以下幾個功能:

1) 檢查質數

2) 返回質數的n個數

3) 返回前n個質數

4) 介於兩個數和許多其他數之間的質數

我們將在本節的示例程序中使用 primepy 模塊的功能來執行這些任務,這就是我們如何理解 primepy 模塊的實現。

實現 1:檢查給定自然數是否為質數的函數:

我們可以用質數模來確定給定的數是否是質數。PrimePy 模塊為我們提供了 check()函數,我們可以用它來檢查數字是不是質數。以下是在 Python 程序中使用 check()函數的語法:


>> prime.check(num)

其中 num =程序中給定的自然數

通過在下面的示例程序中使用 check()函數,我們可以理解 primepy 模塊的 check()函數的實現:

示例 1: 看看下面的 Python 程序,我們可以從中找出給定的數字是否是質數:


# Importing prime library from the Primepy module
from primePy import primes as pms
# Taking two natural numbers as user input
num1 = int(input("Enter first natural number: "))
num2 = int(input("Enter second natural number: "))
# Checking If user input numbers are prime
res1 = pms.check(num1)
res2 = pms.check(num2)
# Printing result in the output
print("Is the first natural number which is entered by user in input is prime? ", res1)
print("Is the second number entered by user is prime? ", res2)

輸出:

Enter first natural number: 88
Enter second natural number: 673
Is the first natural number which is entered by the user in the input is prime?  False
Is the second number entered by user is prime?  True

如我們所見,輸出以真和假的形式打印,說明用戶輸入的數字是否是質數。這就是我們如何使用 primepy 模塊的 check()函數編寫任何 Python 程序來檢查程序中給定的數字是否是 prime。

實現 2:打印程序中給定自然數的所有質因數:

利用質數模塊的功能,我們可以很容易地打印出程序中任意給定自然數輸出中的所有質數因子。PrimePy 模塊為我們提供了 factors()函數,我們可以在任何示例程序中使用該函數來打印給定用戶輸入數字或程序中給定數字的所有 prime factors。以下是在 Python 程序中使用 factors()函數的語法:


>> prime.factors(num)

其中 num =程序中給定的自然數

我們可以理解 primepy 模塊的 factors()函數的實現,通過在下面的示例程序中使用該函數來打印自然輸入數的所有 prime factors:

示例 2: 請看下面的程序,我們在其中找到並打印了給定用戶輸入數字的所有質因數作為結果:


# Importing prime library from the Primepy module
from primePy import primes as pms
# Taking two natural numbers as user input
num1 = int(input("Enter first natural number: "))
num2 = int(input("Enter second natural number: "))
# Finding all prime factors of user input numbers
res1 = pms.factors(num1)
res2 = pms.factors(num2)
# Printing result of prime factors in the output
print("All prime factors of the first number entered by user: ", res1)
print("All prime factors of the second number entered by user: ", res2)

輸出:

Enter first natural number: 243
Enter second natural number: 679
All prime factors of the first number entered by user:  [3, 3, 3, 3, 3]
All prime factors of the second number entered by user:  [7, 97]

如我們所見,用戶在輸入中輸入的兩個數字的所有質因數都打印在輸出中,這就是我們如何知道任何給定數字的所有質因數。

實現 3:打印數字的最低或第一質因數:

primepy 模塊的這種實現與本模塊的前一種實現非常相似。這兩種實現的唯一區別是,我們將只打印一個數字的第一個或最低質因數,而以前,我們打印給定數字的所有質因數。質數模塊為我們提供了因子(),我們可以用它來打印任何給定數字的最低質數因子。以下是在 Python 程序中使用 factor()函數的語法:


>> prime.factor(num)

其中 num =程序中給定的自然數

我們可以理解 primepy 模塊的 factor()函數的實現,通過在下面的示例程序中使用這個函數來打印一個數的第一個 prime factor:

例 3: 看看下面的程序,我們使用了 factor()函數來打印給定輸入數字的最低或第一個質因數:


# Importing prime library from the Primepy module
from primePy import primes as pms
# Taking two natural numbers as user input
num1 = int(input("Enter first natural number: "))
num2 = int(input("Enter second natural number: "))
# Finding lowest prime factor from the user input numbers
res1 = pms.factor(num1)
res2 = pms.factor(num2)
# Printing result of the lowest prime factor in the output
print("The lowest prime factor of the first number entered by user: ", res1)
print("The first prime factor of the second number entered by user: ", res2)

輸出:

Enter first natural number: 73
Enter second natural number: 279
The lowest prime factor of the first number entered by user:  73
The first prime factor of the second number entered by user:  3

如我們所見,兩個輸入數字的最低或第一個質因數都打印在輸出中,這就是我們如何在任何 Python 程序中使用 primepy 模塊的 factor()函數來打印任何給定自然數的最低質因數。

實現 4:給定自然數的質數:

假設我們有一個給定的數,我們必須找出有多少質數小於或等於給定的數。如果手動計算,這是一項非常繁忙和耗時的任務,這就是我們可以利用 primepy 模塊的地方。primepy 模塊為我們提供了一個選項,在這個選項中,我們可以打印用戶給定數字之前的所有質數。對於這個任務,質數模塊為我們提供了 upto()函數,我們可以用它來打印任意自然數的質數。以下是在 Python 程序中使用 factor()函數的語法:


>> prime.upto(num)

其中 num =程序中給定的自然數

我們可以理解質數模塊的 upto()函數的實現,通過在下面的示例程序中使用該函數,用戶可以打印給定數量的質數:

示例 4: 看看下面的程序,其中我們使用了 upto()函數來打印用戶在 input()中給定數字之前的所有質數:


# Importing prime library from the Primepy module
from primePy import primes as pms
# Taking two natural numbers as user input
num1 = int(input("Enter first natural number: "))
num2 = int(input("Enter second natural number: "))
# Finding total prime number upto user input numbers
res1 = pms.upto(num1)
res2 = pms.upto(num2)
# Printing result of prime factors in the output
print("Following are all prime numbers that are either less than or equal to", num1, "as given by the user: ", res1)
print("Following are all prime numbers that are either less than or equal to", num2, "as given by the user: ", res2)

輸出:

Enter first natural number: 15
Enter second natural number: 18
Following are all prime numbers that are either less than or equal to 15 as given by the user :  [2, 3, 5, 7, 11, 13]
Following are all prime numbers that are either less than or equal to 18 as given by the user :  [2, 3, 5, 7, 11, 13, 17]

如我們所見,所有小於或等於用戶給出的輸入數的質數都會打印在輸出中。這就是我們如何使用 upto 函數打印給定自然數的所有質數。

實現 5:打印前n個質數:

primepy 模塊的這種實現類似於該模塊的前一種實現,因為在這兩種實現中,我們都根據給定的數字打印某些質數。具體說到這個實現部分,我們將首先打印『n』(其中 n 是自然數)質數,並且『n』將是程序中的給定數。primepy 模塊為我們提供了 first()函數,我們可以用它來打印輸出中的前n個質數。以下是在 Python 程序中使用第一個()函數的語法:


>> prime.first(num)

其中 num =程序中給定的自然數

我們可以理解 primepy 模塊的 first()函數的實現,通過在下面的示例程序中使用該函數來打印第一個「n」個 prime factors:

例 5: 看看下面的程序,我們使用了 first()函數來打印用戶給定輸入數的前『n』個質因數:


# Importing prime library from the Primepy module
from primePy import primes as pms
# Taking a natural number as user input
num1 = int(input("The number of first prime numbers you want to print: "))
# Finding total first prime numbers according to user input number
res1 = pms.first(num1)
# Printing result of all first prime numbers in the output
print("These are first", num1, "prime numbers as the number entered by user: ", res1)

輸出:

The number of first prime numbers you want to print: 7
These are first 7 prime numbers as the number entered by the user:  [2, 3, 5, 7, 11, 13, 17]

正如我們所看到的,第一個『num』(用戶在程序中輸入的數字)質數被打印在輸出中,這就是我們可以打印任何第一個『num』質數的方式。

實現 6:獲取任意兩個給定輸入數之間的所有質數:

primepy 模塊的這個實現部分也非常類似於這個模塊的 upto()函數的實現。唯一的區別是,在這裡,我們會找到任意兩個給定數字之間的質數,而以前,我們會找到給定數字之前的質數(或 1 和給定數字之間的質數)。為了執行找出任意兩個用戶輸入自然數之間存在的所有質數的任務,質數模塊為我們提供了 between()函數。以下是使用 between()函數查找兩個給定數字之間的所有質數的語法:


>> primes.between(num1, num2)

# Importing prime library from the Primepy module
from primePy import primes as pms
# Taking three natural numbers as user input
num1 = int(input("Enter first natural number: "))
num2 = int(input("Enter second natural number: "))
num3 = int(input("Enter third natural number: "))
# Getting all prime numbers which are between the two input numbers
res1 = pms.between(num1, num2)
res2 = pms.between(num2, num3)
# Printing resultant prime numbers in the output
print("These are all prime numbers which are present between first and second number given by you: ", res1)
print("These are the prime numbers which are present between the second and third number given by you: ", res2)

輸出:

Enter first natural number: 5
Enter second natural number: 41
Enter third natural number: 79
These are all prime numbers which are present between first and second number given by you:  [5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
These are all prime numbers which are present between the second and third number given by you:  [41, 43, 47, 53, 59, 61, 67, 71, 73]

如我們所見,用戶在兩個給定自然數之間出現的所有質數都會打印在輸出中,這就是我們如何使用 primepy 模塊的 between()函數找出並打印任意兩個給定數之間的所有質數。


原創文章,作者:M1QR0,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/129014.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
M1QR0的頭像M1QR0
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29

發表回復

登錄後才能評論