使用庫函式在 Go 語言中顯示兩個區間之間的素數的程式。
在本文中,我們將討論如何在 Go 語言中使用函式顯示兩個區間之間的素數。
語法
func Sieve(n int) []int func IsPrime(n int) bool If else conditionals in GO language: If condition { // code to be executed } else { // code to be executed } For loop as a while loop in GO language: for condition { // code to be executed // increment or decrement the count variable. }
Sieve() 函式定義在 primes 包中,它接受一個整數作為引數,並返回一個包含該整數之前所有素數的整數陣列。
IsPrime() 函式定義在 primes 包中,它接受一個 64 位無符號整數作為引數,並根據該數字是否為素數返回布林表示式。
有很多方法可以顯示兩個區間之間的素數。我們將在接下來的示例中討論這些方法。
示例 1:使用函式在 Go 語言中顯示兩個區間之間的素數的程式
演算法
步驟 1 - 匯入 fmt 和 primes 包。
步驟 2 - 初始化並定義 primeNumber() 函式。
步驟 3 - 啟動 main() 函式。
步驟 4 - 初始化 int 資料型別的變數併為其賦值。
步驟 5 - 呼叫 primeNumber() 函式並將分配的數字作為命令列引數傳遞給它。
步驟 6 - 在螢幕上列印結果。
示例
// golang program to display prime numbers using library function. package main import ( "fmt" "github.com/fxtlabs/primes" ) // fmt package allows us to print anything on the screen. // primes package allows us to use methods that help us to use methods related to primes. // initializing and defining the primeNumber() function func primeNumber(number int) []int { // getting the prime numbers in the said range prime := primes.Sieve(number) // returning the result return prime } func main() { // initializing the variables to get the intervals. var number int // printing prime numbers between 2 and 50 number = 50 // calling the primeNumber() function result := primeNumber(number) // printing the results on the screen fmt.Printf("prime numbers between 2 and %d are: ", number) fmt.Println(result) } main.go:5:4: cannot find package "github.com/fxtlabs/primes" in any of: /usr/lib/golang/src/github.com/fxtlabs/primes (from $GOROOT) /home/cg/root/99493/go/src/github.com/fxtlabs/primes (from $GOPATH)
輸出
prime numbers between 2 and 50 are: [2 3 5 7 11 13 17 19 23 29 31 37 41 43 47]
程式碼描述
首先,我們匯入允許我們列印任何內容的 fmt 包和允許我們使用其他預定義素數方法的 primes 包。
然後建立並定義 primeNumber() 函式。此函式包含列印素數的邏輯。
啟動 main() 函式。
初始化並定義變數 number,它將是列印素數的上限。
然後呼叫 primeNumber() 函式並將此值作為命令列引數傳遞給它。
呼叫 primes 包中定義的 sieve() 方法並將選定的數字作為引數傳遞給它。
將此函式返回的整數陣列儲存在單獨的變數中並返回它。
從 primeNumbers() 函式返回此陣列。
使用 fmt.Println() 函式儲存結果並在螢幕上列印它們。
示例 2:使用函式在 Go 語言中顯示兩個自定義區間之間的素數的程式。
讓我們使用另一種方法使用函式在兩個區間中列印素數。
演算法
步驟 1 - 匯入 fmt 包。
步驟 2 - 初始化並定義 primeNumber() 函式。
步驟 3 - 啟動 main() 函式。
步驟 4 - 初始化 int 資料型別的變數併為其賦值。這些值將形成要計算素數的區間。
步驟 5 - 呼叫 primeNumber() 函式。
步驟 6 - 在螢幕上列印結果。
示例
// golang program to display prime numbers in two custom intervals using library function. package main import ( "fmt" "github.com/afshin/prime" ) // fmt package allows us to print anything on the screen. // prime package allows us to use inbuilt prime functions // initializing and defining the primeNumber() function func primeNumber(lower_limit, upper_limit uint64) { fmt.Printf("prime numbers between %d and %d are:\n", lower_limit, upper_limit) // printing the result for i := lower_limit; i <= upper_limit; i++ { // getting bool value from isprime() function and using it as condition of if loop. if prime.IsPrime(i) { fmt.Println(i) } } } func main() { // initializing the variables to get the intervals from the user var lower_limit, upper_limit uint64 // let us print prime numbers between 5 and 60. // assigning the value of lower_limit and upper_limit variables. lower_limit = 5 upper_limit = 60 // calling the primeNumber() function primeNumber(lower_limit, upper_limit) }
輸出
prime numbers between 5 and 60 are: 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
程式碼描述
首先,我們匯入允許我們列印任何內容的 fmt 包和允許我們使用其他預定義素數方法的 primes 包。
然後建立並定義 primeNumber() 函式。此函式包含列印素數的邏輯。
啟動 main() 函式。
初始化並將數字分配給變數,該變數將是列印素數的上限和下限。
然後呼叫 primeNumber() 函式並將這些區間作為引數傳遞給它。
呼叫 primes 包中定義的 isprime() 方法。此函式接受一個 64 位無符號整數作為引數,並根據該數字是否為素數返回布林表示式。
檢查區間上下限之間每個值的此條件。
使用 fmt.Printf() 函式在螢幕上列印滿足上述 if 條件的值。
結論
我們已經成功編譯並執行了 Go 語言程式,以使用函式顯示兩個區間之間的素數,並附帶示例。