Skip to content

Instantly share code, notes, and snippets.

@genghisjahn
Created March 6, 2022 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genghisjahn/4ef5bd289d7c11e349e77c17922cbf82 to your computer and use it in GitHub Desktop.
Save genghisjahn/4ef5bd289d7c11e349e77c17922cbf82 to your computer and use it in GitHub Desktop.
FindFactors
package main
import "fmt"
func main() {
for i := 1; i <= 10000; i++ {
f := getFactors(i)
if len(f) == 1 {
fmt.Println(i, "is prime.")
continue
}
if len(f) > 0 {
fmt.Println(i, "factors:", f)
}
}
}
func getFactors(num int) []int {
var result []int
a := num / 2
for i := a; i > 0; i-- {
if num%i == 0 {
result = append(result, i)
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment