Open In App

How to Find Prime Number in Golang?

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Prime numbers are a fascinating concept in the field of mathematics and computer science. They are numbers that have only two distinct positive divisors: 1 and the number itself. This article will guide you through the process of finding prime numbers in Golang, a statically typed, compiled language known for its simplicity and efficiency.

What are Prime Numbers?

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, and so on.

For example, the number 5 is prime because it can only be divided by 1 and 5 without leaving a remainder.

Algorithm to Find Prime Numbers

The simplest method to check if a number is prime is by dividing it by all numbers less than it and checking if any division results in an integer. If we find any number that divides, we conclude that the number is not prime. If no such number is found, the number is prime.

How to Check if a Number is Prime in Go

Here’s how you can implement the prime number-finding algorithm in Golang:

Go
package main 

import ( 
"fmt"
)

func isPrime(n int) bool {
if n <= 1 {
return false
}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return false
}
}
return true
}

func main() {
fmt.Println(isPrime(5)) // Output: true
fmt.Println(isPrime(10)) // Output: false
}

Output
true
false

In this code, the isPrime function checks if a number is prime. It returns true if the number is prime and false otherwise.

Explanation:

  • package main: Declares that this is an executable program.
  • import "fmt": Imports the fmt package, which is used for formatted I/O operations, such as printing to the console.
  • func isPrime(n int) bool: Defines a function named isPrime that takes an integer n as an argument and returns a boolean value.
  • if n <= 1 { return false }: Checks if n is less than or equal to 1. If so, it returns false because 0 and 1 are not prime numbers.
  • for i := 2; i*i <= n; i++ { if n%i == 0 { return false } }: A loop that starts with i = 2 and increments i by 1 until i*i is greater than n. Inside the loop:
    • if n%i == 0 { return false }: Checks if n is divisible by i. If n is divisible by any number other than 1 and itself, it is not a prime number, and the function returns false.
  • return true: If the loop completes without finding any divisors, the function returns true, indicating that n is a prime number.
  • func main() { ... }: The main function is the entry point of the program.
  • fmt.Println(isPrime(5)): Calls the isPrime function with the argument 5 and prints the result. Since 5 is a prime number, the output is true.

How to Find Prime Numbers Up to N in Go

Let’s consider an example where we want to find all the prime numbers up to a certain number, say 20, using Golang. Here’s how you can do it:

Go
package main
import (
"fmt"
)
func isPrime(n int) bool {
if n <= 1 {
return false
}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return false
}
}
return true
}
func main() {
var n int=20;
for i := 0; i <= n; i++ {
if isPrime(i) {
fmt.Println(i)
}
}
}

Output
2
3
5
7
11
13
17
19

In this code, we use a for loop to iterate over numbers from 0 to 20. For each number, we call the isPrime function. If the function returns true, we print the number. When you run this code, it will print all the prime numbers up to 20.

Explanation:

  • func isPrime(n int) bool: This defines a function named isPrime that takes an integer n and returns a boolean value.
  • if n <= 1 { return false }: This checks if n is less than or equal to 1. If so, it returns false because 0 and 1 are not prime numbers.
  • for i := 2; i*i <= n; i++ { if n%i == 0 { return false } }: This is a loop that starts with i = 2 and increments i by 1 until i*i is greater than n. Inside the loop:
    • if n%i == 0 { return false }: This checks if n is divisible by i. If n is divisible by any number other than 1 and itself, it is not a prime number, and the function returns false.
  • return true: If the loop completes without finding any divisors, the function returns true, indicating that n is a prime number.
  • func main() { ... }: This is the entry point of the program.
  • var n int = 20: This declares a variable n and initializes it to 20.
  • for i := 0; i <= n; i++ { if isPrime(i) { fmt.Println(i) } }: This is a loop that starts with i = 0 and increments i by 1 until i is greater than n (20). Inside the loop:
    • if isPrime(i) { fmt.Println(i) }: This checks if i is a prime number by calling the isPrime function. If i is prime, it prints i.

Next Article
Article Tags :

Similar Reads