Skip to content

Instantly share code, notes, and snippets.

@NicolaM94
Last active July 12, 2023 21:21
Show Gist options
  • Save NicolaM94/8fb97503da0c4d4431fcd5a858d13cdb to your computer and use it in GitHub Desktop.
Save NicolaM94/8fb97503da0c4d4431fcd5a858d13cdb to your computer and use it in GitHub Desktop.
A function to find all the divisors of an integer n
func FindDivisor(n int) (out []int) {
out = append(out, 1)
out = append(out, n)
for j := 2; j < n/2; j++ {
if j == out[len(out)-2] || j == out[len(out)-1] {
break
}
if j*j == n {
out = append(out, j)
continue
}
if n%j == 0 {
out = append(out, j)
out = append(out, n/j)
}
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment