如何在Go語言中顯示1到N之間的所有質數?
在本教程中,我們將學習如何列印1到N之間的質數,其中N的值將作為使用者輸入。簡要介紹一下質數:質數只能被1或自身整除。在本教程中,我們將討論兩種方法,一種時間複雜度為O(N^2),另一種時間複雜度為O(N*log(logN))。
方法一
時間複雜度:O(N^2)
空間複雜度:O(1)
演算法
步驟1 - 宣告int32型別的數字N
步驟2 - 從使用者處獲取輸入,這將告訴我們在哪個範圍內查詢質數。
步驟3 - 現在我們呼叫一個函式,該函式包含查詢質數並列印它們的邏輯。
示例1
package main // fmt package provides the function to print anything import "fmt" func printPrimeNumbersBeforeN(N int) { // running the for loop from 1 to N for i := 2; i <= N; i++ { // flag which will confirm that the number is Prime or not isPrime := true // This for loop is checking that the current number have // any divisible other than 1 for j := 2; j <= i/2; j++ { if i%j == 0 { isPrime = false break } } // if the value of the flag is false then the number is not prime // and we are not printing it. if isPrime { fmt.Println(i) } } } func main() { // declaring the variable N till which we have to find the Prime Numbers var N int fmt.Println("Enter the value of N.") // Taking the input of N from the user fmt.Scanln(&N) fmt.Println("The prime numbers between 1 to", N, "where N is included are -") // calling the function to find and print the prime numbers printPrimeNumbersBeforeN(N) }
printPrimeNumbersBeforeN(N) - 這是在Go語言中呼叫的函式,其中N作為引數傳遞。在這個定義在main函式之前的函式中,包含了查詢質數的核心邏輯。
func printPrimeNumbersBeforeN(N int) - 這是一個函式定義,它有一個整數作為引數。
for i := 2; i <= N; i++ {} - 此迴圈從2執行到N,對於每個數字,我們將在巢狀迴圈中檢查它是否為質數。
for j := 2; j <= i/2; j++ {} - 如果我們觀察到一個數字不是質數,那麼它的一個因子將是其值的一半。因此,此迴圈從2執行到外迴圈當前索引值的一半。在迴圈內部,我們對內迴圈索引進行外迴圈索引的模運算,如果結果為零,則我們將isPrime設定為false並中斷內迴圈。
if isPrime {} - 上述迴圈結束後,我們檢查isPrime的值,如果為true,則列印該數字。
輸出
Enter the value of N. 25 The prime numbers between 1 to 25 where N is included are - 2 3 5 7 11 13 17 19 23
方法二
在這個方法中,我們將討論埃拉托色尼篩法來查詢1到N之間的質數。這種方法比前面一種方法更有效。
時間複雜度:O(N*log(logN))
空間複雜度:O(N)
演算法
步驟1 - 宣告int32型別的數字N
步驟2 - 從使用者處獲取輸入,這將告訴我們在哪個範圍內查詢質數。
步驟3 - 現在我們呼叫一個函式,該函式包含查詢質數並列印它們的邏輯。
示例2
package main // fmt package provides the function to print anything import "fmt" func initializeArray(primeArray []bool, N int) { // initializing the array with true for i := 0; i < N; i++ { primeArray[i] = true } } func printPrimeNumbersBeforeN(N int) { primeArray := make([]bool, N+1) initializeArray(primeArray, N+1) // running the for loop from 1 to under root N for i := 2; i*i <= N; i++ { if primeArray[i] { // This for loop is running from the square of upper // loop index until N and traversing over the multiple of i // by increasing the j index by i for j := i * i; j <= N; j = j + i { primeArray[j] = false } } } // printing the prime number by checking the status of primeArray status for i := 2; i <= N; i++ { if primeArray[i] { fmt.Println(i) } } } func main() { //declaring the variable N till which we have to find the Prime Numbers var N int fmt.Println("Enter the value of N.") // Taking the input of N from the user fmt.Scanln(&N) fmt.Println("The prime numbers between 1 to", N, "where N is include are -") // calling the function to find and print the prime numbers printPrimeNumbersBeforeN(N) }
printPrimeNumbersBeforeN(N) - 這是在Go語言中呼叫的函式,其中N作為引數傳遞。在這個定義在main函式之前的函式中,包含了查詢質數的核心邏輯。
func printPrimeNumbersBeforeN(N int) - 這是一個函式定義,它有一個整數作為引數。
primeArray := make([]bool, N+1) - 在這裡,我們建立一個名為**primeArray**的布林陣列,大小為N + 1。
finitializeArray(primeArray, N+1) - 這行程式碼呼叫一個在上面實現的函式,該函式用true初始化陣列。
for i := 2; i*i <= N; i++ {} - 因為任何數字的因子都將出現在該數字的平方根之前,所以我們將for迴圈從2執行到N的平方根。
for j := i * i; j <= N; j = j + i {} - 此迴圈從外迴圈索引的平方執行到N,並將內迴圈索引增加i,並將primeArray從true更改為false,因為當前索引已經是某個數字的倍數,所以它不能是質數。
最後,我們透過檢查**primeArray**來列印質數。
輸出
Enter the value of N. 35 The prime numbers between 1 to 35 where N is included are - 2 3 5 7 11 13 17 19 23 29
以上是在Go語言中查詢1到N之間質數的兩種方法。要了解更多關於Go語言的資訊,您可以瀏覽此教程。