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 a whole or positive number greater than 1 which divides itself and one

For example, Number can be divided with remainder is zero. number 19 can be only 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 even number
- Prime Numbers cannot be divided by any other number except itself and 1
- A number which is greater than 5 and ends with 5 is not a prime number.

- Golang functions guide
- Golang For Loop control structure
- Golang For Loop Range form
- Golang read input from user

### Example program to check prime number or not

Following is a prime number check 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
```

### Example program check prime number Read input entered by user console

The following program takes input from user console and stores it in variable number. 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
```

### Example program to check prime number using ProbablyPrime function

**Probablyprime**is a function math/big package. ProbablyPrime executes Miller Rabin test to check whether a given number is prime or not.

This works well with a number of type 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`

EmoticonEmoticon