Skip to content

Instantly share code, notes, and snippets.

@jkoop
Created November 15, 2021 19:05
Show Gist options
  • Save jkoop/7e8caab11918b9b17d73e9a2fe29fba3 to your computer and use it in GitHub Desktop.
Save jkoop/7e8caab11918b9b17d73e9a2fe29fba3 to your computer and use it in GitHub Desktop.
#include <math.h> // gcc -lm enumerateFactorsOfN.c
int enumerateFactorsOfN(int n) {
int factors = 1;
double sqrtN = sqrt(n);
int n2 = (int) sqrtN;
for (int i = 1; i <= n2; i++) {
if (n % i == 0) {
factors++;
}
}
factors *= 2;
if (sqrtN == n2) {
factors--;
}
return factors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment