Created
December 5, 2013 23:32
-
-
Save keipes/7816090 to your computer and use it in GitHub Desktop.
Naive O(n^2) prime factors in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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