You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

37 lines
749 B

package main
// Go version of a prime number finder
import (
"fmt"
"math"
"os"
"strconv"
)
// In stead of in place printing of each prime, it seems
// fmt.Println is costly, and causes the program to take
// 7x more time, so we gain speed at the expense of RAM
// by storing them all in a slice and printing once
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run primes.go [MAX]")
return
}
//primes := make([]int, 0)
max, _ := strconv.Atoi(os.Args[1])
for i := 3; i < max; i += 2 {
isPrime := true
for j := 3; j <= int(math.Ceil(math.Sqrt(float64(i)))); j += 2 {
if i%j == 0 {
isPrime = false
break
}
}
if isPrime {
//primes = append(primes, i)
fmt.Println(i)
}
}
//fmt.Println(primes)
}