/finddivisor.go Secret
Last active
July 12, 2023 21:21
A function to find all the divisors of an integer n
This file contains hidden or 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
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