In this article we will learn about some of the frequently asked GO programming questions in technical like “checking for prime numbers using golang” Code Answer’s. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error or stack handling on go was simple and easy. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in GO. Below are some solution about “checking for prime numbers using golang” Code Answer’s.
checking for prime numbers using golang
xxxxxxxxxx
1
package main
2
3
import (
4
"fmt"
5
"math"
6
)
7
8
func IsPrime(value int) bool {
9
for i := 2; i <= int(math.Floor(float64(value)/2)); i++ {
10
if value%i == 0 {
11
return false
12
}
13
}
14
return value > 1
15
}
16
17
func IsPrimeSqrt(value int) bool {
18
for i := 2; i <= int(math.Floor(math.Sqrt(float64(value)))); i++ {
19
if value%i == 0 {
20
return false
21
}
22
}
23
return value > 1
24
}
25
26
func SieveOfEratosthenes(value int) {
27
f := make([]bool, value)
28
for i := 2; i <= int(math.Sqrt(float64(value))); i++ {
29
if f[i] == false {
30
for j := i * i; j < value; j += i {
31
f[j] = true
32
}
33
}
34
}
35
for i := 2; i < value; i++ {
36
if f[i] == false {
37
fmt.Printf("%v ", i)
38
}
39
}
40
fmt.Println("")
41
}
42
43
func main() {
44
for i := 1; i <= 100; i++ {
45
if IsPrime(i) {
46
fmt.Printf("%v ", i)
47
}
48
}
49
fmt.Println("")
50
for i := 1; i <= 100; i++ {
51
if IsPrimeSqrt(i) {
52
fmt.Printf("%v ", i)
53
}
54
}
55
fmt.Println("")
56
SieveOfEratosthenes(100)
57
}
58
checking for prime numbers using golang
xxxxxxxxxx
1
const n = 1212121
2
if big.NewInt(n).ProbablyPrime(0) {
3
fmt.Println(n, "is prime")
4
} else {
5
fmt.Println(n, "is not prime")
6
}