Skip to content

Instantly share code, notes, and snippets.

@keipes
Created December 5, 2013 23:32
Show Gist options
  • Save keipes/7816090 to your computer and use it in GitHub Desktop.
Save keipes/7816090 to your computer and use it in GitHub Desktop.
Naive O(n^2) prime factors in Go
package main
import (
"fmt";
"os";
"strconv";
"log";
)
func IsPrime(target int) bool {
isPrime := true;
if target > 2 {
for num := 2; num < target; num++ {
if target % num == 0 {
isPrime = false
break
}
}
}
return isPrime
}
func GetPrimesLessThan(target int) {
for num := 2; num <= target; num++ {
if IsPrime(num) {
fmt.Printf("%v\n", num)
}
}
}
func main() {
args := os.Args
target, err := strconv.Atoi(args[1])
if err != nil {
log.Fatal(err)
}
GetPrimesLessThan(target)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment