Skip to content

Instantly share code, notes, and snippets.

@dimitrilw
Last active August 18, 2023 15:10
Show Gist options
  • Save dimitrilw/81b20c491b8f792b2ee2d4ab4372fab8 to your computer and use it in GitHub Desktop.
Save dimitrilw/81b20c491b8f792b2ee2d4ab4372fab8 to your computer and use it in GitHub Desktop.
Go (golang) get all factors of an int
// returns unsorted slice of *all* factors, not just primes
func getFactors(n int) []int {
res := []int{}
for i := 1; i <= int(math.Sqrt(float64(n))); i++ {
if n%i == 0 {
res = append(res, i)
if i*i != n {
res = append(res, n/i)
}
}
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment