Last active
August 18, 2023 15:10
-
-
Save dimitrilw/81b20c491b8f792b2ee2d4ab4372fab8 to your computer and use it in GitHub Desktop.
Go (golang) get all factors of an int
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
// 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