Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
func <span class="token function">printPrimeNumbersBeforeN</span><span class="token punctuation">(</span><span class="token class-name">N</span> <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// running the for loop from 1 to N</span>
<span class="token keyword">for</span> i <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> <span class="token class-name">N</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
<span class="token comment">// flag which will confirm that the number is Prime or not</span>
isPrime <span class="token operator">:</span><span class="token operator">=</span> <span class="token boolean">true</span>
<span class="token comment">// This for loop is checking that the current number have</span>
<span class="token comment">// any divisible other than 1</span>
<span class="token keyword">for</span> j <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> i<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">;</span> j<span class="token operator">++</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> i<span class="token operator">%</span>j <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
isPrime <span class="token operator">=</span> <span class="token boolean">false</span>
<span class="token keyword">break</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// if the value of the flag is false then the number is not prime</span>
<span class="token comment">// and we are not printing it.</span>
<span class="token keyword">if</span> isPrime <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring the variable N till which we have to find the Prime Numbers</span>
<span class="token keyword">var</span> <span class="token class-name">N</span> <span class="token keyword">int</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Enter the value of N."</span><span class="token punctuation">)</span>
<span class="token comment">// Taking the input of N from the user</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token class-name">N</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The prime numbers between 1 to"</span><span class="token punctuation">,</span> <span class="token class-name">N</span><span class="token punctuation">,</span> <span class="token string">"where N is included are -"</span><span class="token punctuation">)</span>
<span class="token comment">// calling the function to find and print the prime numbers printPrimeNumbersBeforeN(N)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">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)
}
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
