primes/primes.go

31 lines
442 B
Go

package main
// Go version of a prime number finder
import(
"fmt"
"os"
"strconv"
"math"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run primes.go [MAX]")
return
}
max,_ := strconv.Atoi(os.Args[1])
for i:= 3; i < max; i+=2 {
is_prime := true
for j := 3; j <= int(math.Ceil(math.Sqrt(float64(i)))); j+=2 {
if i % j == 0 {
is_prime = false
break
}
}
if is_prime {
fmt.Println(i)
}
}
}