How to Display all Prime Numbers from 1 to N in Golang?


In this tutorial, we will learn how to print the prime numbers from 1 to N where N the value of N will be as an input from the user. Just a short brief about Prime numbers is that the Prime numbers are something which can be only divisible by 1 or itself. In this tutorial, we are going to discuss two approaches one which will take O(N^2) of time and the other will take O(N*log(logN)).

Method 1

Time Complexity: O(N^2)

Space Complexity: O(1)

Algorithm

  • STEP 1 − Declaring the number N of type int32

  • STEP 2 − Take the input for it from the user which will tell us the range in which we have to find the prime numbers.

  • STEP 3 − Now we are calling the function which will have the logic for finding the prime numbers and printing them.

Example 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)- This is the function calling in Golang where N is passed as a parameter. In this function which is defined above the main function the core logic of finding prime numbers resides.

  • func printPrimeNumbersBeforeN(N int)- This is the function definition which is having an integer as a parameter.

    • for i := 2; i <= N; i++ {} - This loop is running from 2 to N and for each number, we will check that it's prime or not in the nested loop.

    • for j := 2; j <= i/2; j++ {} - If we observe that if number is not prime then one of it’s factor will be half of it’s value. So, this loop is running from 2 to half the value of the current index of the outer loop. Inside the loop, we are doing the mode of the outer loop index with the inner loop index if it’s zero then we are setting isPrime with false and breaking the inner loop.

  • if isPrime {} - Once the above loop is over we are checking the value of isPrime if it’s true then we are printing that number.

Output

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

Method 2

In this Method, we are going to discuss the Sieve Eratosthenes algorithm to find the Prime Number between 1 to N. This approach is more efficient than the previous one.

Time Complexity: O(N*log(logN))

Space Complexity: O(N)

Algorithm

  • STEP 1 − Declaring the number N of type int32

  • STEP 2 − Take the input for it from the user which will tell us the range in which we have to find the prime numbers.

  • STEP 3 − Now we are calling the function which will have the logic for finding the prime numbers and printing them.

Example 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)- This is the function calling in Golang where N is passed as a parameter. In this function which is defined above the main function the core logic of finding prime numbers resides.

  • func printPrimeNumbersBeforeN(N int)- This is the function definition which is having an integer as a parameter.

    • primeArray := make([]bool, N+1)- Here we are creating the boolean array of size N + 1 with name rimeArray.

    • finitializeArray(primeArray, N+1)- This line is calling a function that is implemented above that is initializing the array with true.

    • for i := 2; i*i <= N; i++ {} - As the factors of any number will occur before the under the root of that number so we are running the for loop from 2 to under root of N.

    • for j := i * i; j <= N; j = j + i {} - This loop is running from the square of upper loop index till N and increasing the inner loop index by i and changing primeArray from true to false as the current index is already multiple of some number so it cannot be a prime number.

  • At last we are printing the prime numbers by checking the primeArray.

Output

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

These are the above two approaches to find prime numbers between 1 to N in Golang. To learn more about Golang you can explore this tutorials.

Updated on: 26-Aug-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements