GO takes primes up to 100
- 2020-05-09 18:43:58
- OfStack
In this paper, the example of GO language filtering method is used to find the prime number within 100. Share with you for your reference. The specific implementation method is as follows:
Idea: find a non-prime number, dig it out, and finally leave a prime number.
Here's a look at go's clean code
GO code insertion is not currently supported, use xml instead of 1.
package main
import (
"fmt"
"math"
)
func main() {
var i, j, n int
var a [101]int
for i = 1; i <= 100; i++ {
a[i] = i
}
a[1] = 0
for i = 2; i < int(math.Sqrt(100)); i++ {
for j = i + 1; j <= 100; j++ {
if (a[i] != 0) && (a[j] != 0) {
if a[j]%a[i] == 0 {
a[j] = 0
}
}
}
}
fmt.Println()
for i, n = 1, 0; i <= 100; i++ {
if a[i] != 0 {
fmt.Print(a[i], "\t")
n++
}
if n == 10 {
fmt.Println()
n = 0
}
}
}
I hope this article has been helpful to your programming of GO language.