Different ways to check a number is a prime number or not in Golang
- Admin
- Dec 31, 2023
- Golang-examples
This post covers the three programs to check Prime numbers,
- One is prime number check for a fixed number,
- Second is to check prime number read input from user console,
- Third to check prime number check for bigint types.
The prime number in mathematics is whole or positive greater than 1 which divides itself and one
For example, the number can be divided with the remainder being zero. number 19 can be divided by 19 or 1 only.
Few important points of the Prime number.
- Numbers 0 and 1 are not prime numbers
- All the prime numbers are odd numbers, Except number 2 which is an even number
- Prime Numbers cannot be divided by any other number except itself and 1
- A number that is greater than 5 and ends with 5 is not a prime number.
To understand the below programs, You have the understanding of the following features in Go Language.
- Golang functions guide
- Golang For Loop control structure
- Golang For Loop Range form
- Golang read input from user
How to check prime number or not in go language?
Following is a code for checking the prime number for a given number.
package main
import (
"fmt"
)
func CheckPrime(number int) {
isPrime: = true
if number == 0 || number == 1 {
fmt.Printf(" %d is not a prime number\n", number)
} else {
for i: = 2;
i <= number / 2;
i++{
if number % i == 0 {
fmt.Printf(" %d is not a prime number\n", number)
isPrime = false
break
}
}
if isPrime == true {
fmt.Printf(" %d is a prime number\n", number)
}
}
}
func main() {
CheckPrime(0) // not a prime
CheckPrime(1) // not a prime
CheckPrime(3) // a prime
CheckPrime(25) // not a prime
}
When the above program is compiled and executed, Output is
0 is not a prime number
1 is not a prime number
3 is a prime number
25 is not a prime number
How to check prime number Read input entered by user console in Golang?
The following program takes input from the user console and stores it in variable numbers. Function CheckPrime is declared of int type.
package main
import (
"fmt"
)
func CheckPrime(number int) {
isPrime: = true
if number == 0 || number == 1 {
fmt.Printf(" %d is not a prime number\n", number)
} else {
for i: = 2;
i <= number / 2;
i++{
if number % i == 0 {
fmt.Printf(" %d is not a prime number\n", number)
isPrime = false
break
}
}
if isPrime == true {
fmt.Printf(" %d is a prime number\n", number)
}
}
}
func main() {
var number int
fmt.Print("Enter Number To check Prime:")
fmt.Scanln( & number)
CheckPrime(number)
}
When the above program is compiled and executed, Output is
Enter Number To check Prime:5
5 is a prime number
Enter Number To check Prime:23
23 is a prime number
Enter Number To check Prime:17
17 is a prime number
How to check prime number using ProbablyPrime function in Golang?
Probablyprime is a function math/big package. ProbablyPrime executes the Miller Rabin test to check whether a given number is prime or not.
This works well with many types of Int64.
package main
import (
"fmt"
"math/big"
)
func main() {
const number = 457862
if big.NewInt(number).ProbablyPrime(0) {
fmt.Printf(" %d is a prime number\n", number)
} else {
fmt.Printf(" %d is not a prime number\n", number)
}
}
Output is
457862 is not a prime number
Conclusion
In this tutorial, You learned multiple programs to check for prime numbers or not in the go programming language.